Skip to content

subpathsOfDirectory(atPath:) should throw on empty paths #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct _Win32DirectoryContentsSequence: Sequence {
init(path: String, appendSlashForDirectory: Bool, prefix: [String]) {
self.slash = appendSlashForDirectory

guard !path.isEmpty else {
self.error = CocoaError.errorWithFilePath(.fileReadInvalidFileName, path)
return
}

do {
hFind = try "\(path)\\*".withNTPathRepresentation {
// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ final class FileManagerTests : XCTestCase {
XCTAssertThrowsError(try $0.subpathsOfDirectory(atPath: "does_not_exist")) {
XCTAssertEqual(($0 as? CocoaError)?.code, .fileReadNoSuchFile)
}

XCTAssertThrowsError(try $0.subpathsOfDirectory(atPath: "")) {
#if os(Windows)
XCTAssertEqual(($0 as? CocoaError)?.code, .fileReadInvalidFileName)
#else
XCTAssertEqual(($0 as? CocoaError)?.code, .fileReadNoSuchFile)
#endif
}

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