| 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "os/user" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "sort" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | ) |
| 14 | |
| 15 | func TestCleanPathsCleansPaths(t *testing.T) { |
| 16 | cleaned := CleanPaths("/foo/bar/,/foo/bar/baz", ",") |
| 17 | assert.Equal(t, []string{"/foo/bar", "/foo/bar/baz"}, cleaned) |
| 18 | } |
| 19 | |
| 20 | func TestCleanPathsReturnsNoResultsWhenGivenNoPaths(t *testing.T) { |
| 21 | cleaned := CleanPaths("", ",") |
| 22 | assert.Empty(t, cleaned) |
| 23 | } |
| 24 | |
| 25 | type ExpandPathTestCase struct { |
| 26 | Path string |
| 27 | Expand bool |
| 28 | |
| 29 | Want string |
| 30 | WantErr string |
| 31 | |
| 32 | currentUser func() (*user.User, error) |
| 33 | lookupUser func(who string) (*user.User, error) |
| 34 | } |
| 35 | |
| 36 | func (c *ExpandPathTestCase) Assert(t *testing.T) { |
| 37 | if c.currentUser != nil { |
| 38 | oldCurrentUser := currentUser |
| 39 | currentUser = c.currentUser |
| 40 | defer func() { currentUser = oldCurrentUser }() |
| 41 | } |
| 42 | |
| 43 | if c.lookupUser != nil { |
| 44 | oldLookupUser := lookupUser |
| 45 | lookupUser = c.lookupUser |
| 46 | defer func() { lookupUser = oldLookupUser }() |
| 47 | } |
| 48 | |
| 49 | got, err := ExpandPath(c.Path, c.Expand) |
| 50 | if err != nil || len(c.WantErr) > 0 { |
| 51 | assert.EqualError(t, err, c.WantErr) |
| 52 | } |
| 53 | assert.Equal(t, filepath.ToSlash(c.Want), filepath.ToSlash(got)) |
| 54 | } |
| 55 | |
| 56 | func TestExpandPath(t *testing.T) { |
| 57 | for desc, c := range map[string]*ExpandPathTestCase{ |
| 58 | "no expand": { |
| 59 | Path: "/path/to/hooks", |
| 60 | Want: "/path/to/hooks", |
| 61 | }, |
| 62 | "current": { |
| 63 | Path: "~/path/to/hooks", |
| 64 | Want: "/home/jane/path/to/hooks", |
| 65 | currentUser: func() (*user.User, error) { |
| 66 | return &user.User{ |
| 67 | HomeDir: "/home/jane", |
| 68 | }, nil |
| 69 | }, |
| 70 | }, |
| 71 | "current, slash": { |
| 72 | Path: "~/", |
| 73 | Want: "/home/jane", |
| 74 | currentUser: func() (*user.User, error) { |
| 75 | return &user.User{ |
| 76 | HomeDir: "/home/jane", |
| 77 | }, nil |
| 78 | }, |
| 79 | }, |
| 80 | "current, no slash": { |
| 81 | Path: "~", |
| 82 | Want: "/home/jane", |
| 83 | currentUser: func() (*user.User, error) { |
| 84 | return &user.User{ |
| 85 | HomeDir: "/home/jane", |
| 86 | }, nil |
| 87 | }, |
| 88 | }, |
| 89 | "non-current": { |
| 90 | Path: "~other/path/to/hooks", |
| 91 | Want: "/home/special/path/to/hooks", |
| 92 | lookupUser: func(who string) (*user.User, error) { |
| 93 | assert.Equal(t, "other", who) |
| 94 | return &user.User{ |
| 95 | HomeDir: "/home/special", |
| 96 | }, nil |
| 97 | }, |
| 98 | }, |
| 99 | "non-current, no slash": { |
| 100 | Path: "~other", |
| 101 | Want: "/home/special", |
| 102 | lookupUser: func(who string) (*user.User, error) { |
| 103 | assert.Equal(t, "other", who) |
| 104 | return &user.User{ |
| 105 | HomeDir: "/home/special", |
| 106 | }, nil |
| 107 | }, |
| 108 | }, |
| 109 | "non-current (missing)": { |
| 110 | Path: "~other/path/to/hooks", |
| 111 | WantErr: "could not find user other: missing", |
| 112 | lookupUser: func(who string) (*user.User, error) { |
| 113 | assert.Equal(t, "other", who) |
| 114 | return nil, fmt.Errorf("missing") |
| 115 | }, |
| 116 | }, |
| 117 | } { |
| 118 | t.Run(desc, c.Assert) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | type ExpandConfigPathTestCase struct { |
| 123 | Path string |
| 124 | DefaultPath string |
| 125 | |
| 126 | Want string |
| 127 | WantErr string |
| 128 | |
| 129 | currentUser func() (*user.User, error) |
| 130 | lookupConfigHome func() string |
| 131 | } |
| 132 | |
| 133 | func (c *ExpandConfigPathTestCase) Assert(t *testing.T) { |
| 134 | if c.currentUser != nil { |
| 135 | oldCurrentUser := currentUser |
| 136 | currentUser = c.currentUser |
| 137 | defer func() { currentUser = oldCurrentUser }() |
| 138 | } |
| 139 | |
| 140 | if c.lookupConfigHome != nil { |
| 141 | oldLookupConfigHome := lookupConfigHome |
| 142 | lookupConfigHome = c.lookupConfigHome |
| 143 | defer func() { lookupConfigHome = oldLookupConfigHome }() |
| 144 | } |
| 145 | |
| 146 | got, err := ExpandConfigPath(c.Path, c.DefaultPath) |
| 147 | if err != nil || len(c.WantErr) > 0 { |
| 148 | assert.EqualError(t, err, c.WantErr) |
| 149 | } |
| 150 | assert.Equal(t, filepath.ToSlash(c.Want), filepath.ToSlash(got)) |
| 151 | } |
| 152 | |
| 153 | func TestExpandConfigPath(t *testing.T) { |
| 154 | os.Unsetenv("XDG_CONFIG_HOME") |
| 155 | for desc, c := range map[string]*ExpandConfigPathTestCase{ |
| 156 | "unexpanded full path": { |
| 157 | Path: "/path/to/attributes", |
| 158 | Want: "/path/to/attributes", |
| 159 | }, |
| 160 | "expanded full path": { |
| 161 | Path: "~/path/to/attributes", |
| 162 | Want: "/home/pat/path/to/attributes", |
| 163 | currentUser: func() (*user.User, error) { |
| 164 | return &user.User{ |
| 165 | HomeDir: "/home/pat", |
| 166 | }, nil |
| 167 | }, |
| 168 | }, |
| 169 | "expanded default path": { |
| 170 | DefaultPath: "git/attributes", |
| 171 | Want: "/home/pat/.config/git/attributes", |
| 172 | currentUser: func() (*user.User, error) { |
| 173 | return &user.User{ |
| 174 | HomeDir: "/home/pat", |
| 175 | }, nil |
| 176 | }, |
| 177 | }, |
| 178 | "XDG_CONFIG_HOME set": { |
| 179 | DefaultPath: "git/attributes", |
| 180 | Want: "/home/pat/configpath/git/attributes", |
| 181 | lookupConfigHome: func() string { |
| 182 | return "/home/pat/configpath" |
| 183 | }, |
| 184 | }, |
| 185 | } { |
| 186 | t.Run(desc, c.Assert) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestFastWalkBasic(t *testing.T) { |
| 191 | wd, err := os.Getwd() |
| 192 | assert.NoError(t, err) |
| 193 | |
| 194 | rootDir := t.TempDir() |
| 195 | os.Chdir(rootDir) |
| 196 | defer os.Chdir(wd) |
| 197 | |
| 198 | expectedEntries := createFastWalkInputData(10, 160) |
| 199 | |
| 200 | walker := fastWalkDir(expectedEntries[0]) |
| 201 | gotEntries, gotErrors := collectFastWalkResults(walker.ch) |
| 202 | |
| 203 | assert.Empty(t, gotErrors) |
| 204 | |
| 205 | sort.Strings(expectedEntries) |
| 206 | sort.Strings(gotEntries) |
| 207 | assert.Equal(t, expectedEntries, gotEntries) |
| 208 | } |
| 209 | |
| 210 | // Make test data - ensure you've Chdir'ed into a temp dir first |
| 211 | // Returns list of files/dirs that are created |
| 212 | // First entry is the parent dir of all others |
| 213 | func createFastWalkInputData(smallFolder, largeFolder int) []string { |
| 214 | dirs := []string{ |
| 215 | "testroot", |
| 216 | "testroot/folder1", |
| 217 | "testroot/folder2", |
| 218 | "testroot/folder2/subfolder1", |
| 219 | "testroot/folder2/subfolder2", |
| 220 | "testroot/folder2/subfolder3", |
| 221 | "testroot/folder2/subfolder4", |
| 222 | "testroot/folder2/subfolder4/subsub", |
| 223 | } |
| 224 | expectedEntries := make([]string, 0, 250) |
| 225 | |
| 226 | for i, dir := range dirs { |
| 227 | os.MkdirAll(dir, 0755) |
| 228 | numFiles := smallFolder |
| 229 | expectedEntries = append(expectedEntries, dir) |
| 230 | if i >= 3 && i <= 5 { |
| 231 | // Bulk test to ensure works with > 1 batch |
| 232 | numFiles = largeFolder |
| 233 | } |
| 234 | for f := 0; f < numFiles; f++ { |
| 235 | filename := join(dir, fmt.Sprintf("file%d.txt", f)) |
| 236 | os.WriteFile(filename, []byte("TEST"), 0644) |
| 237 | expectedEntries = append(expectedEntries, filename) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return expectedEntries |
| 242 | } |
| 243 | |
| 244 | func collectFastWalkResults(fchan <-chan fastWalkInfo) ([]string, []error) { |
| 245 | gotEntries := make([]string, 0, 1000) |
| 246 | gotErrors := make([]error, 0, 5) |
| 247 | for o := range fchan { |
| 248 | if o.Err != nil { |
| 249 | gotErrors = append(gotErrors, o.Err) |
| 250 | } else { |
| 251 | if len(o.ParentDir) == 0 { |
| 252 | gotEntries = append(gotEntries, o.Info.Name()) |
| 253 | } else { |
| 254 | gotEntries = append(gotEntries, join(o.ParentDir, o.Info.Name())) |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return gotEntries, gotErrors |
| 260 | } |
| 261 | |
| 262 | func getFileMode(filename string) os.FileMode { |
| 263 | s, err := os.Stat(filename) |
| 264 | if err != nil { |
| 265 | return 0000 |
| 266 | } |
| 267 | return s.Mode() |
| 268 | } |
| 269 | |
| 270 | // uniq creates an element-wise copy of "xs" containing only unique elements in |
| 271 | // the same order. |
| 272 | func uniq(xs []string) []string { |
| 273 | seen := make(map[string]struct{}) |
| 274 | uniq := make([]string, 0, len(xs)) |
| 275 | |
| 276 | for _, x := range xs { |
| 277 | if _, ok := seen[x]; !ok { |
| 278 | seen[x] = struct{}{} |
| 279 | uniq = append(uniq, x) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return uniq |
| 284 | } |
| 285 | |
| 286 | func TestSetWriteFlag(t *testing.T) { |
| 287 | f, err := os.CreateTemp("", "lfstestwriteflag") |
| 288 | assert.Nil(t, err) |
| 289 | filename := f.Name() |
| 290 | defer os.Remove(filename) |
| 291 | f.Close() |
| 292 | // Set up with read/write bit for all but no execute |
| 293 | assert.Nil(t, os.Chmod(filename, 0666)) |
| 294 | |
| 295 | assert.Nil(t, SetFileWriteFlag(filename, false)) |
| 296 | // should turn off all write |
| 297 | assert.EqualValues(t, 0444, getFileMode(filename)) |
| 298 | assert.Nil(t, SetFileWriteFlag(filename, true)) |
| 299 | // should only add back user write (on Mac/Linux) |
| 300 | if runtime.GOOS == "windows" { |
| 301 | assert.EqualValues(t, 0666, getFileMode(filename)) |
| 302 | } else { |
| 303 | assert.EqualValues(t, 0644, getFileMode(filename)) |
| 304 | } |
| 305 | |
| 306 | // Can't run selective UGO tests on Windows as doesn't support separate roles |
| 307 | // Also Golang only supports read/write but not execute on Windows |
| 308 | if runtime.GOOS != "windows" { |
| 309 | // Set up with read/write/execute bit for all but no execute |
| 310 | assert.Nil(t, os.Chmod(filename, 0777)) |
| 311 | assert.Nil(t, SetFileWriteFlag(filename, false)) |
| 312 | // should turn off all write but not execute |
| 313 | assert.EqualValues(t, 0555, getFileMode(filename)) |
| 314 | assert.Nil(t, SetFileWriteFlag(filename, true)) |
| 315 | // should only add back user write (on Mac/Linux) |
| 316 | if runtime.GOOS == "windows" { |
| 317 | assert.EqualValues(t, 0777, getFileMode(filename)) |
| 318 | } else { |
| 319 | assert.EqualValues(t, 0755, getFileMode(filename)) |
| 320 | } |
| 321 | |
| 322 | assert.Nil(t, os.Chmod(filename, 0440)) |
| 323 | assert.Nil(t, SetFileWriteFlag(filename, false)) |
| 324 | assert.EqualValues(t, 0440, getFileMode(filename)) |
| 325 | assert.Nil(t, SetFileWriteFlag(filename, true)) |
| 326 | // should only add back user write |
| 327 | assert.EqualValues(t, 0640, getFileMode(filename)) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | func TestExecutablePermissions(t *testing.T) { |
| 332 | assert.EqualValues(t, os.FileMode(0755), ExecutablePermissions(0644)) |
| 333 | assert.EqualValues(t, os.FileMode(0750), ExecutablePermissions(0640)) |
| 334 | assert.EqualValues(t, os.FileMode(0700), ExecutablePermissions(0600)) |
| 335 | } |