Skip to content

Commit df5aa9b

Browse files
authored
subpathsOfDirectory(atPath:) should throw on empty paths (#869)
1 parent 8d8920e commit df5aa9b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ struct _Win32DirectoryContentsSequence: Sequence {
3333
init(path: String, appendSlashForDirectory: Bool, prefix: [String]) {
3434
self.slash = appendSlashForDirectory
3535

36+
guard !path.isEmpty else {
37+
self.error = CocoaError.errorWithFilePath(.fileReadInvalidFileName, path)
38+
return
39+
}
40+
3641
do {
3742
hFind = try "\(path)\\*".withNTPathRepresentation {
3843
// We use `FindFirstFileExW` to avoid the lookup of the short name of the file. We never consult the field and this can speed up the file enumeration.

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ final class FileManagerTests : XCTestCase {
278278
XCTAssertThrowsError(try $0.subpathsOfDirectory(atPath: "does_not_exist")) {
279279
XCTAssertEqual(($0 as? CocoaError)?.code, .fileReadNoSuchFile)
280280
}
281+
282+
XCTAssertThrowsError(try $0.subpathsOfDirectory(atPath: "")) {
283+
#if os(Windows)
284+
XCTAssertEqual(($0 as? CocoaError)?.code, .fileReadInvalidFileName)
285+
#else
286+
XCTAssertEqual(($0 as? CocoaError)?.code, .fileReadNoSuchFile)
287+
#endif
288+
}
281289

282290
let fullContents = ["dir1", "dir1/dir2", "dir1/dir2/Bar", "dir1/dir2/Foo", "dir1/dir3", "dir1/dir3/Baz", "symlinks", "symlinks/Bar", "symlinks/Foo", "symlinks/Parent"]
283291
let cwd = $0.currentDirectoryPath

0 commit comments

Comments
 (0)