From a0dbb8c4ade88655da077f3a125b909438be4f3a Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 00:09:43 -0600 Subject: [PATCH] examples: use fstest.MapFS for FS examples --- README.md | 2 ++ examples_test.go | 32 +++++++++++++++++++++++--------- must/examples_test.go | 32 +++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 72810e6..f581155 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples_test.go b/examples_test.go index 32a2d91..bf873e9 100644 --- a/examples_test.go +++ b/examples_test.go @@ -14,6 +14,7 @@ import ( "regexp" "strconv" "strings" + "testing/fstest" "time" "github.com/shoenig/test/wait" @@ -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: } @@ -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: } @@ -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: } @@ -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: } @@ -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: } @@ -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: } diff --git a/must/examples_test.go b/must/examples_test.go index b23238c..ad7bc55 100644 --- a/must/examples_test.go +++ b/must/examples_test.go @@ -16,6 +16,7 @@ import ( "regexp" "strconv" "strings" + "testing/fstest" "time" "github.com/shoenig/test/wait" @@ -233,7 +234,10 @@ func ExampleDirExists() { } func ExampleDirExistsFS() { - DirExistsFS(t, os.DirFS("/"), "tmp") + fsys := fstest.MapFS{ + "foo": &fstest.MapFile{Mode: fs.ModeDir}, + } + DirExistsFS(t, fsys, "foo") // Output: } @@ -243,7 +247,8 @@ func ExampleDirNotExists() { } func ExampleDirNotExistsFS() { - DirNotExistsFS(t, os.DirFS("/"), "does/not/exist") + fsys := fstest.MapFS{} + DirNotExistsFS(t, fsys, "does/not/exist") // Output: } @@ -343,8 +348,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: } @@ -355,8 +364,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: } @@ -367,8 +378,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: } @@ -378,7 +391,8 @@ func ExampleFileNotExists() { } func ExampleFileNotExistsFS() { - FileNotExistsFS(t, os.DirFS("/tmp"), "not_existing_file") + fsys := fstest.MapFS{} + FileNotExistsFS(t, fsys, "not_existing_file") // Output: }