Skip to content

Commit 28365c1

Browse files
committed
Add Unit Test For Case When There Is An Error
1 parent 9511d86 commit 28365c1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

script_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,39 @@ func TestFindFiles_InNonexistentPathReturnsError(t *testing.T) {
12861286
}
12871287
}
12881288

1289+
func TestFindFiles_DoesNotErrorWhenSubDirectoryIsNotReadable(t *testing.T) {
1290+
t.Parallel()
1291+
tmpDir := t.TempDir()
1292+
fileAPath := filepath.Join(tmpDir, "file_a.txt")
1293+
if err := os.WriteFile(fileAPath, []byte("hello world!"), os.ModePerm); err != nil {
1294+
t.Fatal(err)
1295+
}
1296+
restrictedDirPath := filepath.Join(tmpDir, "restricted_dir")
1297+
if err := os.Mkdir(restrictedDirPath, os.ModePerm); err != nil {
1298+
t.Fatal(err)
1299+
}
1300+
fileBPath := filepath.Join(restrictedDirPath, "file_b.txt")
1301+
if err := os.WriteFile(fileBPath, []byte("hello again!"), os.ModePerm); err != nil {
1302+
t.Fatal(err)
1303+
}
1304+
if err := os.Chmod(restrictedDirPath, 0o000); err != nil {
1305+
t.Fatal(err)
1306+
}
1307+
t.Cleanup(func() { os.Chmod(restrictedDirPath, os.ModePerm) })
1308+
p := script.FindFiles(tmpDir)
1309+
if p.Error() != nil {
1310+
t.Fatal(p.Error())
1311+
}
1312+
want := fileAPath + "\n"
1313+
got, err := p.String()
1314+
if err != nil {
1315+
t.Fatal(err)
1316+
}
1317+
if !cmp.Equal(want, got) {
1318+
t.Fatal(cmp.Diff(want, got))
1319+
}
1320+
}
1321+
12891322
func TestIfExists_ProducesErrorPlusNoOutputForNonexistentFile(t *testing.T) {
12901323
t.Parallel()
12911324
want := ""

0 commit comments

Comments
 (0)