| 1 | package iofsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 5 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 6 | "io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/" |
| 7 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 8 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 10 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 11 | "testing/fstestscip-go gomod github.com/golang/go/src go1.25.0 `testing/fstest`/" |
| 12 | |
| 13 | billyfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/ "github.com/go-git/go-billy/v6scip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/" |
| 14 | "github.com/go-git/go-billy/v6/memfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/memfs`/" |
| 15 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/" |
| 16 | ) |
| 17 | |
| 18 | type errorListscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/errorList# interface { |
| 19 | Unwrapscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/errorList#Unwrap.() []error |
| 20 | } |
| 21 | |
| 22 | // TestWithFSTest leverages the packaged Go fstest package, which seems comprehensive. |
| 23 | func TestWithFSTestscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/TestWithFSTest().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 24 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 25 | memfs := memfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/memfs`/.Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/memfs`/New().() |
| 26 | iofs := Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/New().(memfs) |
| 27 | |
| 28 | files := map[string]string{ |
| 29 | "foo.txt": "hello, world", |
| 30 | "bar.txt": "goodbye, world", |
| 31 | "dir/baz.txt": "こんにちわ, world", |
| 32 | } |
| 33 | createdFiles := make([]string, 0, len(files)) |
| 34 | for filename, contents := range files { |
| 35 | makeFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/makeFile().(t, memfs, filename, contents) |
| 36 | createdFiles = append(createdFiles, filename) |
| 37 | } |
| 38 | |
| 39 | err := fstestscip-go gomod github.com/golang/go/src go1.25.0 `testing/fstest`/.TestFSscip-go gomod github.com/golang/go/src go1.25.0 `testing/fstest`/TestFS().(iofs, createdFiles...) |
| 40 | if err != nil { |
| 41 | checkFsTestErrorscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/checkFsTestError().(t, err, files) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // TestOpenForwardSlashPath verifies that Open works with forward-slash paths |
| 46 | // on all platforms. This is a regression test ensuring filepath.Clean is not |
| 47 | // used when cleaning paths in Open() (it should use path.Clean) so valid |
| 48 | // fs.FS paths like "dir/file.txt" are not rejected on Windows. |
| 49 | func TestOpenForwardSlashPathscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/TestOpenForwardSlashPath().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 50 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 51 | mem := memfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/memfs`/.Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/memfs`/New().() |
| 52 | adapter := Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/New().(mem) |
| 53 | |
| 54 | makeFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/makeFile().(t, mem, "dir/subdir/file.txt", "content") |
| 55 | |
| 56 | tests := []struct { |
| 57 | name string |
| 58 | path string |
| 59 | wantErr bool |
| 60 | }{ |
| 61 | {"simple-nested", "dir/subdir/file.txt", false}, |
| 62 | {"directory", "dir/subdir", false}, |
| 63 | {"dot-path", ".", false}, |
| 64 | {"absolute-path-rejected", "/dir/subdir/file.txt", true}, |
| 65 | } |
| 66 | |
| 67 | for _, tt := range tests { |
| 68 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 69 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 70 | f, err := adapter.Openscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FS#Open().(tt.path) |
| 71 | if tt.wantErr { |
| 72 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Errorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/Error().(t, err) |
| 73 | return |
| 74 | } |
| 75 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 76 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Implementsscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/Implements().(t, (*ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReaderAtscip-go gomod github.com/golang/go/src go1.25.0 io/ReaderAt#)(nil), f) |
| 77 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Implementsscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/Implements().(t, (*ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Seekerscip-go gomod github.com/golang/go/src go1.25.0 io/Seeker#)(nil), f) |
| 78 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, f.Closescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/File#Close().()) |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestDeletesscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/TestDeletes().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 84 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 85 | memfs := memfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/memfs`/.Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/memfs`/New().() |
| 86 | iofs, ok := Newscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/New().(memfs).(fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ReadFileFSscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFileFS#) |
| 87 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Truescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/True().(t, ok) |
| 88 | |
| 89 | makeFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/makeFile().(t, memfs, "foo.txt", "hello, world") |
| 90 | makeFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/makeFile().(t, memfs, "deleted", "nothing to see") |
| 91 | |
| 92 | if _, err := iofs.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFileFS#ReadFile().("nonexistent"); err == nil { |
| 93 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected error for nonexistent file") |
| 94 | } |
| 95 | |
| 96 | data, err := iofs.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFileFS#ReadFile().("deleted") |
| 97 | if err != nil { |
| 98 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to read file before delete: %v", err) |
| 99 | } |
| 100 | if string(data) != "nothing to see" { |
| 101 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected contents before delete: %v", data) |
| 102 | } |
| 103 | |
| 104 | if err := memfs.Removescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Basic#Remove.("deleted"); err != nil { |
| 105 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to remove file: %v", err) |
| 106 | } |
| 107 | |
| 108 | if _, err = iofs.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFileFS#ReadFile().("deleted"); err == nil { |
| 109 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("file existed after delete!") |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func makeFilescip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/makeFile().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, fs billyfsscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/.Basicscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Basic#, filename string, contents string) { |
| 114 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 115 | file, err := fs.Createscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6`/Basic#Create.(filename) |
| 116 | if err != nil { |
| 117 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to create file %s: %v", filename, err) |
| 118 | } |
| 119 | defer file.Closescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/File#Close().() |
| 120 | _, err = file.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().([]byte(contents)) |
| 121 | if err != nil { |
| 122 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to write to file %s: %v", filename, err) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func checkFsTestErrorscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/checkFsTestError().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, err error, files map[string]string) { |
| 127 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 128 | |
| 129 | if unwrapped := errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Unwrapscip-go gomod github.com/golang/go/src go1.25.0 errors/Unwrap().(err); unwrapped != nil { |
| 130 | err = unwrapped |
| 131 | } |
| 132 | |
| 133 | // Go >= 1.23 (after https://cs.opensource.google/go/go/+/74cce866f865c3188a34309e4ebc7a5c9ed0683d) |
| 134 | // has nicely-Joined wrapped errors. Try that first. |
| 135 | if errs, ok := err.(errorListscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/errorList#); ok { |
| 136 | for _, e := range errs.Unwrapscip-go gomod github.com/go-git/go-billy/v6 . `github.com/go-git/go-billy/v6/helper/iofs`/errorList#Unwrap.() { |
| 137 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(e.Error(), "ModTime") { |
| 138 | // Memfs returns the current time for Stat().ModTime(), which triggers |
| 139 | // a diff complaint in fstest. We can ignore this, or store modtimes |
| 140 | // for every file in Memfs (at a cost of 16 bytes / file). |
| 141 | t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().("Skipping ModTime error (ok).") |
| 142 | } else { |
| 143 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Unexpected fstest error: %v", e) |
| 144 | } |
| 145 | } |
| 146 | } else { |
| 147 | if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Versionscip-go gomod github.com/golang/go/src go1.25.0 runtime/Version().() >= "go1.23" { |
| 148 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to test fs:\n%v", err) |
| 149 | } |
| 150 | // filter lines from the error text corresponding to the above errors; |
| 151 | // output looks like: |
| 152 | // TestFS found errors: |
| 153 | // bar.txt: mismatch: |
| 154 | // 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 |
| 155 | // 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 |
| 156 | // |
| 157 | // 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 |
| 158 | // want bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.376907011 +0000 UTC m=+0.002508970 |
| 159 | // 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 |
| 160 | // want bar.txt IsDir=false Mode=-rw-rw-rw- Size=14 ModTime=2024-09-17 10:09:00.376907011 +0000 UTC m=+0.002508970 |
| 161 | // We filter on "empty line" or "ModTime" or "$filename: mismatch" to ignore these. |
| 162 | lines := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(err.Error(), "\n") |
| 163 | filtered := make([]string, 0, len(lines)) |
| 164 | mismatches := make(map[string]struct{}, len(files)*2) |
| 165 | for name := range files { |
| 166 | for dirname := name; dirname != "."; dirname = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(dirname) { |
| 167 | mismatches[dirname+": mismatch:"] = struct{}{} |
| 168 | } |
| 169 | } |
| 170 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(lines[0]) == "TestFS found errors:" { |
| 171 | lines = lines[1:] |
| 172 | } |
| 173 | for _, line := range lines { |
| 174 | trimmed := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(line) |
| 175 | if trimmed == "" || stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(trimmed, "ModTime=") { |
| 176 | continue |
| 177 | } |
| 178 | |
| 179 | if _, ok := mismatches[trimmed]; ok { |
| 180 | continue |
| 181 | } |
| 182 | filtered = append(filtered, line) |
| 183 | } |
| 184 | if len(filtered) > 0 { |
| 185 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to test fs:\n%s", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(filtered, "\n")) |
| 186 | } |
| 187 | } |
| 188 | } |