Skip to content

Commit 1d92dfc

Browse files
committed
tests: Use withTemporaryDirectory wrapper in body of testPackageInitExecutable
`testPackageInitExecutable` checks that the default Swift package generated by `swift package init` can be built with each SDK under test. The packages are already generated in temporary directories, so they do not suffer from the deadlocking problem when run under `swift test`. Currently each test run uses a directory with the same name in the user's temporary directory. This means that the test has to remove anything left behind by a previous run before starting a new on. Using `withTemporaryDirectory` avoids this problem because it creates a fresh temporary directory for each run and deletes it automatically when the body closure returns.
1 parent 881b9c7 commit 1d92dfc

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

Tests/SwiftSDKGeneratorTests/EndToEndTests.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ final class EndToEndTests: XCTestCase {
103103
func testPackageInitExecutable() async throws {
104104
throw XCTSkip("EndToEnd tests currently deadlock under `swift test`: https://github.com/swiftlang/swift-sdk-generator/issues/143")
105105

106-
let fm = FileManager.default
107-
108106
var packageDirectory = FilePath(#filePath)
109107
packageDirectory.removeLastComponent()
110108
packageDirectory.removeLastComponent()
@@ -125,24 +123,27 @@ final class EndToEndTests: XCTestCase {
125123
}
126124

127125
for testcase in self.testcases {
128-
let testPackageURL = FileManager.default.temporaryDirectory.appendingPathComponent("swift-sdk-generator-test")
129-
let testPackageDir = FilePath(testPackageURL.path)
130-
try? fm.removeItem(atPath: testPackageDir.string)
131-
try fm.createDirectory(atPath: testPackageDir.string, withIntermediateDirectories: true)
132-
133-
try await Shell.run("swift package --package-path \(testPackageDir) init --type executable")
134-
let main_swift = testPackageURL.appendingPathComponent("Sources/main.swift")
135-
try testcase.write(to: main_swift, atomically: true, encoding: .utf8)
136-
137-
var buildOutput = try await Shell.readStdout(
138-
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName)"
139-
)
140-
XCTAssertTrue(buildOutput.contains("Build complete!"))
141-
try await Shell.run("rm -rf \(testPackageDir.appending(".build"))")
142-
buildOutput = try await Shell.readStdout(
143-
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName) --static-swift-stdlib"
144-
)
145-
XCTAssertTrue(buildOutput.contains("Build complete!"))
126+
try await FileManager.default.withTemporaryDirectory(logger: logger) { tempDir in
127+
let testPackageURL = tempDir.appendingPathComponent("swift-sdk-generator-test")
128+
let testPackageDir = FilePath(testPackageURL.path)
129+
try FileManager.default.createDirectory(atPath: testPackageDir.string, withIntermediateDirectories: true)
130+
131+
try await Shell.run("swift package --package-path \(testPackageDir) init --type executable")
132+
let main_swift = testPackageURL.appendingPathComponent("Sources/main.swift")
133+
try testcase.write(to: main_swift, atomically: true, encoding: .utf8)
134+
135+
var buildOutput = try await Shell.readStdout(
136+
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName)"
137+
)
138+
XCTAssertTrue(buildOutput.contains("Build complete!"))
139+
140+
try await Shell.run("rm -rf \(testPackageDir.appending(".build"))")
141+
142+
buildOutput = try await Shell.readStdout(
143+
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName) --static-swift-stdlib"
144+
)
145+
XCTAssertTrue(buildOutput.contains("Build complete!"))
146+
}
146147
}
147148
}
148149
}

0 commit comments

Comments
 (0)