Skip to content

Commit de0385c

Browse files
authored
Merge pull request #2666 from aciidb0mb3r/fix-tmpdir-bug
[TSCBasic] Don't delete empty tmpdir
2 parents bee4b94 + f1a91fe commit de0385c

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

swift-tools-support-core/Sources/TSCBasic/TemporaryFile.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,7 @@ public func withTemporaryDirectory<Result>(
217217
let path = AbsolutePath(String(cString: template))
218218

219219
defer {
220-
let isEmptyDirectory: (String) -> Bool = { path in
221-
guard let contents = try? FileManager.default.contentsOfDirectory(atPath: path) else { return false }
222-
return contents.isEmpty
223-
}
224-
225-
if removeTreeOnDeinit || isEmptyDirectory(path.pathString) {
220+
if removeTreeOnDeinit {
226221
_ = try? FileManager.default.removeItem(atPath: path.pathString)
227222
}
228223
}

swift-tools-support-core/Tests/TSCBasicTests/TemporaryFileTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class TemporaryFileTests: XCTestCase {
9393

9494
func testBasicTemporaryDirectory() throws {
9595
// Test can create and remove temp directory.
96-
let path1: AbsolutePath = try withTemporaryDirectory { tempDirPath in
96+
let path1: AbsolutePath = try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDirPath in
9797
XCTAssertTrue(localFileSystem.isDirectory(tempDirPath))
9898
return tempDirPath
9999
}
@@ -123,8 +123,8 @@ class TemporaryFileTests: XCTestCase {
123123
}
124124

125125
func testCanCreateUniqueTempDirectories() throws {
126-
let (pathOne, pathTwo): (AbsolutePath, AbsolutePath) = try withTemporaryDirectory { pathOne in
127-
let pathTwo: AbsolutePath = try withTemporaryDirectory { pathTwo in
126+
let (pathOne, pathTwo): (AbsolutePath, AbsolutePath) = try withTemporaryDirectory(removeTreeOnDeinit: true) { pathOne in
127+
let pathTwo: AbsolutePath = try withTemporaryDirectory(removeTreeOnDeinit: true) { pathTwo in
128128
XCTAssertTrue(localFileSystem.isDirectory(pathOne))
129129
XCTAssertTrue(localFileSystem.isDirectory(pathTwo))
130130
// Their paths should be different.

0 commit comments

Comments
 (0)