Debian Codegraph @

txtar/fs_test.go

raw symbols SCIP
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package txtar_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar_test`/
6
7 import (
8 "io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/"
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 "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/"
14 )
15
16 func TestFSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar_test`/TestFS().(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#) {
17 var fstestcases = []struct {
18 name, input, files string
19 }{
20 {
21 name: "empty",
22 input: ``,
23 files: "",
24 },
25 {
26 name: "one",
27 input: `
28 -- one.txt --
29 one
30 `,
31 files: "one.txt",
32 },
33 {
34 name: "two",
35 input: `
36 -- one.txt --
37 one
38 -- two.txt --
39 two
40 `,
41 files: "one.txt two.txt",
42 },
43 {
44 name: "subdirectories",
45 input: `
46 -- one.txt --
47 one
48 -- 2/two.txt --
49 two
50 -- 2/3/three.txt --
51 three
52 -- 4/four.txt --
53 four
54 `,
55 files: "one.txt 2/two.txt 2/3/three.txt 4/four.txt",
56 },
57 }
58
59 for _, tc := range fstestcases {
60 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tc.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#) {
61 a := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(tc.input))
62 fsys, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.FSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/FS().(a)
63 if err != nil {
64 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
65 }
66
67 files := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.25.0 strings/Fields().(tc.files)
68 if 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().(fsys, files...); err != nil {
69 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
70 }
71
72 for _, f := range a.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files. {
73 b, err := fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFile().(fsys, f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name.)
74 if err != nil {
75 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ReadFile(%q) failed with error: %v", f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name., err)
76 }
77 if got, want := string(b), string(f.Datascip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Data.); got != want {
78 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ReadFile(%q) = %q; want %q", f.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name., got, want)
79 }
80 }
81 })
82 }
83 }
84
85 func TestInvalidscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar_test`/TestInvalid().(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#) {
86 invalidtestcases := []struct {
87 name, want string
88 input string
89 }{
90 {"unclean file names", "invalid path", `
91 -- 1/../one.txt --
92 one
93 -- 2/sub/../two.txt --
94 two
95 `},
96 {"duplicate name", `cannot create fs.FS from txtar.Archive: duplicate path "1/2/one.txt"`, `
97 -- 1/2/one.txt --
98 one
99 -- 1/2/one.txt --
100 two
101 `},
102 {"file conflicts with directory", `duplicate path "1/2"`, `
103 -- 1/2 --
104 one
105 -- 1/2/one.txt --
106 two
107 `},
108 }
109
110 for _, tc := range invalidtestcases {
111 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tc.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#) {
112 a := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(tc.input))
113 _, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.FSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/FS().(a)
114 if err == nil {
115 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("txtar.FS(...) succeeded; expected an error")
116 }
117 if got := err.Error(); !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().(got, tc.want) || tc.want == "" {
118 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("txtar.FS(...) got error %q; want %q", got, tc.want)
119 }
120 })
121 }
122 }
123
124 func TestModifiedscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar_test`/TestModified().(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#) {
125 const input = `
126 -- one.txt --
127 one
128 `
129 for _, mod := range []func(a *txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#){
130 func(a *txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#) { a.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.[0].Datascip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Data. = []byte("other") },
131 func(a *txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#) { a.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.[0].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name. = "other" },
132 func(a *txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Archivescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#) { a.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files. = nil },
133 } {
134 a := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(input))
135 if n := len(a.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files.); n != 1 {
136 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("txtar.Parse(%q) got %d files; expected 1", input, n)
137 }
138
139 fsys, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.FSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/FS().(a)
140 if err != nil {
141 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
142 }
143
144 // Confirm we can open "one.txt".
145 _, err = fsys.Openscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FS#Open().("one.txt")
146 if err != nil {
147 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
148 }
149 // Modify a to get ErrModified when opening "one.txt".
150 mod(a)
151
152 _, err = fsys.Openscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FS#Open().("one.txt")
153 if err != txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.ErrModifiedscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/ErrModified. {
154 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Open(%q) got error %s; want ErrModified", "one.txt", err)
155 }
156 }
157 }
158
159 func TestReadFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar_test`/TestReadFile().(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#) {
160 const input = `
161 -- 1/one.txt --
162 one
163 `
164 a := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(input))
165 fsys, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.FSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/FS().(a)
166 if err != nil {
167 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
168 }
169 readfs := fsys.(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#)
170 _, err = readfs.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFileFS#ReadFile().("1")
171 if err == nil {
172 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ReadFile(%q) succeeded; expected an error when reading a directory", "1")
173 }
174
175 content, err := readfs.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/ReadFileFS#ReadFile().("1/one.txt")
176 if err != nil {
177 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
178 }
179 want := "one\n"
180 if got := string(content); want != got {
181 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ReadFile(%q) = %q; want %q", "1/one.txt", got, want)
182 }
183 }