Fix broken file and dir helpers on Windows #163
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
File and directory helpers don't work properly on Windows, and a couple of them even panic. This PR fixes a few underlying problems:
os.DirFS
does not handle Windows paths properly, andbrokenfs
isn't doing the trick to fix it. When those functions validate names, they choke on the colon after the drive letter and returnfs.ErrInvalid
. Perhaps this can be avoided by not providing a drive letter (that seems to be a solution given in os: hard to use DirFS for cross-platform root filesystem golang/go#44279) but that's inconvenient and would probably only work if the file/directory is on the same drive as the working directory. I fixed this by adding having the non-FS helpers use new assertions that callos
functions instead offs
ones.Some file and directory assertions only check for
ErrNotExist
and then assume thatStat
succeeded. In the best case, this misses an uncaughtErrInvalid
and gives a misleading result; in the worst case, as inFileExistsFS
andDirExistsFS
, it leads to a panic from a nil dereference. I prevented the panics by checking for other errors as well before dereferencing theStat
result.There are no tests in the test suite that make sure an assertion wasn't triggered, and some of the tests that do exist get skipped on Windows. I updated the tests to check both major paths, and I made most of the tests Windows-compatible by using
t.TempDir()
instead of standard Unix paths. I didn't do this to theFileMode
andDirMode
tests because I don't know how Unix permissions work on Windows.