Skip to content

Commit 6047f59

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 222d3a0 commit 6047f59

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

Tests/SwiftSDKGeneratorTests/EndToEndTests.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ final class EndToEndTests: XCTestCase {
9494
}
9595

9696
func testPackageInitExecutable() async throws {
97-
let fm = FileManager.default
98-
9997
var packageDirectory = FilePath(#filePath)
10098
packageDirectory.removeLastComponent()
10199
packageDirectory.removeLastComponent()
@@ -116,29 +114,31 @@ final class EndToEndTests: XCTestCase {
116114
}
117115

118116
for testcase in self.testcases {
119-
let testPackageURL = FileManager.default.temporaryDirectory.appendingPathComponent("swift-sdk-generator-test")
120-
let testPackageDir = FilePath(testPackageURL.path)
121-
try? fm.removeItem(atPath: testPackageDir.string)
122-
try fm.createDirectory(atPath: testPackageDir.string, withIntermediateDirectories: true)
123-
124-
try await Shell.run("swift package --package-path \(testPackageDir) init --type executable")
125-
let main_swift = testPackageURL.appendingPathComponent("Sources/main.swift")
126-
try testcase.write(to: main_swift, atomically: true, encoding: .utf8)
127-
128-
var buildOutput = try await Shell.readStdout(
129-
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName)"
130-
)
131-
XCTAssertTrue(buildOutput.contains("Build complete!"))
132-
try await Shell.run("rm -rf \(testPackageDir.appending(".build"))")
133-
buildOutput = try await Shell.readStdout(
134-
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName) --static-swift-stdlib"
135-
)
136-
XCTAssertTrue(buildOutput.contains("Build complete!"))
117+
try await FileManager.default.withTemporaryDirectory(logger: logger) { tempDir in
118+
let testPackageURL = tempDir.appendingPathComponent("swift-sdk-generator-test")
119+
let testPackageDir = FilePath(testPackageURL.path)
120+
try FileManager.default.createDirectory(atPath: testPackageDir.string, withIntermediateDirectories: true)
121+
122+
try await Shell.run("swift package --package-path \(testPackageDir) init --type executable")
123+
let main_swift = testPackageURL.appendingPathComponent("Sources/main.swift")
124+
try testcase.write(to: main_swift, atomically: true, encoding: .utf8)
125+
126+
var buildOutput = try await Shell.readStdout(
127+
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName)"
128+
)
129+
XCTAssertTrue(buildOutput.contains("Build complete!"))
130+
131+
try await Shell.run("rm -rf \(testPackageDir.appending(".build"))")
132+
133+
buildOutput = try await Shell.readStdout(
134+
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName) --static-swift-stdlib"
135+
)
136+
XCTAssertTrue(buildOutput.contains("Build complete!"))
137+
}
137138
}
138139
}
139140
}
140141

141-
142142
func testRepeatedSDKBuilds() async throws {
143143
var packageDirectory = FilePath(#filePath)
144144
packageDirectory.removeLastComponent()

0 commit comments

Comments
 (0)