Skip to content

Commit

Permalink
tests: make dir and file mode tests work on Windows
Browse files Browse the repository at this point in the history
assertions: use os funcs in non-FS mode helpers
  • Loading branch information
brandondyck authored and shoenig committed Aug 21, 2024
1 parent 2802fca commit 2e48df0
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 94 deletions.
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.

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

0 comments on commit 2e48df0

Please sign in to comment.