Skip to content

Use os.Stat to check for file instead of os.Open#338

Open
willbeason wants to merge 1 commit intogoogle:masterfrom
willbeason:use-stat-for-file-check
Open

Use os.Stat to check for file instead of os.Open#338
willbeason wants to merge 1 commit intogoogle:masterfrom
willbeason:use-stat-for-file-check

Conversation

@willbeason
Copy link
Copy Markdown

The code for save previously used os.Open to check if the directory to write to with save already exists. This commit changes to use os.Stat instead, which can perform the equivalent verification.

This is cleaner since we don't end up with a file we then need to close.

Added an E2E test to verify this works, but I'm happy to delete if this seems like overkill. I tried to mimic the style of the existing tests.

While writing this test I made some small improvements to the existing tests:

  1. Moved to using t.TempDir (which does automatic cleanup) for temporary directories instead of os.MkDirTemp.

  2. Replaced the direct casts to ExitError with the more modern errors.As.

The code for save previously used os.Open to check if the directory to
write to with save already exists. This commit changes to use os.Stat
instead, which can perform the equivalent verification.

This is cleaner since we don't end up with a file we then need to
close.

Added an E2E test to verify this works, but I'm happy to delete if
this seems like overkill. I tried to mimic the style of the existing
tests.

While writing this test I made some small improvements to the existing
tests:

1. Moved to using t.TempDir (which does automatic cleanup) for temporary
directories instead of os.MkDirTemp.

2. Replaced the direct casts to ExitError with the more modern
errors.As.
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.

1 participant