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 broken file and dir helpers on Windows #163

Merged
merged 4 commits into from
Aug 19, 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
105 changes: 102 additions & 3 deletions 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 @@ -983,8 +1006,13 @@ func FileExistsFS(system fs.FS, file string) (s string) {
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
}

// other errors - file probably exists but cannot be read
if info.IsDir() {
s = "expected file but is a directory\n"
s += bullet("name: %s\n", file)
Expand All @@ -993,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 @@ -1008,15 +1051,42 @@ 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)
return
}
// other errors - directory probably exists but cannot be read
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)
Expand All @@ -1025,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 @@ -1077,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
9 changes: 9 additions & 0 deletions invocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func (it *internalTest) assert() {
}
}

func (it *internalTest) assertNot() {
if !it.helper {
it.t.Fatal("should be marked as helper")
}
if it.trigger {
it.t.Fatalf("condition expected not to trigger; it did\ngot message %q in output", it.capture)
}
}

func (it *internalTest) post() {
if !strings.Contains(it.capture, "PostScript |") {
it.t.Fatal("expected post-script output")
Expand Down
9 changes: 9 additions & 0 deletions must/invocations_test.go

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

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.

Loading
Loading