Skip to content

Fixing up Windows TestFileManager #2339

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
Jul 11, 2019

Conversation

gmittert
Copy link
Contributor

@gmittert gmittert commented Jun 12, 2019

  • _copyOrLinkDirectoryHelper should throw on errors
  • Accounting for that Windows permissions aren't compatible with posix ones
  • When given "" as a file name, most Windows apis return file not found, not invalid filename

@gmittert
Copy link
Contributor Author

cc @compnerd

@swift-ci please test linux

let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
#if os(Windows)
XCTAssertEqual(code, .fileReadNoSuchFile)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is an issue in the error reporting — if a filename is invalid, the API should return .fileReadInvalidFileName no matter what the host OS thinks the right error is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could do that, though it would mean every time we throw because for a Win32 api call failed, we'd have to check if the error is ERROR_FILE_NOT_FOUND then do some checks to see if it should actually be invalid path or perhaps some other more specific error. That doesn't seem cheap to me, but it would probably only be in the exceptional case.

@@ -1378,15 +1451,15 @@ VIDEOS=StopgapVideos
XCTAssertTrue(fm.isDeletableFile(atPath: ""))

XCTAssertThrowsError(try fm.attributesOfItem(atPath: "")) {
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
Copy link
Contributor

Choose a reason for hiding this comment

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

This may not build on macOS.

Copy link
Contributor

@millenomi millenomi left a comment

Choose a reason for hiding this comment

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

See feedback.

@gmittert gmittert force-pushed the VariousFMFixes branch 2 times, most recently from 03fb43b to 8d5edd9 Compare June 17, 2019 17:59
@gmittert
Copy link
Contributor Author

gmittert commented Jun 17, 2019

Removed the NSError changes, and have Windows follow posix such that "" is an invalid file instead of being an unfound file.

@gmittert
Copy link
Contributor Author

@swift-ci please test

@gmittert
Copy link
Contributor Author

Rebase

@swift-ci please test linux

@gmittert
Copy link
Contributor Author

@swift-ci please test linux

@compnerd
Copy link
Member

I think that doing the remapping in the _NSErrorWithWindowsErrno is better. The new function is less likely to get used I think.

- _copyOrLinkDirectoryHelper should throw on errors
- Accounting for that Windows permissions aren't compatible with posix ones
- When given "" as a file name, most Windows apis return file not found,
  not invalid filename. Catch this and istead throw an invalid filename
  as posix does.
@gmittert
Copy link
Contributor Author

I think that doing the remapping in the _NSErrorWithWindowsErrno is better. The new function is less likely to get used I think.

Fair, I've added a [Path]? parameter to _NSErrorWithWindowsErrno instead

@gmittert
Copy link
Contributor Author

@swift-ci please test linux

@compnerd
Copy link
Member

Thanks, this LGTM.

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.

Looks good modulo the confusion on the two sites not propagating the error

@@ -358,8 +360,7 @@ extension FileManager {
}

internal func _copyOrLinkDirectoryHelper(atPath srcPath: String, toPath dstPath: String, variant: String = "Copy", _ body: (String, String, FileAttributeType) throws -> ()) throws {
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA()
do { faAttributes = try windowsFileAttributes(atPath: srcPath) } catch { return }
let faAttributes = try windowsFileAttributes(atPath: srcPath)
Copy link
Member

Choose a reason for hiding this comment

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

How come we don't need the exception to propagate here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, if srcPath didn't exist, it would catch the error from windowsFileAttributes and _copyItem would silently not do anything. This way it bubbles up the exception and throws a file not found as it should.

@@ -372,7 +373,7 @@ extension FileManager {
let src = joinPath(prefix: srcPath, suffix: item)
let dst = joinPath(prefix: dstPath, suffix: item)

do { faAttributes = try windowsFileAttributes(atPath: src) } catch { return }
let faAttributes = try windowsFileAttributes(atPath: src)
Copy link
Member

Choose a reason for hiding this comment

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

And here

@compnerd compnerd requested a review from millenomi July 2, 2019 20:42
@millenomi millenomi merged commit 189a90d into swiftlang:master Jul 11, 2019
@gmittert gmittert deleted the VariousFMFixes branch September 9, 2019 18:12
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.

4 participants