Skip to content

Commit 159a6ef

Browse files
committed
Address UI test re-review feedback
1 parent a854487 commit 159a6ef

4 files changed

Lines changed: 61 additions & 12 deletions

File tree

CodeEdit/AppDelegate.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
8585
}
8686
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
8787
} catch {
88-
logger.error("Failed to create UI test workspace: \(error.localizedDescription, privacy: .public)")
88+
let path = url.path(percentEncoded: false)
89+
let message = "Failed to create UI test workspace at \(path): \(error.localizedDescription)"
90+
logger.error("\(message, privacy: .public)")
91+
fatalError(message)
8992
}
9093

9194
return url

CodeEditUITests/App.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ enum App {
3535
tempDirID
3636
]
3737
application.launch()
38-
return (application, tempDirID)
38+
return (application, appWritableTempProjectPath(id: tempDirID))
3939
}
4040

4141
static func launch() -> XCUIApplication {

CodeEditUITests/Features/NavigatorArea/ProjectNavigator/ProjectNavigatorFileManagementUITests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ final class ProjectNavigatorFileManagementUITests: XCTestCase {
3030
navigator = Query.Window.getProjectNavigator(window)
3131
XCTAssertTrue(navigator.exists, "Navigator not found")
3232
XCTAssertEqual(Query.Navigator.getRows(navigator).count, 1, "Found more than just the root file.")
33+
34+
if name.contains("testCreateNewFiles") {
35+
var isDirectory: ObjCBool = false
36+
XCTAssertTrue(
37+
FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory),
38+
"App-writable temp directory was not created at \(path ?? "")"
39+
)
40+
XCTAssertTrue(isDirectory.boolValue, "App-writable temp project path is not a directory")
41+
}
3342
}
3443
}
3544

CodeEditUITests/ProjectPath.swift

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@ func projectPath() -> String {
1818
}
1919

2020
private var tempProjectPathIds = Set<String>()
21+
private let codeEditAppBundleID = "app.codeedit.CodeEdit"
22+
23+
private func testRunnerTempProjectURL(id: String) -> URL {
24+
FileManager.default.temporaryDirectory
25+
.appending(path: "CodeEditUITests")
26+
.appending(path: id)
27+
}
28+
29+
private func userHomeDirectoryOutsideSandbox() -> URL {
30+
let homePath = NSHomeDirectoryForUser(NSUserName()) ?? NSHomeDirectory()
31+
// UI test runners are sandboxed too; strip that container before addressing the app's container.
32+
if let containerRange = homePath.range(of: "/Library/Containers/") {
33+
return URL(fileURLWithPath: String(homePath[..<containerRange.lowerBound]), isDirectory: true)
34+
}
35+
return URL(fileURLWithPath: homePath, isDirectory: true)
36+
}
37+
38+
private func appWritableTempProjectURL(id: String) -> URL {
39+
// CodeEdit is sandboxed, so app-created temporary workspaces live in the app container.
40+
userHomeDirectoryOutsideSandbox()
41+
.appending(path: "Library")
42+
.appending(path: "Containers")
43+
.appending(path: codeEditAppBundleID)
44+
.appending(path: "Data")
45+
.appending(path: "tmp")
46+
.appending(path: "CodeEditUITests")
47+
.appending(path: id)
48+
}
2149

2250
private func makeTempID() -> String {
2351
let id = String((0..<10).map { _ in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-".randomElement()! })
@@ -29,9 +57,8 @@ private func makeTempID() -> String {
2957
}
3058

3159
func tempProjectPath() throws -> String {
32-
let baseDir = FileManager.default.temporaryDirectory.appending(path: "CodeEditUITests")
3360
let id = makeTempID()
34-
let path = baseDir.appending(path: id)
61+
let path = testRunnerTempProjectURL(id: id)
3562
try FileManager.default.createDirectory(at: path, withIntermediateDirectories: true)
3663
return path.path(percentEncoded: false)
3764
}
@@ -40,22 +67,32 @@ func appWritableTempProjectID() -> String {
4067
makeTempID()
4168
}
4269

70+
func appWritableTempProjectPath(id: String) -> String {
71+
appWritableTempProjectURL(id: id).path(percentEncoded: false)
72+
}
73+
4374
func cleanUpTempProjectPaths() throws {
4475
let fileManager = FileManager.default
45-
let baseDir = FileManager.default.temporaryDirectory.appending(path: "CodeEditUITests")
4676
var cleanupError: Error?
4777
var remainingIDs = Set<String>()
4878

4979
for id in tempProjectPathIds {
50-
let path = baseDir.appending(path: id)
51-
guard fileManager.fileExists(atPath: path.path(percentEncoded: false)) else {
52-
continue
80+
let paths = [
81+
testRunnerTempProjectURL(id: id),
82+
appWritableTempProjectURL(id: id)
83+
]
84+
var didFailCleanup = false
85+
86+
for path in paths where fileManager.fileExists(atPath: path.path(percentEncoded: false)) {
87+
do {
88+
try fileManager.removeItem(at: path)
89+
} catch {
90+
cleanupError = cleanupError ?? error
91+
didFailCleanup = true
92+
}
5393
}
5494

55-
do {
56-
try fileManager.removeItem(at: path)
57-
} catch {
58-
cleanupError = cleanupError ?? error
95+
if didFailCleanup {
5996
remainingIDs.insert(id)
6097
}
6198
}

0 commit comments

Comments
 (0)