Skip to content

Commit

Permalink
Use fs.FileMode type for SetFileMode() params
Browse files Browse the repository at this point in the history
  • Loading branch information
djjuhasz authored and sevein committed May 25, 2024
1 parent c40175c commit 5fa59f1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
6 changes: 3 additions & 3 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func Move(src, dst string) error {

// SetFileModes recursively sets the file mode of directory root and its
// contents.
func SetFileModes(root string, dirMode, fileMode int) error {
func SetFileModes(root string, dirMode, fileMode fs.FileMode) error {
return filepath.WalkDir(root,
func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}

mode := fs.FileMode(fileMode)
mode := fileMode
if d.IsDir() {
mode = fs.FileMode(dirMode)
mode = dirMode
}

if err := os.Chmod(path, mode); err != nil {
Expand Down
52 changes: 33 additions & 19 deletions fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package fsutil_test

import (
"fmt"
"io/fs"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/fs"
tfs "gotest.tools/v3/fs"

"go.artefactual.dev/tools/fsutil"
)
Expand Down Expand Up @@ -80,26 +81,39 @@ func TestBaseNoExt(t *testing.T) {
}

func TestSetFileModes(t *testing.T) {
td := fs.NewDir(t, "enduro-test-fsutil",
fs.WithDir("transfer", fs.WithMode(0o755),
fs.WithFile("test1", "I'm a test file.", fs.WithMode(0o644)),
fs.WithDir("subdir", fs.WithMode(0o755),
fs.WithFile("test2", "Another test file.", fs.WithMode(0o644)),
t.Parallel()

t.Run("Sets file modes", func(t *testing.T) {
t.Parallel()

var (
initDirMode fs.FileMode = 0o755
initFileMode fs.FileMode = 0o644
wantDirMode fs.FileMode = 0o700
wantFileMode fs.FileMode = 0o600
)

td := tfs.NewDir(t, "enduro-test-fsutil",
tfs.WithDir("transfer", tfs.WithMode(initDirMode),
tfs.WithFile("test1", "I'm a test file.", tfs.WithMode(initFileMode)),
tfs.WithDir("subdir", tfs.WithMode(initDirMode),
tfs.WithFile("test2", "Another test file.", tfs.WithMode(initFileMode)),
),
),
),
)
)

err := fsutil.SetFileModes(td.Join("transfer"), 0o700, 0o600)
assert.NilError(t, err)
assert.Assert(t, fs.Equal(
td.Path(),
fs.Expected(t,
fs.WithDir("transfer", fs.WithMode(0o700),
fs.WithFile("test1", "I'm a test file.", fs.WithMode(0o600)),
fs.WithDir("subdir", fs.WithMode(0o700),
fs.WithFile("test2", "Another test file.", fs.WithMode(0o600)),
err := fsutil.SetFileModes(td.Join("transfer"), wantDirMode, wantFileMode)
assert.NilError(t, err)
assert.Assert(t, tfs.Equal(
td.Path(),
tfs.Expected(t,
tfs.WithDir("transfer", tfs.WithMode(wantDirMode),
tfs.WithFile("test1", "I'm a test file.", tfs.WithMode(wantFileMode)),
tfs.WithDir("subdir", tfs.WithMode(wantDirMode),
tfs.WithFile("test2", "Another test file.", tfs.WithMode(wantFileMode)),
),
),
),
),
))
))
})
}

0 comments on commit 5fa59f1

Please sign in to comment.