Debian Codegraph @

util/walk_test.go

raw symbols SCIP
1 package util_test
2
3 import (
4 "errors"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "github.com/go-git/go-billy/v6"
10 "github.com/go-git/go-billy/v6/memfs"
11 "github.com/go-git/go-billy/v6/util"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 )
15
16 var targetSubfolder = filepath.FromSlash("path/to/some/subfolder")
17
18 func TestWalkCanSkipTopDirectory(t *testing.T) {
19 filesystem := memfs.New()
20 err := util.Walk(filesystem, "/root/that/does/not/exist", func(_ string, _ os.FileInfo, _ error) error { return filepath.SkipDir })
21
22 require.NoError(t, err)
23 }
24
25 func TestWalkReturnsAnErrorWhenRootDoesNotExist(t *testing.T) {
26 filesystem := memfs.New()
27 err := util.Walk(filesystem, "/root/that/does/not/exist", func(_ string, _ os.FileInfo, err error) error { return err })
28
29 require.Error(t, err)
30 }
31
32 func TestWalkOnPlainFile(t *testing.T) {
33 filesystem := memfs.New()
34 createFile(t, filesystem, "./README.md")
35 discoveredPaths := []string{}
36
37 err := util.Walk(filesystem, "./README.md", func(path string, _ os.FileInfo, _ error) error {
38 discoveredPaths = append(discoveredPaths, path)
39 return nil
40 })
41 require.NoError(t, err)
42 assert.Equal(t, []string{"./README.md"}, discoveredPaths)
43 }
44
45 func TestWalkOnExistingFolder(t *testing.T) {
46 filesystem := memfs.New()
47 createFile(t, filesystem, "path/to/some/subfolder/that/contain/file")
48 createFile(t, filesystem, "path/to/some/file")
49 discoveredPaths := []string{}
50 err := util.Walk(filesystem, "path", func(path string, _ os.FileInfo, _ error) error {
51 discoveredPaths = append(discoveredPaths, path)
52 return nil
53 })
54 require.NoError(t, err)
55
56 assert.Contains(t, discoveredPaths, "path")
57 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to"))
58 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some"))
59 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/file"))
60 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder"))
61 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that"))
62 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain"))
63 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain/file"))
64 }
65
66 func TestWalkCanSkipFolder(t *testing.T) {
67 filesystem := memfs.New()
68 createFile(t, filesystem, "path/to/some/subfolder/that/contain/file")
69 createFile(t, filesystem, "path/to/some/file")
70 discoveredPaths := []string{}
71 err := util.Walk(filesystem, "path", func(path string, _ os.FileInfo, _ error) error {
72 discoveredPaths = append(discoveredPaths, path)
73 if path == targetSubfolder {
74 return filepath.SkipDir
75 }
76 return nil
77 })
78 require.NoError(t, err)
79 assert.Contains(t, discoveredPaths, "path")
80 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to"))
81 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some"))
82 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/file"))
83 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder"))
84 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that"))
85 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain"))
86 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain/file"))
87 }
88
89 func TestWalkStopsOnError(t *testing.T) {
90 filesystem := memfs.New()
91 createFile(t, filesystem, "path/to/some/subfolder/that/contain/file")
92 createFile(t, filesystem, "path/to/some/file")
93 discoveredPaths := []string{}
94 err := util.Walk(filesystem, "path", func(path string, _ os.FileInfo, _ error) error {
95 discoveredPaths = append(discoveredPaths, path)
96 if path == targetSubfolder {
97 return errors.New("uncaught error")
98 }
99 return nil
100 })
101 require.Error(t, err)
102 assert.Contains(t, discoveredPaths, "path")
103 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to"))
104 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some"))
105 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/file"))
106 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder"))
107 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that"))
108 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain"))
109 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain/file"))
110 }
111
112 func TestWalkForwardsStatErrors(t *testing.T) {
113 memFilesystem := memfs.New()
114 filesystem := &fnFs{
115 Filesystem: memFilesystem,
116 lstat: func(path string) (os.FileInfo, error) {
117 if path == targetSubfolder {
118 return nil, errors.New("uncaught error")
119 }
120 return memFilesystem.Lstat(path)
121 },
122 }
123
124 createFile(t, filesystem, "path/to/some/subfolder/that/contain/file")
125 createFile(t, filesystem, "path/to/some/file")
126 discoveredPaths := []string{}
127 err := util.Walk(filesystem, "path", func(path string, _ os.FileInfo, err error) error {
128 discoveredPaths = append(discoveredPaths, path)
129 if path == targetSubfolder {
130 require.Error(t, err)
131 }
132 return err
133 })
134 require.Error(t, err)
135 assert.Contains(t, discoveredPaths, "path")
136 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to"))
137 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some"))
138 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/file"))
139 assert.Contains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder"))
140 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that"))
141 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain"))
142 assert.NotContains(t, discoveredPaths, filepath.FromSlash("path/to/some/subfolder/that/contain/file"))
143 }
144
145 func createFile(t *testing.T, filesystem billy.Filesystem, path string) {
146 t.Helper()
147 fd, err := filesystem.Create(path)
148
149 require.NoError(t, err)
150 fd.Close()
151 }
152
153 type fnFs struct {
154 billy.Filesystem
155 lstat func(path string) (os.FileInfo, error)
156 }
157
158 func (f *fnFs) Lstat(path string) (os.FileInfo, error) {
159 if f.lstat != nil {
160 return f.lstat(path)
161 }
162 return nil, errors.New("not implemented")
163 }