Debian Codegraph @

file_test.go

raw symbols SCIP
1 package utils
2
3 import (
4 "errors"
5 "fmt"
6 "io/fs"
7 "net/http"
8 "os"
9 "path/filepath"
10 "runtime"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14 )
15
16 // errFS wraps another http.FileSystem and fails opening a specific path.
17 type errFS struct {
18 http.FileSystem
19 fail string
20 }
21
22 func Test_ReadFile(t *testing.T) {
23 t.Parallel()
24
25 testFS := http.FS(os.DirFS(".github/tests"))
26 file, err := ReadFile("john.txt", testFS)
27
28 switch runtime.GOOS {
29 case "windows":
30 require.Equal(t, "doe\r\n", string(file))
31 default:
32 require.Equal(t, "doe\n", string(file))
33 }
34
35 require.NoError(t, err)
36 }
37
38 func Test_Walk(t *testing.T) {
39 t.Parallel()
40
41 type file struct {
42 path string
43 name string
44 isDir bool
45 }
46 var files []file
47
48 expectedResults := []file{
49 {
50 path: "example",
51 name: "example",
52 isDir: true,
53 },
54 {
55 path: "example/example1.txt",
56 name: "example1.txt",
57 isDir: false,
58 },
59 {
60 path: "john.txt",
61 name: "john.txt",
62 isDir: false,
63 },
64 }
65
66 testFS := http.FS(os.DirFS(".github/tests"))
67 err := Walk(testFS, ".", func(path string, info fs.FileInfo, _ error) error {
68 if path != "." {
69 files = append(files, file{
70 path: path,
71 name: info.Name(),
72 isDir: info.IsDir(),
73 })
74 }
75
76 return nil
77 })
78
79 require.NoError(t, err)
80 require.Equal(t, expectedResults, files)
81 }
82
83 func Test_Walk_Error(t *testing.T) {
84 t.Parallel()
85
86 testFS := http.FS(os.DirFS(".github/tests"))
87 err := Walk(testFS, "nonexistent", func(path string, _ fs.FileInfo, _ error) error {
88 return fmt.Errorf("file not found: %s", path)
89 })
90
91 require.Error(t, err)
92 }
93
94 func Test_ReadFile_Error(t *testing.T) {
95 t.Parallel()
96
97 // Test error when file does not exist
98 testFS := http.FS(os.DirFS(".github/tests"))
99 _, err := ReadFile("nonexistent.txt", testFS)
100 require.Error(t, err)
101
102 // Test error when file does not exist and fs is nil
103 _, err = ReadFile("nonexistent.txt", nil)
104 require.Error(t, err)
105 }
106
107 // Test for readDirNames success and error cases
108 func Test_readDirNames(t *testing.T) {
109 t.Parallel()
110
111 testFS := http.FS(os.DirFS(".github/tests"))
112
113 names, err := readDirNames(testFS, ".")
114 require.NoError(t, err)
115 require.Equal(t, []string{"example", "john.txt"}, names)
116
117 _, err = readDirNames(testFS, "does-not-exist")
118 require.Error(t, err)
119 }
120
121 // Test for readDir error handling and content retrieval
122 func Test_readDir(t *testing.T) {
123 t.Parallel()
124
125 testFS := http.FS(os.DirFS(".github/tests"))
126
127 fis, err := readDir(testFS, ".")
128 require.NoError(t, err)
129 require.Len(t, fis, 2)
130
131 _, err = readDir(testFS, "missing")
132 require.Error(t, err)
133 }
134
135 // Test walkInternal when walkFn returns SkipDir
136 func Test_walkInternal_SkipDir(t *testing.T) {
137 testFS := http.FS(os.DirFS(".github/tests"))
138 info, err := stat(testFS, ".")
139 require.NoError(t, err)
140
141 var visited []string
142 err = walkInternal(testFS, ".", info, func(path string, _ fs.FileInfo, _ error) error {
143 visited = append(visited, path)
144 return filepath.SkipDir
145 })
146 require.NoError(t, err)
147 require.Equal(t, []string{"."}, visited)
148 }
149
150 // Test walkInternal returning error from walkFn
151 func Test_walkInternal_Error(t *testing.T) {
152 testFS := http.FS(os.DirFS(".github/tests"))
153 info, err := stat(testFS, ".")
154 require.NoError(t, err)
155
156 testErr := errors.New("walk error")
157 err = walkInternal(testFS, ".", info, func(path string, _ fs.FileInfo, _ error) error {
158 if path != "." {
159 return testErr
160 }
161 return nil
162 })
163 require.ErrorIs(t, err, testErr)
164 }
165
166 func (e errFS) Open(name string) (http.File, error) {
167 if filepath.Clean(name) == e.fail {
168 return nil, os.ErrNotExist
169 }
170 return e.FileSystem.Open(name)
171 }
172
173 // Test walkInternal when stat returns error for a child and walkFn returns nil
174 func Test_walkInternal_StatError(t *testing.T) {
175 testFS := errFS{http.FS(os.DirFS(".github/tests")), filepath.Join("example", "example1.txt")}
176 info, err := stat(testFS, ".")
177 require.NoError(t, err)
178
179 err = walkInternal(testFS, ".", info, func(_ string, _ fs.FileInfo, _ error) error {
180 return nil
181 })
182 require.NoError(t, err)
183 }
184
185 // Test walkInternal when stat returns error and walkFn propagates error
186 func Test_walkInternal_StatErrorPropagate(t *testing.T) {
187 testFS := errFS{http.FS(os.DirFS(".github/tests")), filepath.Join("example", "example1.txt")}
188 info, err := stat(testFS, ".")
189 require.NoError(t, err)
190
191 testErr := errors.New("stat failed")
192 err = walkInternal(testFS, ".", info, func(_ string, _ fs.FileInfo, err error) error {
193 if err != nil {
194 return testErr
195 }
196 return nil
197 })
198 require.ErrorIs(t, err, testErr)
199 }
200
201 // Test reading a file directly from the OS when no filesystem is provided
202 func Test_ReadFile_NoFS(t *testing.T) {
203 t.Parallel()
204
205 data, err := ReadFile(".github/tests/john.txt", nil)
206 require.NoError(t, err)
207 require.Contains(t, string(data), "doe")
208 }
209
210 func Test_ReadFile_SubDir(t *testing.T) {
211 t.Parallel()
212
213 data, err := ReadFile("example/example1.txt", http.FS(os.DirFS(".github/tests")))
214 require.NoError(t, err)
215 require.NotNil(t, data)
216 }