-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathProjectPath.swift
More file actions
68 lines (57 loc) · 1.77 KB
/
Copy pathProjectPath.swift
File metadata and controls
68 lines (57 loc) · 1.77 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
//
// 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 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 baseDir = FileManager.default.temporaryDirectory.appending(path: "CodeEditUITests")
let id = makeTempID()
let path = baseDir.appending(path: id)
try FileManager.default.createDirectory(at: path, withIntermediateDirectories: true)
return path.path(percentEncoded: false)
}
func appWritableTempProjectID() -> String {
makeTempID()
}
func cleanUpTempProjectPaths() throws {
let fileManager = FileManager.default
let baseDir = FileManager.default.temporaryDirectory.appending(path: "CodeEditUITests")
var cleanupError: Error?
var remainingIDs = Set<String>()
for id in tempProjectPathIds {
let path = baseDir.appending(path: id)
guard fileManager.fileExists(atPath: path.path(percentEncoded: false)) else {
continue
}
do {
try fileManager.removeItem(at: path)
} catch {
cleanupError = cleanupError ?? error
remainingIDs.insert(id)
}
}
tempProjectPathIds = remainingIDs
if let cleanupError {
throw cleanupError
}
}