Skip to content

Commit 499c17a

Browse files
committed
Fix failing tests on Windows
Due to Foundation URL/path changes.
1 parent 3b66555 commit 499c17a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

unittests/Swift/SwiftNinjaTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class SwiftNinjaTests: XCTestCase {
3535
let manifest = try NinjaManifest(
3636
path: manifestFile,
3737
workingDirectory: URL(fileURLWithPath: manifestFile)
38-
.deletingLastPathComponent().path)
38+
.deletingLastPathComponent().withUnsafeFileSystemRepresentation { try XCTUnwrap($0.map(String.init(cString:))) })
3939

4040
let expectedRule = NinjaRule(
4141
name: "CMD", variables: ["command": "ls $in $out $statementvar",
@@ -60,7 +60,7 @@ class SwiftNinjaTests: XCTestCase {
6060
])
6161
}
6262

63-
func testMissingRule() {
63+
func testMissingRule() throws {
6464
let manifestFile = makeTemporaryFile("""
6565
build output: CMD input\n
6666
""")
@@ -79,18 +79,18 @@ class SwiftNinjaTests: XCTestCase {
7979
XCTAssertThrowsError(try NinjaManifest(
8080
path: manifestFile,
8181
workingDirectory: URL(fileURLWithPath: manifestFile)
82-
.deletingLastPathComponent().path)) { error in
82+
.deletingLastPathComponent().withUnsafeFileSystemRepresentation { try XCTUnwrap($0.map(String.init(cString:))) })) { error in
8383
guard case NinjaError.invalidManifest(let errors) = error else {
8484
XCTFail("Load error was not a NinjaError.invalidManifest")
8585
return
8686
}
8787
assertExpectedError(errors)
8888
}
8989

90-
let (manifest, errors) = NinjaManifest.createNonThrowing(
90+
let (manifest, errors) = try NinjaManifest.createNonThrowing(
9191
path: manifestFile,
9292
workingDirectory: URL(fileURLWithPath: manifestFile)
93-
.deletingLastPathComponent().path)
93+
.deletingLastPathComponent().withUnsafeFileSystemRepresentation { try XCTUnwrap($0.map(String.init(cString:))) })
9494

9595
XCTAssertNil(manifest.rules["CMD"])
9696
XCTAssertEqual(manifest.statements, [

unittests/TestSupport/XCTestCase+Extensions.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ public extension XCTestCase {
1414
//
1515
// FIXME: Move to a shared location.
1616
func makeTemporaryFile(_ contents: String? = nil) -> String {
17-
let directory = NSTemporaryDirectory()
17+
var directory = NSTemporaryDirectory()
18+
#if os(Windows)
19+
// Workaround: https://github.com/apple/swift-corelibs-foundation/issues/5066
20+
if directory.hasPrefix("/") {
21+
directory.removeFirst()
22+
}
23+
#endif
1824
let filename = UUID().uuidString
1925
let fileURL = URL(fileURLWithPath: directory).appendingPathComponent(filename)
2026

@@ -26,18 +32,27 @@ public extension XCTestCase {
2632
}
2733
}
2834

35+
let filePath = fileURL.withUnsafeFileSystemRepresentation {
36+
if let path = $0.map(String.init(cString:)) {
37+
return path
38+
} else {
39+
XCTFail("Could not convert file URL to path")
40+
return String()
41+
}
42+
}
43+
2944
addTeardownBlock {
3045
do {
3146
let fileManager = FileManager.default
32-
if fileManager.fileExists(atPath: fileURL.path) {
47+
if fileManager.fileExists(atPath: filePath) {
3348
try fileManager.removeItem(at: fileURL)
34-
XCTAssertFalse(fileManager.fileExists(atPath: fileURL.path))
49+
XCTAssertFalse(fileManager.fileExists(atPath: filePath))
3550
}
3651
} catch {
3752
XCTFail("Error while deleting temporary file: \(error)")
3853
}
3954
}
4055

41-
return fileURL.path
56+
return filePath
4257
}
4358
}

0 commit comments

Comments
 (0)