Skip to content

Commit

Permalink
assertions: use os functions in non-FS file and dir helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck committed Aug 19, 2024
1 parent e3c82e1 commit a85e392
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 27 deletions.
91 changes: 90 additions & 1 deletion internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,29 @@ func MapNotContainsValueEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[V
})
}

func FileExists(file string) (s string) {
info, err := os.Stat(file)
if errors.Is(err, fs.ErrNotExist) {
s = "expected file to exist\n"
s += bullet(" name: %s\n", file)
s += bullet("error: %s\n", err)
return
}
if err != nil {
s = "got an unexpected error\n"
s += bullet("name: %s\n", file)
s += bullet("error: %s\n", err)
return
}

if info.IsDir() {
s = "expected file but is a directory\n"
s += bullet("name: %s\n", file)
return
}
return
}

func FileExistsFS(system fs.FS, file string) (s string) {
info, err := fs.Stat(system, file)
if errors.Is(err, fs.ErrNotExist) {
Expand All @@ -998,6 +1021,21 @@ func FileExistsFS(system fs.FS, file string) (s string) {
return
}

func FileNotExists(file string) (s string) {
_, err := os.Stat(file)
if err == nil {
s = "expected file to not exist\n"
s += bullet("name: %s\n", file)
return
}
if !errors.Is(err, fs.ErrNotExist) {
s = "expected not existing file but got different error\n"
s += bullet("error: %s\n", err)
return
}
return
}

func FileNotExistsFS(system fs.FS, file string) (s string) {
_, err := fs.Stat(system, file)
if err == nil {
Expand All @@ -1013,9 +1051,31 @@ func FileNotExistsFS(system fs.FS, file string) (s string) {
return
}

func DirExists(directory string) (s string) {
info, err := os.Stat(directory)
if errors.Is(err, fs.ErrNotExist) {
s = "expected directory to exist\n"
s += bullet(" name: %s\n", directory)
s += bullet("error: %s\n", err)
return
}
if err != nil {
s = "got an unexpected error\n"
s += bullet("name: %s\n", directory)
s += bullet("error: %s\n", err)
return
}
if !info.IsDir() {
s = "expected directory but is a file\n"
s += bullet("name: %s\n", directory)
return
}
return
}

func DirExistsFS(system fs.FS, directory string) (s string) {
info, err := fs.Stat(system, directory)
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
s = "expected directory to exist\n"
s += bullet(" name: %s\n", directory)
s += bullet("error: %s\n", err)
Expand All @@ -1035,6 +1095,16 @@ func DirExistsFS(system fs.FS, directory string) (s string) {
return
}

func DirNotExists(directory string) (s string) {
_, err := os.Stat(directory)
if !errors.Is(err, fs.ErrNotExist) {
s = "expected directory to not exist\n"
s += bullet("name: %s\n", directory)
return
}
return
}

func DirNotExistsFS(system fs.FS, directory string) (s string) {
_, err := fs.Stat(system, directory)
if !errors.Is(err, fs.ErrNotExist) {
Expand Down Expand Up @@ -1087,6 +1157,25 @@ func DirModeFS(system fs.FS, path string, permissions fs.FileMode) (s string) {
return
}

func FileContains(file, content string) (s string) {
b, err := os.ReadFile(file)
if err != nil {
s = "expected to read file\n"
s += bullet(" name: %s\n", file)
s += bullet("error: %s\n", err)
return
}
actual := string(b)
if !strings.Contains(string(b), content) {
s = "expected file contents\n"
s += bullet(" name: %s\n", file)
s += bullet("wanted: %s\n", content)
s += bullet("actual: %s\n", actual)
return
}
return
}

func FileContainsFS(system fs.FS, file, content string) (s string) {
b, err := fs.ReadFile(system, file)
if err != nil {
Expand Down
18 changes: 5 additions & 13 deletions must/must.go

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

18 changes: 5 additions & 13 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -547,9 +546,7 @@ func FileExistsFS(t T, system fs.FS, file string, settings ...Setting) {
// FileExists asserts file exists on the OS filesystem.
func FileExists(t T, file string, settings ...Setting) {
t.Helper()
dir := filepath.Dir(file)
file = filepath.Base(file)
invoke(t, assertions.FileExistsFS(os.DirFS(dir), file), settings...)
invoke(t, assertions.FileExists(file), settings...)
}

// FileNotExistsFS asserts file does not exist on the fs.FS filesystem.
Expand All @@ -564,9 +561,7 @@ func FileNotExistsFS(t T, system fs.FS, file string, settings ...Setting) {
// FileNotExists asserts file does not exist on the OS filesystem.
func FileNotExists(t T, file string, settings ...Setting) {
t.Helper()
dir := filepath.Dir(file)
file = filepath.Base(file)
invoke(t, assertions.FileNotExistsFS(os.DirFS(dir), file), settings...)
invoke(t, assertions.FileNotExists(file), settings...)
}

// DirExistsFS asserts directory exists on the fs.FS filesystem.
Expand All @@ -582,8 +577,7 @@ func DirExistsFS(t T, system fs.FS, directory string, settings ...Setting) {
// DirExists asserts directory exists on the OS filesystem.
func DirExists(t T, directory string, settings ...Setting) {
t.Helper()
directory = strings.TrimPrefix(directory, "/")
invoke(t, assertions.DirExistsFS(os.DirFS(brokenfs.Root), directory), settings...)
invoke(t, assertions.DirExists(directory), settings...)
}

// DirNotExistsFS asserts directory does not exist on the fs.FS filesystem.
Expand All @@ -598,8 +592,7 @@ func DirNotExistsFS(t T, system fs.FS, directory string, settings ...Setting) {
// DirNotExists asserts directory does not exist on the OS filesystem.
func DirNotExists(t T, directory string, settings ...Setting) {
t.Helper()
directory = strings.TrimPrefix(directory, "/")
invoke(t, assertions.DirNotExistsFS(os.DirFS(brokenfs.Root), directory), settings...)
invoke(t, assertions.DirNotExists(directory), settings...)
}

// FileModeFS asserts the file or directory at path on fs.FS has exactly the given permission bits.
Expand Down Expand Up @@ -647,8 +640,7 @@ func FileContainsFS(t T, system fs.FS, file, content string, settings ...Setting
// FileContains asserts the file on the OS filesystem contains content as a substring.
func FileContains(t T, file, content string, settings ...Setting) {
t.Helper()
file = strings.TrimPrefix(file, "/")
invoke(t, assertions.FileContainsFS(os.DirFS(brokenfs.Root), file, content), settings...)
invoke(t, assertions.FileContains(file, content), settings...)
}

// FilePathValid asserts path is a valid file path.
Expand Down

0 comments on commit a85e392

Please sign in to comment.