Skip to content

Commit

Permalink
Add Chmod and expand a test
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Jan 15, 2024
1 parent 99a3246 commit 8366036
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,13 @@ func (p *Path) CopyDirTo(dst *Path) error {
return nil
}

// Chmod changes the mode of the named file to mode. If the file is a
// symbolic link, it changes the mode of the link's target. If there
// is an error, it will be of type *os.PathError.
func (p *Path) Chmod(mode fs.FileMode) error {
return os.Chmod(p.path, mode)
}

// Chtimes changes the access and modification times of the named file,
// similar to the Unix utime() or utimes() functions.
func (p *Path) Chtimes(atime, mtime time.Time) error {
Expand Down
23 changes: 23 additions & 0 deletions readdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ package paths

import (
"fmt"
"io/fs"
"os"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -317,4 +319,25 @@ func TestReadDirRecursiveLoopDetection(t *testing.T) {
pathEqualsTo(t, "testdata/loops/regular_3/dir2/file2", l[5])
pathEqualsTo(t, "testdata/loops/regular_3/link", l[6]) // broken symlink is reported in files
}

if runtime.GOOS != "windows" {
dir1 := loopsPath.Join("regular_4_with_permission_error", "dir1")

l, err := unbuondedReaddir("regular_4_with_permission_error")
require.NoError(t, err)
require.NotEmpty(t, l)

dir1Stat, err := dir1.Stat()
require.NoError(t, err)
err = dir1.Chmod(fs.FileMode(0)) // Enforce permission error
require.NoError(t, err)
t.Cleanup(func() {
// Restore normal permission after the test
dir1.Chmod(dir1Stat.Mode())
})

l, err = unbuondedReaddir("regular_4_with_permission_error")
require.Error(t, err)
require.Nil(t, l)
}
}
Empty file.
1 change: 1 addition & 0 deletions testdata/loops/regular_4_with_permission_error/link

0 comments on commit 8366036

Please sign in to comment.