-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathProjectPath.swift
More file actions
105 lines (89 loc) Β· 3.05 KB
/
Copy pathProjectPath.swift
File metadata and controls
105 lines (89 loc) Β· 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// ProjectPath.swift
// CodeEditUITests
//
// Created by Khan Winter on 7/10/24.
//
import Foundation
func projectPath() -> String {
return String(
URL(fileURLWithPath: #filePath)
.pathComponents
.prefix(while: { $0 != "CodeEditUITests" })
.joined(separator: "/")
.dropFirst()
)
}
private var tempProjectPathIds = Set<String>()
private let codeEditAppBundleID = "app.codeedit.CodeEdit"
private func testRunnerTempProjectURL(id: String) -> URL {
FileManager.default.temporaryDirectory
.appending(path: "CodeEditUITests")
.appending(path: id)
}
private func userHomeDirectoryOutsideSandbox() -> URL {
let homePath = NSHomeDirectoryForUser(NSUserName()) ?? NSHomeDirectory()
// UI test runners are sandboxed too; strip that container before addressing the app's container.
if let containerRange = homePath.range(of: "/Library/Containers/") {
return URL(fileURLWithPath: String(homePath[..<containerRange.lowerBound]), isDirectory: true)
}
return URL(fileURLWithPath: homePath, isDirectory: true)
}
private func appWritableTempProjectURL(id: String) -> URL {
// CodeEdit is sandboxed, so app-created temporary workspaces live in the app container.
userHomeDirectoryOutsideSandbox()
.appending(path: "Library")
.appending(path: "Containers")
.appending(path: codeEditAppBundleID)
.appending(path: "Data")
.appending(path: "tmp")
.appending(path: "CodeEditUITests")
.appending(path: id)
}
private func makeTempID() -> String {
let id = String((0..<10).map { _ in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-".randomElement()! })
if tempProjectPathIds.contains(id) {
return makeTempID()
}
tempProjectPathIds.insert(id)
return id
}
func tempProjectPath() throws -> String {
let id = makeTempID()
let path = testRunnerTempProjectURL(id: id)
try FileManager.default.createDirectory(at: path, withIntermediateDirectories: true)
return path.path(percentEncoded: false)
}
func appWritableTempProjectID() -> String {
makeTempID()
}
func appWritableTempProjectPath(id: String) -> String {
appWritableTempProjectURL(id: id).path(percentEncoded: false)
}
func cleanUpTempProjectPaths() throws {
let fileManager = FileManager.default
var cleanupError: Error?
var remainingIDs = Set<String>()
for id in tempProjectPathIds {
let paths = [
testRunnerTempProjectURL(id: id),
appWritableTempProjectURL(id: id)
]
var didFailCleanup = false
for path in paths where fileManager.fileExists(atPath: path.path(percentEncoded: false)) {
do {
try fileManager.removeItem(at: path)
} catch {
cleanupError = cleanupError ?? error
didFailCleanup = true
}
}
if didFailCleanup {
remainingIDs.insert(id)
}
}
tempProjectPathIds = remainingIDs
if let cleanupError {
throw cleanupError
}
}