Skip to content

Commit 8366036

Browse files
committed
Add Chmod and expand a test
1 parent 99a3246 commit 8366036

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

paths.go

+7
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,13 @@ func (p *Path) CopyDirTo(dst *Path) error {
418418
return nil
419419
}
420420

421+
// Chmod changes the mode of the named file to mode. If the file is a
422+
// symbolic link, it changes the mode of the link's target. If there
423+
// is an error, it will be of type *os.PathError.
424+
func (p *Path) Chmod(mode fs.FileMode) error {
425+
return os.Chmod(p.path, mode)
426+
}
427+
421428
// Chtimes changes the access and modification times of the named file,
422429
// similar to the Unix utime() or utimes() functions.
423430
func (p *Path) Chtimes(atime, mtime time.Time) error {

readdir_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ package paths
3131

3232
import (
3333
"fmt"
34+
"io/fs"
3435
"os"
36+
"runtime"
3537
"testing"
3638
"time"
3739

@@ -317,4 +319,25 @@ func TestReadDirRecursiveLoopDetection(t *testing.T) {
317319
pathEqualsTo(t, "testdata/loops/regular_3/dir2/file2", l[5])
318320
pathEqualsTo(t, "testdata/loops/regular_3/link", l[6]) // broken symlink is reported in files
319321
}
322+
323+
if runtime.GOOS != "windows" {
324+
dir1 := loopsPath.Join("regular_4_with_permission_error", "dir1")
325+
326+
l, err := unbuondedReaddir("regular_4_with_permission_error")
327+
require.NoError(t, err)
328+
require.NotEmpty(t, l)
329+
330+
dir1Stat, err := dir1.Stat()
331+
require.NoError(t, err)
332+
err = dir1.Chmod(fs.FileMode(0)) // Enforce permission error
333+
require.NoError(t, err)
334+
t.Cleanup(func() {
335+
// Restore normal permission after the test
336+
dir1.Chmod(dir1Stat.Mode())
337+
})
338+
339+
l, err = unbuondedReaddir("regular_4_with_permission_error")
340+
require.Error(t, err)
341+
require.Nil(t, l)
342+
}
320343
}

testdata/loops/regular_4_with_permission_error/dir1/file1

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
broken

0 commit comments

Comments
 (0)