Skip to content

Commit a66f6c4

Browse files
authored
Merge pull request #506 from abertelrud/move-rmfiletree-to-basic
Move `removeFileTree()` down from Utility to Basics
2 parents 62e264b + ae5fa37 commit a66f6c4

File tree

10 files changed

+53
-77
lines changed

10 files changed

+53
-77
lines changed

Sources/Basic/PathShims.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public func makeDirectories(_ path: AbsolutePath) throws {
7171
#endif
7272
}
7373

74+
/// Recursively deletes the file system entity at `path`. If there is no file system entity at `path`, this function does nothing (in particular, this is not considered to be an error).
75+
public func removeFileTree(_ path: AbsolutePath) throws {
76+
#if os(Linux)
77+
try FileManager.default().removeItem(atPath: path.asString)
78+
#else
79+
try FileManager.default.removeItem(atPath: path.asString)
80+
#endif
81+
}
82+
7483
/// Creates a symbolic link at `path` whose content points to `dest`. If `relative` is true, the symlink contents will be a relative path, otherwise it will be absolute.
7584
public func symlink(_ path: AbsolutePath, pointingAt dest: AbsolutePath, relative: Bool = true) throws {
7685
let destString = relative ? dest.relative(to: path.parentDirectory).asString : dest.asString

Sources/Commands/SwiftBuildTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ public struct SwiftBuildTool: SwiftTool {
150150

151151
case .clean(.dist):
152152
if opts.path.packages.asString.exists {
153-
try Utility.removeFileTree(opts.path.packages.asString)
153+
try removeFileTree(opts.path.packages)
154154
}
155155
fallthrough
156156

157157
case .clean(.build):
158158
// FIXME: This test is lame, `removeFileTree` shouldn't error on this.
159159
if opts.path.build.asString.exists {
160-
try Utility.removeFileTree(opts.path.build.asString)
160+
try removeFileTree(opts.path.build)
161161
}
162162
}
163163

Sources/Commands/SwiftPackageTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public struct SwiftPackageTool: SwiftTool {
195195
throw Error.repositoryHasChanges(item.asString)
196196
}
197197
}
198-
try Utility.removeFileTree(opts.path.packages.asString)
198+
try removeFileTree(opts.path.packages)
199199
}
200200
fallthrough
201201

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public final class ManifestLoader {
126126
guard let toml = try localFileSystem.readFileContents(filePath).asString else {
127127
throw ManifestParseError.invalidEncoding
128128
}
129-
try Utility.removeFileTree(filePath.asString) // Delete the temp file after reading it
129+
try removeFileTree(filePath) // Delete the temp file after reading it
130130

131131
return toml != "" ? toml : nil
132132
}

Sources/Utility/removeFileTree.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

Tests/Basic/PathShimTests.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,38 @@ class PathShimTests : XCTestCase {
5858
try! makeDirectories(dirPath)
5959
}
6060

61+
func testRecursiveDirectoryRemoval() {
62+
// For the tests we'll need a temporary directory.
63+
let tmpDir = try! TemporaryDirectory(removeTreeOnDeinit: true)
64+
65+
// Create a couple of directories. The first one shouldn't end up getting removed, the second one will.
66+
let keepDirPath = tmpDir.path.appending(components: "abc1")
67+
try! makeDirectories(keepDirPath)
68+
let tossDirPath = tmpDir.path.appending(components: "abc2", "def", "ghi", "mno", "pqr")
69+
try! makeDirectories(tossDirPath)
70+
71+
// Create a symbolic link in a directory to be removed; it points to a directory to not remove.
72+
let slnkPath = tossDirPath.appending(components: "slnk")
73+
try! symlink(create: slnkPath.asString, pointingAt: keepDirPath.asString, relativeTo: tossDirPath.asString)
74+
75+
// Make sure the symbolic link got set up correctly.
76+
XCTAssertTrue(slnkPath.asString.isSymlink)
77+
XCTAssertEqual(resolveSymlinks(slnkPath), keepDirPath)
78+
XCTAssertTrue(resolveSymlinks(slnkPath).asString.isDirectory)
79+
80+
// Now remove the directory hierarchy that contains the symlink.
81+
try! removeFileTree(tossDirPath)
82+
83+
// Make sure it got removed, along with the symlink, but that the target of the symlink remains.
84+
XCTAssertFalse(tossDirPath.asString.exists)
85+
XCTAssertFalse(tossDirPath.asString.isDirectory)
86+
XCTAssertTrue(keepDirPath.asString.exists)
87+
XCTAssertTrue(keepDirPath.asString.isDirectory)
88+
}
89+
6190
static var allTests = [
62-
("testResolvingSymlinks", testResolvingSymlinks),
63-
("testRescursiveDirectoryCreation", testRescursiveDirectoryCreation)
91+
("testResolvingSymlinks", testResolvingSymlinks),
92+
("testRescursiveDirectoryCreation", testRescursiveDirectoryCreation),
93+
("testRecursiveDirectoryRemoval", testRecursiveDirectoryRemoval)
6494
]
6595
}

Tests/Functional/InvalidLayoutTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class InvalidLayoutsTestCase: XCTestCase {
3333
*/
3434
fixture(name: "InvalidLayouts/Generic1") { prefix in
3535
XCTAssertBuildFails(prefix)
36-
try Utility.removeFileTree(prefix.appending("main.swift").asString)
36+
try removeFileTree(prefix.appending("main.swift"))
3737
XCTAssertBuilds(prefix)
3838
}
3939
}
@@ -48,7 +48,7 @@ class InvalidLayoutsTestCase: XCTestCase {
4848
*/
4949
fixture(name: "InvalidLayouts/Generic2") { prefix in
5050
XCTAssertBuildFails(prefix)
51-
try Utility.removeFileTree(prefix.appending("main.swift").asString)
51+
try removeFileTree(prefix.appending("main.swift"))
5252
XCTAssertBuilds(prefix)
5353
}
5454
}
@@ -63,7 +63,7 @@ class InvalidLayoutsTestCase: XCTestCase {
6363
*/
6464
fixture(name: "InvalidLayouts/Generic3") { prefix in
6565
XCTAssertBuildFails(prefix)
66-
try Utility.removeFileTree(prefix.appending("Sources").appending("main.swift").asString)
66+
try removeFileTree(prefix.appending("Sources").appending("main.swift"))
6767
XCTAssertBuilds(prefix)
6868
}
6969
}
@@ -78,7 +78,7 @@ class InvalidLayoutsTestCase: XCTestCase {
7878
*/
7979
fixture(name: "InvalidLayouts/Generic4") { prefix in
8080
XCTAssertBuildFails(prefix)
81-
try Utility.removeFileTree(prefix.appending("main.swift").asString)
81+
try removeFileTree(prefix.appending("main.swift"))
8282
XCTAssertBuilds(prefix)
8383
}
8484
}
@@ -100,8 +100,8 @@ class InvalidLayoutsTestCase: XCTestCase {
100100
// determineTargets() but also we are saying: this
101101
// layout is only for *very* simple projects.
102102

103-
try Utility.removeFileTree(prefix.appending("Foo").appending("Foo.swift").asString)
104-
try Utility.removeFileTree(prefix.appending("Foo").asString)
103+
try removeFileTree(prefix.appending("Foo").appending("Foo.swift"))
104+
try removeFileTree(prefix.appending("Foo"))
105105
XCTAssertBuilds(prefix)
106106
}
107107
}

Tests/Utility/PathTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ class StatTests: XCTestCase {
181181
XCTAssertTrue(root.appending("symlink").asString.isDirectory)
182182
XCTAssertTrue(root.appending("symlink").asString.isSymlink)
183183

184-
try removeFileTree(root.appending("foo/bar").asString)
185-
try removeFileTree(root.appending("foo").asString)
184+
try removeFileTree(root.appending("foo/bar"))
185+
try removeFileTree(root.appending("foo"))
186186

187187
XCTAssertTrue(root.appending("symlink").asString.isSymlink)
188188
XCTAssertFalse(root.appending("symlink").asString.isDirectory)

Tests/Utility/RmtreeTests.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

Tests/Utility/XCTestManifests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public func allTests() -> [XCTestCaseEntry] {
1919
testCase(PathTests.allTests),
2020
testCase(PkgConfigParserTests.allTests),
2121
testCase(RelativePathTests.allTests),
22-
testCase(RmtreeTests.allTests),
2322
testCase(ShellTests.allTests),
2423
testCase(StatTests.allTests),
2524
testCase(StringTests.allTests),

0 commit comments

Comments
 (0)