Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file/dir mode helpers and tests #166

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func (it *internalTest) Errorf(s string, args ...any) {
}
msg := strings.TrimSpace(fmt.Sprintf(s, args...))
it.capture = msg
fmt.Println(msg)
it.t.Log(msg)
}
42 changes: 42 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,25 @@ func DirNotExistsFS(system fs.FS, directory string) (s string) {
return
}

func FileMode(path string, permissions fs.FileMode) (s string) {
info, err := os.Stat(path)
if err != nil {
s = "expected to stat path\n"
s += bullet(" name: %s\n", path)
s += bullet("error: %s\n", err)
return
}

mode := info.Mode()
if permissions != mode {
s = "expected different file permissions\n"
s += bullet("name: %s\n", path)
s += bullet(" exp: %s\n", permissions)
s += bullet(" got: %s\n", mode)
}
return
}

func FileModeFS(system fs.FS, path string, permissions fs.FileMode) (s string) {
info, err := fs.Stat(system, path)
if err != nil {
Expand All @@ -1134,6 +1153,29 @@ func FileModeFS(system fs.FS, path string, permissions fs.FileMode) (s string) {
return
}

func DirMode(path string, permissions fs.FileMode) (s string) {
info, err := os.Stat(path)
if err != nil {
s = "expected to stat path\n"
s += bullet(" name: %s\n", path)
s += bullet("error: %s\n", err)
return
}
if !info.IsDir() {
s = "expected to stat a directory\n"
s += bullet("name: %s\n", path)
return
}
mode := info.Mode()
if permissions != mode {
s = "expected different file permissions\n"
s += bullet("name: %s\n", path)
s += bullet(" exp: %s\n", permissions)
s += bullet(" got: %s\n", mode)
}
return
}

func DirModeFS(system fs.FS, path string, permissions fs.FileMode) (s string) {
info, err := fs.Stat(system, path)
if err != nil {
Expand Down
10 changes: 0 additions & 10 deletions internal/brokenfs/fs_default.go

This file was deleted.

20 changes: 0 additions & 20 deletions internal/brokenfs/fs_windows.go

This file was deleted.

2 changes: 1 addition & 1 deletion must/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func (it *internalTest) Fatalf(s string, args ...any) {
}
msg := strings.TrimSpace(fmt.Sprintf(s, args...))
it.capture = msg
fmt.Println(msg)
it.t.Log(msg)
}
8 changes: 2 additions & 6 deletions must/must.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 119 additions & 26 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ package test
import (
"io"
"io/fs"
"os"
"regexp"
"strings"

"github.com/shoenig/test/interfaces"
"github.com/shoenig/test/internal/assertions"
"github.com/shoenig/test/internal/brokenfs"
"github.com/shoenig/test/internal/constraints"
"github.com/shoenig/test/internal/util"
"github.com/shoenig/test/wait"
Expand Down Expand Up @@ -607,8 +605,7 @@ func FileModeFS(t T, system fs.FS, path string, permissions fs.FileMode, setting
// FileMode asserts the file or directory at path on the OS filesystem has exactly the given permission bits.
func FileMode(t T, path string, permissions fs.FileMode, settings ...Setting) {
t.Helper()
path = strings.TrimPrefix(path, "/")
invoke(t, assertions.FileModeFS(os.DirFS(brokenfs.Root), path, permissions), settings...)
invoke(t, assertions.FileMode(path, permissions), settings...)
}

// DirModeFS asserts the directory at path on fs.FS has exactly the given permission bits.
Expand All @@ -623,8 +620,7 @@ func DirModeFS(t T, system fs.FS, path string, permissions fs.FileMode, 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...)
invoke(t, assertions.DirMode(path, permissions), settings...)
}

// FileContainsFS asserts the file on fs.FS contains content as a substring.
Expand Down
Loading
Loading