Skip to content

Commit

Permalink
[TSCBasic] Don't delete empty tmpdir
Browse files Browse the repository at this point in the history
Only delete tmpdir when explicitly asked by clients.
  • Loading branch information
aciidgh committed Apr 3, 2020
1 parent 6c0e6b5 commit ae8ccef
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
7 changes: 1 addition & 6 deletions Sources/TSCBasic/TemporaryFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,7 @@ public func withTemporaryDirectory<Result>(
let path = AbsolutePath(String(cString: template))

defer {
let isEmptyDirectory: (String) -> Bool = { path in
guard let contents = try? FileManager.default.contentsOfDirectory(atPath: path) else { return false }
return contents.isEmpty
}

if removeTreeOnDeinit || isEmptyDirectory(path.pathString) {
if removeTreeOnDeinit {
_ = try? FileManager.default.removeItem(atPath: path.pathString)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/TSCBasicTests/TemporaryFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class TemporaryFileTests: XCTestCase {

func testBasicTemporaryDirectory() throws {
// Test can create and remove temp directory.
let path1: AbsolutePath = try withTemporaryDirectory { tempDirPath in
let path1: AbsolutePath = try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDirPath in
XCTAssertTrue(localFileSystem.isDirectory(tempDirPath))
return tempDirPath
}
Expand Down Expand Up @@ -123,8 +123,8 @@ class TemporaryFileTests: XCTestCase {
}

func testCanCreateUniqueTempDirectories() throws {
let (pathOne, pathTwo): (AbsolutePath, AbsolutePath) = try withTemporaryDirectory { pathOne in
let pathTwo: AbsolutePath = try withTemporaryDirectory { pathTwo in
let (pathOne, pathTwo): (AbsolutePath, AbsolutePath) = try withTemporaryDirectory(removeTreeOnDeinit: true) { pathOne in
let pathTwo: AbsolutePath = try withTemporaryDirectory(removeTreeOnDeinit: true) { pathTwo in
XCTAssertTrue(localFileSystem.isDirectory(pathOne))
XCTAssertTrue(localFileSystem.isDirectory(pathTwo))
// Their paths should be different.
Expand Down

0 comments on commit ae8ccef

Please sign in to comment.