|
| 1 | +package iofs |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/fs" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "testing/fstest" |
| 11 | + |
| 12 | + billyfs "github.com/go-git/go-billy/v5" |
| 13 | + "github.com/go-git/go-billy/v5/memfs" |
| 14 | +) |
| 15 | + |
| 16 | +type errorList interface { |
| 17 | + Unwrap() []error |
| 18 | +} |
| 19 | + |
| 20 | +type wrappedError interface { |
| 21 | + Unwrap() error |
| 22 | +} |
| 23 | + |
| 24 | +// TestWithFSTest leverages the packaged Go fstest package, which seems comprehensive. |
| 25 | +func TestWithFSTest(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + memfs := memfs.New() |
| 28 | + iofs := New(memfs) |
| 29 | + |
| 30 | + files := map[string]string{ |
| 31 | + "foo.txt": "hello, world", |
| 32 | + "bar.txt": "goodbye, world", |
| 33 | + filepath.Join("dir", "baz.txt"): "こんにちわ, world", |
| 34 | + } |
| 35 | + created_files := make([]string, 0, len(files)) |
| 36 | + for filename, contents := range files { |
| 37 | + makeFile(memfs, t, filename, contents) |
| 38 | + created_files = append(created_files, filename) |
| 39 | + } |
| 40 | + |
| 41 | + if runtime.GOOS == "windows" { |
| 42 | + t.Skip("fstest.TestFS is not yet windows path aware") |
| 43 | + } |
| 44 | + |
| 45 | + err := fstest.TestFS(iofs, created_files...) |
| 46 | + if err != nil { |
| 47 | + checkFsTestError(t, err, files) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestDeletes(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + memfs := memfs.New() |
| 54 | + iofs := New(memfs).(fs.ReadFileFS) |
| 55 | + |
| 56 | + makeFile(memfs, t, "foo.txt", "hello, world") |
| 57 | + makeFile(memfs, t, "deleted", "nothing to see") |
| 58 | + |
| 59 | + if _, err := iofs.ReadFile("nonexistent"); err == nil { |
| 60 | + t.Errorf("expected error for nonexistent file") |
| 61 | + } |
| 62 | + |
| 63 | + data, err := iofs.ReadFile("deleted") |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("failed to read file before delete: %v", err) |
| 66 | + } |
| 67 | + if string(data) != "nothing to see" { |
| 68 | + t.Errorf("unexpected contents before delete: %v", data) |
| 69 | + } |
| 70 | + |
| 71 | + if err := memfs.Remove("deleted"); err != nil { |
| 72 | + t.Fatalf("failed to remove file: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + if _, err = iofs.ReadFile("deleted"); err == nil { |
| 76 | + t.Errorf("file existed after delete!") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func makeFile(fs billyfs.Basic, t *testing.T, filename string, contents string) { |
| 81 | + t.Helper() |
| 82 | + file, err := fs.Create(filename) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("failed to create file %s: %v", filename, err) |
| 85 | + } |
| 86 | + defer file.Close() |
| 87 | + _, err = file.Write([]byte(contents)) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("failed to write to file %s: %v", filename, err) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func checkFsTestError(t *testing.T, err error, files map[string]string) { |
| 94 | + t.Helper() |
| 95 | + |
| 96 | + if unwrapped := errors.Unwrap(err); unwrapped != nil { |
| 97 | + err = unwrapped |
| 98 | + } |
| 99 | + |
| 100 | + // Go >= 1.23 (after https://cs.opensource.google/go/go/+/74cce866f865c3188a34309e4ebc7a5c9ed0683d) |
| 101 | + // has nicely-Joined wrapped errors. Try that first. |
| 102 | + if errs, ok := err.(errorList); ok { |
| 103 | + for _, e := range errs.Unwrap() { |
| 104 | + |
| 105 | + if strings.Contains(e.Error(), "ModTime") { |
| 106 | + // Memfs returns the current time for Stat().ModTime(), which triggers |
| 107 | + // a diff complaint in fstest. We can ignore this, or store modtimes |
| 108 | + // for every file in Memfs (at a cost of 16 bytes / file). |
| 109 | + t.Log("Skipping ModTime error (ok).") |
| 110 | + } else { |
| 111 | + t.Errorf("Unexpected fstest error: %v", e) |
| 112 | + } |
| 113 | + } |
| 114 | + } else { |
| 115 | + if runtime.Version() >= "go1.23" { |
| 116 | + t.Fatalf("Failed to test fs:\n%v", err) |
| 117 | + } |
| 118 | + // filter lines from the error text corresponding to the above errors; |
| 119 | + // output looks like: |
| 120 | + // TestFS found errors: |
| 121 | + // bar.txt: mismatch: |
| 122 | + // entry.Info() = bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.377023639 +0000 UTC m=+0.002625548 |
| 123 | + // file.Stat() = bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.376907011 +0000 UTC m=+0.002508970 |
| 124 | + // |
| 125 | + // bar.txt: fs.Stat(...) = bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.381356651 +0000 UTC m=+0.006959191 |
| 126 | + // want bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.376907011 +0000 UTC m=+0.002508970 |
| 127 | + // bar.txt: fsys.Stat(...) = bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.381488617 +0000 UTC m=+0.007090346 |
| 128 | + // want bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.376907011 +0000 UTC m=+0.002508970 |
| 129 | + // We filter on "empty line" or "ModTime" or "$filename: mismatch" to ignore these. |
| 130 | + lines := strings.Split(err.Error(), "\n") |
| 131 | + filtered := make([]string, 0, len(lines)) |
| 132 | + filename_mismatches := make(map[string]struct{}, len(files)*2) |
| 133 | + for name := range files { |
| 134 | + for dirname := name; dirname != "."; dirname = filepath.Dir(dirname) { |
| 135 | + filename_mismatches[dirname+": mismatch:"] = struct{}{} |
| 136 | + } |
| 137 | + } |
| 138 | + if strings.TrimSpace(lines[0]) == "TestFS found errors:" { |
| 139 | + lines = lines[1:] |
| 140 | + } |
| 141 | + for _, line := range lines { |
| 142 | + trimmed := strings.TrimSpace(line) |
| 143 | + if trimmed == "" || strings.Contains(trimmed, "ModTime=") { |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + if _, ok := filename_mismatches[trimmed]; ok { |
| 148 | + continue |
| 149 | + } |
| 150 | + filtered = append(filtered, line) |
| 151 | + } |
| 152 | + if len(filtered) > 0 { |
| 153 | + t.Fatalf("Failed to test fs:\n%s", strings.Join(filtered, "\n")) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments