-
-
Notifications
You must be signed in to change notification settings - Fork 315
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
[fix] change behaviour of FindFiles
to ignore errors
#217
base: master
Are you sure you want to change the base?
Conversation
@bitfield Do you have any thoughts on how we should test the case where there is an error but we get more than one path? |
That's a tricky one! What about creating a tempdir and then setting up files |
@bitfield I gave it a shot but I can't seem to produce that case. Changing the permissions of the file doesn't prevent the path from being listed. Let me know if you have any thoughts on how to proceed. |
Remember |
@bitfield added a test case - can you take another look? |
script_test.go
Outdated
if err := os.Chmod(restrictedDirPath, 0o000); err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { os.Chmod(restrictedDirPath, os.ModePerm) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tempdir will be deleted anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bitfield I need to do this so that this dir can get cleaned up. Otherwise, the cleanup function throws a permissions error. Let me know if you'd like to do this a different way.
TempDir RemoveAll cleanup: openfdat /var/folders/mn/31m4g_6j7qz26m7_00mgsbrc0000gn/T/TestFindFiles_DoesNotErrorWhenSubDirectoryIsNotReadable1455531535/001/restricted_dir: permission denied
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because you're also creating a file inside the restricted directory. So RemoveAll
isn't able to delete the file, because that requires modifying the directory, which you don't have permission to do.
You don't need this file for the test (and FindFiles
won't be able to even know it exists, because of the directory permissions). Without the file, you don't need the Cleanup
func, because the automatic RemoveAll
succeeds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bitfield If I don't add the file - then wouldn't the test pass regardless because FindFiles doesn't print directories? I was trying to show that there's a path inside the restricted dir that is not printed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, according to your test name, the test is about whether or not FindFiles
returns an error if it encounters one on its walk. If the error happens to be "can't list files in directory", then we hardly need to test that FindFIles
doesn't return those files: how could it? Testing the impossible doesn't catch bugs (testing assumptions, on the other hand, is an excellent idea and we should always do it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bitfield got it - thanks for the explanation! i removed the file for the test
This test covers the case where the induced error is on the last path that |
Ah okay I see. I added a check in |
Well, I don't think it matters what the error is. The point is that if we encounter any errors during the walk, we should only report them if we haven't also found some valid files. |
I see - should we then always return |
Well, there are only four possibilities:
So I don't think we actually have a choice to make here, unless there's another possibility you can think of? |
@bitfield Good point. But if we always run func TestFindFiles_InNonexistentPathReturnsError(t *testing.T) {
t.Parallel()
p := script.FindFiles("nonexistent_path")
if p.Error() == nil {
t.Fatal("want error for nonexistent path")
}
} |
Right. But the |
Signed-off-by: Mahad Zaryab <[email protected]>
@bitfield Addressed. Let me know what you think. |
Great! This code looks solid. Would you like to update the documentation a little to explain this (sensible, but non-obvious) error behaviour? |
filepath.Walk
with the newerfs.WalkDir
FindFiles
to only return an error if no paths were found