diff --git a/must/must.go b/must/must.go index 7fbcbfc..42e504b 100644 --- a/must/must.go +++ b/must/must.go @@ -617,6 +617,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. diff --git a/must/must_test.go b/must/must_test.go index dbabe38..399bd79 100644 --- a/must/must_test.go +++ b/must/must_test.go @@ -1439,6 +1439,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")