Skip to content

Fix Up FileManager Tests on Windows #2277

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
May 26, 2019
Merged

Conversation

gmittert
Copy link
Contributor

@gmittert gmittert commented May 16, 2019

  • Windows directory enumerator now doesn't look in a directory it
    returns until nextObject is called again
  • Fixed some path handling platform issues
  • Properly calculate level

See SR-10738

@gmittert gmittert requested a review from compnerd May 16, 2019 23:58
@gmittert
Copy link
Contributor Author

@spevans Is there a particular scenario you're testing for with the file names ending with . (https://github.com/apple/swift-corelibs-foundation/pull/2277/files#diff-fffbc0319b30adac9f635327a750b82fR509)? Windows isn't capable of create/working with files ending with . so I'm trying to figure out what I want to do with those test cases on Windows.

@@ -345,7 +349,11 @@ extension NSString {

let automount = "/var/automount"
resolved = resolved._tryToRemovePathPrefix(automount) ?? resolved
#if os(Windows)
return String(resolved.map { $0 == "\\" ? "/" : $0 })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the path separator here?

@@ -32,7 +32,11 @@ private func _standardizedPath(_ path: String) -> String {
if !path.absolutePath {
return path._nsObject.standardizingPath
}
#if os(Windows)
return String(path.map({$0 == "/" ? "\\" : $0}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the standardizing should collapse relative components (i.e. .. should be chased, . dropped). As such, any reason to not just use PathCchCanonicalizeEx?

@@ -118,7 +118,11 @@ extension String {
}

internal var absolutePath: Bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bleh! It would be nice to rename this to isAbsolutePath.

if item == "item" {
item1FileAttributes = e.fileAttributes
} else if item == "path2/item" {
item2FileAttributes = e.fileAttributes
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this hunk equivalent to just changing L459 (R) to } else if item == "path2/item" || item == "path2\\item" {?

}
#if os(Windows)
XCTAssertEqual(foundItems, Set(["item", "path2", "path2\\item"]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you can span the Set over two lines and do an #if os(Windows) to swap out the third element.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't. You can move the set creation elsewhere, or make it mutable and #if os(Windows) the addition.


func findFirstValidFile() -> URL? {
while let url = _stack.popLast() {
let dirFSR = url.withUnsafeFileSystemRepresentation { $0.flatMap { fsr in String(utf8String: fsr) } } ?? ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this seems pretty expensive.

func findFirstValidFile() -> URL? {
while let url = _stack.popLast() {
let dirFSR = url.withUnsafeFileSystemRepresentation { $0.flatMap { fsr in String(utf8String: fsr) } } ?? ""
if !FileManager.default.fileExists(atPath: dirFSR, isDirectory: nil) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? Should dirFSR be renamed? If isDirectory is nil, then we do not know if it is a file or directory, so it seems to be misleading.

return nil

var isDir: ObjCBool = false
let dirFSR = _current.withUnsafeFileSystemRepresentation { $0.flatMap { fsr in String(utf8String: fsr) } } ?? ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, the dirFSR is really misleading here. Can we rename this as we do not know what the path points to.

@gmittert
Copy link
Contributor Author

  • Cleaned up the directory enumerator code to be less terrible
  • Fixed some slash issues and removed the extra conversions

@gmittert
Copy link
Contributor Author

@swift-ci please test

@gmittert
Copy link
Contributor Author

@swift-ci please test

@@ -4474,7 +4474,7 @@ CFStringRef CFURLCreateStringWithFileSystemPath(CFAllocatorRef allocator, CFURLR
// and do a linked-on-or-later check so we don't break third parties.
// See <rdar://problem/4003028> Converting volume name from POSIX to HFS form fails and
// <rdar://problem/4018895> CF needs to back out 4003028 for icky details.
if ( relPath && CFURLHasDirectoryPath(anURL) && CFStringGetLength(relPath) > 1 && CFStringGetCharacterAtIndex(relPath, CFStringGetLength(relPath)-1) == '/') {
if ( relPath && CFURLHasDirectoryPath(anURL) && CFStringGetLength(relPath) > 1 && CFStringGetCharacterAtIndex(relPath, CFStringGetLength(relPath)-1) == _CFGetSlash()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is from a URL, is this change needed, and if it is from a URL is this not incorrect? CC: @millenomi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it's not, I've removed that change.

let itemPath1 = basePath + "itemFile1"
#if os(Windows)
// Filenames ending with '.' are not valid on Windows, so don't bother testing them
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@millenomi - what do you think about this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine.

- Windows directory enumerator now doesn't look in a directory it
  returns until nextObject is called again
- Fixed some path handling platform issues
- Properly calculate level
Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me now.

@millenomi @spevans - any comments on the behavioural changes on Windows for FileManager?

@gmittert
Copy link
Contributor Author

@swift-ci please test

@millenomi
Copy link
Contributor

@swift-ci please test and merge

1 similar comment
@millenomi
Copy link
Contributor

@swift-ci please test and merge

@compnerd compnerd merged commit c5357f3 into swiftlang:master May 26, 2019
@gmittert gmittert deleted the 94.5FixFM branch June 10, 2019 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants