| 1 | package util_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/go-git/go-billy/v5" |
| 12 | "github.com/go-git/go-billy/v5/memfs" |
| 13 | "github.com/go-git/go-billy/v5/util" |
| 14 | |
| 15 | . "gopkg.in/check.v1" |
| 16 | ) |
| 17 | |
| 18 | type WalkSuite struct{} |
| 19 | |
| 20 | func TestWalk(t *testing.T) { TestingT(t) } |
| 21 | |
| 22 | var _ = Suite(&WalkSuite{}) |
| 23 | var targetSubfolder = filepath.FromSlash("path/to/some/subfolder") |
| 24 | |
| 25 | func (s *WalkSuite) TestWalkCanSkipTopDirectory(c *C) { |
| 26 | filesystem := memfs.New() |
| 27 | c.Assert(util.Walk(filesystem, "/root/that/does/not/exist", func(path string, info os.FileInfo, err error) error { return filepath.SkipDir }), IsNil) |
| 28 | } |
| 29 | |
| 30 | func (s *WalkSuite) TestWalkReturnsAnErrorWhenRootDoesNotExist(c *C) { |
| 31 | filesystem := memfs.New() |
| 32 | c.Assert(util.Walk(filesystem, "/root/that/does/not/exist", func(path string, info os.FileInfo, err error) error { return err }), NotNil) |
| 33 | } |
| 34 | |
| 35 | func (s *WalkSuite) TestWalkOnPlainFile(c *C) { |
| 36 | filesystem := memfs.New() |
| 37 | createFile(c, filesystem, "./README.md") |
| 38 | discoveredPaths := []string{} |
| 39 | c.Assert(util.Walk(filesystem, "./README.md", func(path string, info os.FileInfo, err error) error { |
| 40 | discoveredPaths = append(discoveredPaths, path) |
| 41 | return nil |
| 42 | }), IsNil) |
| 43 | c.Assert(discoveredPaths, DeepEquals, []string{"./README.md"}) |
| 44 | } |
| 45 | |
| 46 | func (s *WalkSuite) TestWalkOnExistingFolder(c *C) { |
| 47 | filesystem := memfs.New() |
| 48 | createFile(c, filesystem, "path/to/some/subfolder/that/contain/file") |
| 49 | createFile(c, filesystem, "path/to/some/file") |
| 50 | discoveredPaths := []string{} |
| 51 | c.Assert(util.Walk(filesystem, "path", func(path string, info os.FileInfo, err error) error { |
| 52 | discoveredPaths = append(discoveredPaths, path) |
| 53 | return nil |
| 54 | }), IsNil) |
| 55 | c.Assert(discoveredPaths, Contains, "path") |
| 56 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to")) |
| 57 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some")) |
| 58 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/file")) |
| 59 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/subfolder")) |
| 60 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/subfolder/that")) |
| 61 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/subfolder/that/contain")) |
| 62 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/subfolder/that/contain/file")) |
| 63 | } |
| 64 | |
| 65 | func (s *WalkSuite) TestWalkCanSkipFolder(c *C) { |
| 66 | filesystem := memfs.New() |
| 67 | createFile(c, filesystem, "path/to/some/subfolder/that/contain/file") |
| 68 | createFile(c, filesystem, "path/to/some/file") |
| 69 | discoveredPaths := []string{} |
| 70 | c.Assert(util.Walk(filesystem, "path", func(path string, info os.FileInfo, err error) error { |
| 71 | discoveredPaths = append(discoveredPaths, path) |
| 72 | if path == targetSubfolder { |
| 73 | return filepath.SkipDir |
| 74 | } |
| 75 | return nil |
| 76 | }), IsNil) |
| 77 | c.Assert(discoveredPaths, Contains, "path") |
| 78 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to")) |
| 79 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some")) |
| 80 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/file")) |
| 81 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/subfolder")) |
| 82 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that")) |
| 83 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that/contain")) |
| 84 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that/contain/file")) |
| 85 | } |
| 86 | |
| 87 | func (s *WalkSuite) TestWalkStopsOnError(c *C) { |
| 88 | filesystem := memfs.New() |
| 89 | createFile(c, filesystem, "path/to/some/subfolder/that/contain/file") |
| 90 | createFile(c, filesystem, "path/to/some/file") |
| 91 | discoveredPaths := []string{} |
| 92 | c.Assert(util.Walk(filesystem, "path", func(path string, info os.FileInfo, err error) error { |
| 93 | discoveredPaths = append(discoveredPaths, path) |
| 94 | if path == targetSubfolder { |
| 95 | return errors.New("uncaught error") |
| 96 | } |
| 97 | return nil |
| 98 | }), NotNil) |
| 99 | c.Assert(discoveredPaths, Contains, "path") |
| 100 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to")) |
| 101 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some")) |
| 102 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/file")) |
| 103 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/subfolder")) |
| 104 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that")) |
| 105 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that/contain")) |
| 106 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that/contain/file")) |
| 107 | } |
| 108 | |
| 109 | func (s *WalkSuite) TestWalkForwardsStatErrors(c *C) { |
| 110 | memFilesystem := memfs.New() |
| 111 | filesystem := &fnFs{ |
| 112 | Filesystem: memFilesystem, |
| 113 | lstat: func(path string) (os.FileInfo, error) { |
| 114 | if path == targetSubfolder { |
| 115 | return nil, errors.New("uncaught error") |
| 116 | } |
| 117 | return memFilesystem.Lstat(path) |
| 118 | }, |
| 119 | } |
| 120 | |
| 121 | createFile(c, filesystem, "path/to/some/subfolder/that/contain/file") |
| 122 | createFile(c, filesystem, "path/to/some/file") |
| 123 | discoveredPaths := []string{} |
| 124 | c.Assert(util.Walk(filesystem, "path", func(path string, info os.FileInfo, err error) error { |
| 125 | discoveredPaths = append(discoveredPaths, path) |
| 126 | if path == targetSubfolder { |
| 127 | c.Assert(err, NotNil) |
| 128 | } |
| 129 | return err |
| 130 | }), NotNil) |
| 131 | c.Assert(discoveredPaths, Contains, "path") |
| 132 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to")) |
| 133 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some")) |
| 134 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/file")) |
| 135 | c.Assert(discoveredPaths, Contains, filepath.FromSlash("path/to/some/subfolder")) |
| 136 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that")) |
| 137 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that/contain")) |
| 138 | c.Assert(discoveredPaths, NotContain, filepath.FromSlash("path/to/some/subfolder/that/contain/file")) |
| 139 | } |
| 140 | |
| 141 | func createFile(c *C, filesystem billy.Filesystem, path string) { |
| 142 | fd, err := filesystem.Create(path) |
| 143 | c.Assert(err, IsNil) |
| 144 | if err != nil { |
| 145 | fd.Close() |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | type fnFs struct { |
| 150 | billy.Filesystem |
| 151 | lstat func(path string) (os.FileInfo, error) |
| 152 | } |
| 153 | |
| 154 | func (f *fnFs) Lstat(path string) (os.FileInfo, error) { |
| 155 | if f.lstat != nil { |
| 156 | return f.lstat(path) |
| 157 | } |
| 158 | return nil, errors.New("not implemented") |
| 159 | } |
| 160 | |
| 161 | type containsChecker struct { |
| 162 | *CheckerInfo |
| 163 | } |
| 164 | |
| 165 | func (checker *containsChecker) Check(params []interface{}, names []string) (result bool, err string) { |
| 166 | defer func() { |
| 167 | if v := recover(); v != nil { |
| 168 | result = false |
| 169 | err = fmt.Sprint(v) |
| 170 | } |
| 171 | }() |
| 172 | |
| 173 | value := reflect.ValueOf(params[0]) |
| 174 | result = false |
| 175 | err = fmt.Sprintf("%v does not contain %v", params[0], params[1]) |
| 176 | switch value.Kind() { |
| 177 | case reflect.Array, reflect.Slice: |
| 178 | for i := 0; i < value.Len(); i++ { |
| 179 | r := reflect.DeepEqual(value.Index(i).Interface(), params[1]) |
| 180 | if r { |
| 181 | result = true |
| 182 | err = "" |
| 183 | } |
| 184 | } |
| 185 | default: |
| 186 | return false, "obtained value type is not iterable" |
| 187 | } |
| 188 | return |
| 189 | } |
| 190 | |
| 191 | var Contains Checker = &containsChecker{ |
| 192 | &CheckerInfo{Name: "Contains", Params: []string{"obtained", "expected"}}, |
| 193 | } |
| 194 | |
| 195 | var NotContain Checker = Not(Contains) |