Skip to content

Commit fa20e7a

Browse files
committed
Skip Directory If Permission Error
1 parent 9f2b623 commit fa20e7a

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

script.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ func FindFiles(dir string) *Pipe {
9595
var paths []string
9696
err := fs.WalkDir(os.DirFS(dir), ".", func(path string, d fs.DirEntry, err error) error {
9797
if err != nil {
98+
if os.IsPermission(err) {
99+
return fs.SkipDir
100+
}
98101
return err
99102
}
100103
if !d.IsDir() {

script_unix_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ func TestExecPipesDataToExternalCommandAndGetsExpectedOutput(t *testing.T) {
111111
func TestFindFiles_DoesNotErrorWhenSubDirectoryIsNotReadable(t *testing.T) {
112112
t.Parallel()
113113
tmpDir := t.TempDir()
114-
fileAPath := filepath.Join(tmpDir, "file_a.txt")
115-
if err := os.WriteFile(fileAPath, []byte("hello world!"), os.ModePerm); err != nil {
114+
restrictedDirPath := filepath.Join(tmpDir, "a_restricted_dir")
115+
if err := os.Mkdir(restrictedDirPath, os.ModePerm); err != nil {
116116
t.Fatal(err)
117117
}
118-
restrictedDirPath := filepath.Join(tmpDir, "restricted_dir")
119-
if err := os.Mkdir(restrictedDirPath, os.ModePerm); err != nil {
118+
fileAPath := filepath.Join(restrictedDirPath, "file_a.txt")
119+
if err := os.WriteFile(fileAPath, []byte("hello again!"), os.ModePerm); err != nil {
120120
t.Fatal(err)
121121
}
122-
fileBPath := filepath.Join(restrictedDirPath, "file_b.txt")
123-
if err := os.WriteFile(fileBPath, []byte("hello again!"), os.ModePerm); err != nil {
122+
fileBPath := filepath.Join(tmpDir, "file_b.txt")
123+
if err := os.WriteFile(fileBPath, []byte("hello world!"), os.ModePerm); err != nil {
124124
t.Fatal(err)
125125
}
126126
if err := os.Chmod(restrictedDirPath, 0o000); err != nil {
@@ -131,7 +131,7 @@ func TestFindFiles_DoesNotErrorWhenSubDirectoryIsNotReadable(t *testing.T) {
131131
if err != nil {
132132
t.Fatal(err)
133133
}
134-
want := fileAPath + "\n"
134+
want := fileBPath + "\n"
135135
if err != nil {
136136
t.Fatal(err)
137137
}

0 commit comments

Comments
 (0)