Skip to content

Commit

Permalink
examples: use fstest.MapFS for FS examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored and shoenig committed Sep 10, 2024
1 parent bff98a4 commit a0dbb8c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ There are five key packages,
### Changes
:ballot_box_with_check: v1.11.0 adds an ErrorAs helper

- FS examples are more reliable

:ballot_box_with_check: v1.10.0 adds a `util` package for helpers that return values

- Adds ability to create and automatically clean up temporary files
Expand Down
32 changes: 23 additions & 9 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"regexp"
"strconv"
"strings"
"testing/fstest"
"time"

"github.com/shoenig/test/wait"
Expand Down Expand Up @@ -231,7 +232,10 @@ func ExampleDirExists() {
}

func ExampleDirExistsFS() {
DirExistsFS(t, os.DirFS("/"), "tmp")
fsys := fstest.MapFS{
"foo": &fstest.MapFile{Mode: fs.ModeDir},
}
DirExistsFS(t, fsys, "foo")
// Output:
}

Expand All @@ -241,7 +245,8 @@ func ExampleDirNotExists() {
}

func ExampleDirNotExistsFS() {
DirNotExistsFS(t, os.DirFS("/"), "does/not/exist")
fsys := fstest.MapFS{}
DirNotExistsFS(t, fsys, "does/not/exist")
// Output:
}

Expand Down Expand Up @@ -341,8 +346,12 @@ func ExampleFileContains() {
}

func ExampleFileContainsFS() {
_ = os.WriteFile("/tmp/example", []byte("foo bar baz"), fs.FileMode(0600))
FileContainsFS(t, os.DirFS("/tmp"), "example", "bar")
fsys := fstest.MapFS{
"example": &fstest.MapFile{
Data: []byte("foo bar baz"),
},
}
FileContainsFS(t, fsys, "example", "bar")
// Output:
}

Expand All @@ -353,8 +362,10 @@ func ExampleFileExists() {
}

func ExampleFileExistsFS() {
_ = os.WriteFile("/tmp/example", []byte{}, fs.FileMode(0600))
FileExistsFS(t, os.DirFS("/tmp"), "example")
fsys := fstest.MapFS{
"example": &fstest.MapFile{},
}
FileExistsFS(t, fsys, "example")
// Output:
}

Expand All @@ -365,8 +376,10 @@ func ExampleFileMode() {
}

func ExampleFileModeFS() {
_ = os.WriteFile("/tmp/example_fm", []byte{}, fs.FileMode(0600))
FileModeFS(t, os.DirFS("/tmp"), "example_fm", fs.FileMode(0600))
fsys := fstest.MapFS{
"example": &fstest.MapFile{Mode: 0600},
}
FileModeFS(t, fsys, "example", fs.FileMode(0600))
// Output:
}

Expand All @@ -376,7 +389,8 @@ func ExampleFileNotExists() {
}

func ExampleFileNotExistsFS() {
FileNotExistsFS(t, os.DirFS("/tmp"), "not_existing_file")
fsys := fstest.MapFS{}
FileNotExistsFS(t, fsys, "not_existing_file")
// Output:
}

Expand Down
32 changes: 23 additions & 9 deletions must/examples_test.go

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

0 comments on commit a0dbb8c

Please sign in to comment.