Skip to content

Commit db26074

Browse files
committed
Add additional safety checks when creating and removing temporary directories
1 parent 5d4ce3e commit db26074

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/SwiftDocCTestUtilities/XCTestCase+TemporaryDirectory.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,20 @@ public extension XCTestCase {
3939
tempURL.standardize()
4040

4141
addTeardownBlock {
42-
try? fileManager.removeItem(at: baseURL)
42+
do {
43+
if fileManager.fileExists(atPath: baseURL.path) {
44+
try fileManager.removeItem(at: baseURL)
45+
}
46+
} catch {
47+
XCTFail("Failed to remove temporary directory: '\(error)'")
48+
}
4349
}
50+
51+
if !fileManager.fileExists(atPath: bundleParentDir.path) {
52+
// Create the base URL directory without intermediate directories so that an error is raised if the parent directory doesn't exist.
53+
try fileManager.createDirectory(at: baseURL, withIntermediateDirectories: false, attributes: nil)
54+
}
55+
4456
try fileManager.createDirectory(at: tempURL, withIntermediateDirectories: true, attributes: nil)
4557

4658
return tempURL

0 commit comments

Comments
 (0)