Skip to content

Commit

Permalink
add DirModeFS and DirMode funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini authored and shoenig committed Apr 1, 2024
1 parent 4b435aa commit 034bee1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,22 @@ func FileMode(t T, path string, permissions fs.FileMode, settings ...Setting) {
invoke(t, assertions.FileModeFS(os.DirFS(brokenfs.Root), path, permissions), settings...)
}

// DirModeFS asserts the directory at path on fs.FS has exactly the given permission bits.
//
// Example,
// DirModeFS(t, os.DirFS("/"), "bin", 0655)
func DirModeFS(t T, system fs.FS, path string, permissions fs.FileMode, settings ...Setting) {
t.Helper()
invoke(t, assertions.DirModeFS(system, path, permissions), settings...)
}

// DirMode asserts the directory at path on the OS filesystem has exactly the given permission bits.
func DirMode(t T, path string, permissions fs.FileMode, settings ...Setting) {
t.Helper()
path = strings.TrimPrefix(path, "/")
invoke(t, assertions.DirModeFS(os.DirFS(brokenfs.Root), path, permissions), settings...)
}

// FileContainsFS asserts the file on fs.FS contains content as a substring.
//
// Often os.DirFS is used to interact with the host filesystem.
Expand Down
38 changes: 38 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,44 @@ func TestFileMode(t *testing.T) {
FileMode(tc, "/bin/find", unexpected)
}

func TestDirModeFS(t *testing.T) {
needsOS(t, "linux")

t.Run("different permissions", func(t *testing.T) {
tc := newCase(t, `expected different file permissions`)
t.Cleanup(tc.assert)

var unexpected os.FileMode = 0755 // (actual 0755)
DirModeFS(tc, os.DirFS("/"), "bin", unexpected)
})

t.Run("not a dir", func(t *testing.T) {
tc := newCase(t, `expected to stat a directory`)
t.Cleanup(tc.assert)

DirModeFS(tc, os.DirFS("/bin"), "find", os.FileMode(0))
})
}

func TestDirMode(t *testing.T) {
needsOS(t, "linux")

t.Run("different permissions", func(t *testing.T) {
tc := newCase(t, `expected different file permissions`)
t.Cleanup(tc.assert)

var unexpected os.FileMode = 0755 // (actual 0755)
DirMode(tc, "/bin", unexpected)
})

t.Run("not a dir", func(t *testing.T) {
tc := newCase(t, `expected to stat a directory`)
t.Cleanup(tc.assert)

DirMode(tc, "/bin/find", os.FileMode(0))
})
}

func TestFileContainsFS(t *testing.T) {
needsOS(t, "linux")

Expand Down

0 comments on commit 034bee1

Please sign in to comment.