From 18f1f04e632b6ca6d8c169d02860f5c482595927 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 18 Aug 2024 00:44:40 -0600 Subject: [PATCH] assertions: use os functions in non-FS file and dir helpers --- internal/assertions/assertions.go | 91 ++++++++++++++++++++++++++++++- must/must.go | 18 ++---- test.go | 18 ++---- 3 files changed, 100 insertions(+), 27 deletions(-) diff --git a/internal/assertions/assertions.go b/internal/assertions/assertions.go index a2b5096..727c24b 100644 --- a/internal/assertions/assertions.go +++ b/internal/assertions/assertions.go @@ -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) { @@ -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 { @@ -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) @@ -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) { @@ -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 { diff --git a/must/must.go b/must/must.go index 619ba81..eb35397 100644 --- a/must/must.go +++ b/must/must.go @@ -9,7 +9,6 @@ import ( "io" "io/fs" "os" - "path/filepath" "regexp" "strings" @@ -549,9 +548,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. @@ -566,9 +563,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. @@ -584,8 +579,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. @@ -600,8 +594,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. @@ -649,8 +642,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. diff --git a/test.go b/test.go index 73ef6ae..20effc9 100644 --- a/test.go +++ b/test.go @@ -7,7 +7,6 @@ import ( "io" "io/fs" "os" - "path/filepath" "regexp" "strings" @@ -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. @@ -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. @@ -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. @@ -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. @@ -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.