diff --git a/find/find_test/find_csv_test.go b/find/find_test/find_csv_test.go index ca7f3ba..63da5d3 100644 --- a/find/find_test/find_csv_test.go +++ b/find/find_test/find_csv_test.go @@ -11,7 +11,7 @@ var csvCases = []testutil.TestCase{ Name: "find matches from csv file", Want: []string{ "LICENSE.txt", - "backup/documents/.hidden_old_resume.txt", + "backup/documents/.hidden_resume.txt", "projects/project1/index.html", "projects/project2/index.html", "videos/funny_cats (3).mp4", diff --git a/find/find_test/find_test.go b/find/find_test/find_test.go index 9011471..29ac24c 100644 --- a/find/find_test/find_test.go +++ b/find/find_test/find_test.go @@ -11,7 +11,7 @@ import ( var findFileSystem = []string{ "backup/archive.zip", - "backup/documents/.hidden_old_resume.txt", + "backup/documents/.hidden_resume.txt", "backup/documents/old_cover_letter.docx", "backup/documents/old_resume.docx", "backup/important_data/file1.txt", @@ -31,7 +31,7 @@ var findFileSystem = []string{ "photos/family/photo2.PNG", "photos/family/photo3.gif", "photos/vacation/beach.jpg", - "photos/vacation/mountains/.hidden_old_photo.jpg", + "photos/vacation/mountains/.hidden_photo.jpg", "photos/vacation/mountains/OLDPHOTO3.JPG", "photos/vacation/mountains/OLD_PHOTO5.JPG", "photos/vacation/mountains/photo1.jpg", @@ -101,7 +101,8 @@ var testCases = []testutil.TestCase{ "photos/family/photo3.gif", "photos/vacation/mountains/photo1.jpg", }, - Args: []string{"-f", "photo", "-R", "-E", "^old", "-E", "webp$"}, + Args: []string{"-f", "photo", "-R", "-E", "^old", "-E", "webp$"}, + SetupFunc: setupWindowsHidden, }, { @@ -130,9 +131,10 @@ var testCases = []testutil.TestCase{ }, { - Name: "match files at the top level", - Want: []string{"LICENSE.txt", "Makefile", "README.md", "main.go"}, - Args: []string{"-f", ".*"}, + Name: "match files at the top level", + Want: []string{"LICENSE.txt", "Makefile", "README.md", "main.go"}, + Args: []string{"-f", ".*"}, + SetupFunc: setupWindowsHidden, }, { @@ -185,7 +187,7 @@ var testCases = []testutil.TestCase{ }, { - Name: "match files not containing an umulat", + Name: "match files containing an umulat", Want: []string{ "photos/vacation/mountains/Öffnen.txt", }, @@ -247,6 +249,10 @@ func findTest(t *testing.T, cases []testutil.TestCase) { t.Run(tc.Name, func(t *testing.T) { testutil.UpdateBaseDir(tc.Want, testDir) + if tc.SetupFunc != nil { + t.Cleanup(tc.SetupFunc(t, testDir)) + } + // TODO: Make it possible to test without explicitly providing // directory argument config := testutil.GetConfig(t, &tc, testDir) diff --git a/find/find_test/find_unix_test.go b/find/find_test/find_unix_test.go index 794c592..59a97b7 100644 --- a/find/find_test/find_unix_test.go +++ b/find/find_test/find_unix_test.go @@ -9,6 +9,14 @@ import ( "github.com/ayoisaiah/f2/internal/testutil" ) +func setHidden(path string) error { + return nil +} + +func setupWindowsHidden(t *testing.T, testDir string) (teardown func()) { + return func() {} +} + var unixTestCases = []testutil.TestCase{ { Name: "exclude hidden files by default", @@ -20,9 +28,9 @@ var unixTestCases = []testutil.TestCase{ Name: "include hidden files in search", Want: []string{ ".hidden_file", - "backup/documents/.hidden_old_resume.txt", + "backup/documents/.hidden_resume.txt", "documents/.hidden_file.txt", - "photos/vacation/mountains/.hidden_old_photo.jpg", + "photos/vacation/mountains/.hidden_photo.jpg", }, Args: []string{"-f", "hidden", "-RH"}, }, diff --git a/find/find_test/find_windows_test.go b/find/find_test/find_windows_test.go index ad71c54..d22b100 100644 --- a/find/find_test/find_windows_test.go +++ b/find/find_test/find_windows_test.go @@ -4,24 +4,80 @@ package find_test import ( + "os" + "path/filepath" + "syscall" "testing" "github.com/ayoisaiah/f2/internal/testutil" ) +func setHidden(path string) error { + filenameW, err := syscall.UTF16PtrFromString(path) + if err != nil { + return err + } + + err = syscall.SetFileAttributes(filenameW, syscall.FILE_ATTRIBUTE_HIDDEN) + if err != nil { + return err + } + + return nil +} + +func setupWindowsHidden(t *testing.T, testDir string) (teardown func()) { + err := filepath.WalkDir( + testDir, + func(path string, d os.DirEntry, err error) error { + if err != nil { + return err // Handle errors gracefully + } + + if !d.IsDir() && filepath.Base(path)[0] == 46 { + setHidden((path)) + } + + return nil + }, + ) + if err != nil { + t.Fatal(err) + } + + return func() {} +} + var windowsTestCases = []testutil.TestCase{ { Name: "dot files shouldn't be regarded as hidden in Windows", Want: []string{ ".hidden_file", - "backup/documents/.hidden_old_resume.txt", + "backup/documents/.hidden_resume.txt", "documents/.hidden_file.txt", - "photos/vacation/mountains/.hidden_old_photo.jpg", + "photos/vacation/mountains/.hidden_photo.jpg", }, Args: []string{"-f", "hidden", "-R"}, }, - // TODO: Add a test for hidden files + { + Name: "exclude files with hidden attribute", + Want: []string{}, + Args: []string{"-f", "hidden", "-R"}, + SetupFunc: setupWindowsHidden, + }, + + { + Name: "include files with hidden attribute", + Want: []string{ + ".hidden_file", + "backup/documents/.hidden_resume.txt", + "documents/.hidden_file.txt", + "photos/vacation/mountains/.hidden_photo.jpg", + }, + Args: []string{"-f", "hidden", "-RH"}, + SetupFunc: setupWindowsHidden, + }, } // TestFindWindows only tests search behaviors perculiar to Windows diff --git a/find/find_test/testdata/input.csv b/find/find_test/testdata/input.csv index fb817a9..d3161ee 100644 --- a/find/find_test/testdata/input.csv +++ b/find/find_test/testdata/input.csv @@ -1,6 +1,6 @@ Filename,Replacement,Random LICENSE.txt,LICENCE,GPLv3 -backup/documents/.hidden_old_resume.txt,not_hidden.txt, +backup/documents/.hidden_resume.txt,not_hidden.txt, projects/project1/index.html,main.html, projects/project2/index.html,{csv.3},profile-page documents/cv.docx,resume.docx,myname diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index 88d4c07..9318e80 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -19,16 +19,16 @@ import ( // TestCase represents a unique test case. type TestCase struct { - Error error `json:"error"` - ConflictDetected bool `json:"conflict_detected"` - SetupFunc func(t *testing.T) (teardown func()) `json:"-"` - DefaultOpts string `json:"default_opts"` - Name string `json:"name"` - GoldenFile string `json:"golden_file"` - Args []string `json:"args"` - PathArgs []string `json:"path_args"` - Changes file.Changes `json:"changes"` - Want []string `json:"want"` + Error error `json:"error"` + ConflictDetected bool `json:"conflict_detected"` + SetupFunc func(t *testing.T, testDir string) (teardown func()) `json:"-"` + DefaultOpts string `json:"default_opts"` + Name string `json:"name"` + GoldenFile string `json:"golden_file"` + Args []string `json:"args"` + PathArgs []string `json:"path_args"` + Changes file.Changes `json:"changes"` + Want []string `json:"want"` } // SetupFileSystem creates all required files and folders for diff --git a/replace/replace_test/replace_test.go b/replace/replace_test/replace_test.go index 6476589..9ad2028 100644 --- a/replace/replace_test/replace_test.go +++ b/replace/replace_test/replace_test.go @@ -28,7 +28,7 @@ func replaceTest(t *testing.T, cases []testutil.TestCase) { t.Run(tc.Name, func(t *testing.T) { if tc.SetupFunc != nil { - t.Cleanup(tc.SetupFunc(t)) + t.Cleanup(tc.SetupFunc(t, "")) } config := testutil.GetConfig(t, &tc, ".") diff --git a/replace/replace_test/variables_test.go b/replace/replace_test/variables_test.go index 1a5bd6b..81aff53 100644 --- a/replace/replace_test/variables_test.go +++ b/replace/replace_test/variables_test.go @@ -19,7 +19,7 @@ func getCurrentDate() string { return fmt.Sprintf("%d-%02d-%02d", year, month, day) } -func createDateFile(t *testing.T) func() { +func createDateFile(t *testing.T, testDir string) func() { t.Helper() dateFilePath := filepath.Join("testdata", "date.txt") diff --git a/report/report_test/report_test.go b/report/report_test/report_test.go index 8b39f5a..72e924b 100644 --- a/report/report_test/report_test.go +++ b/report/report_test/report_test.go @@ -46,7 +46,7 @@ func reportTest(t *testing.T, cases []testutil.TestCase) { t.Run(tc.Name, func(t *testing.T) { if tc.SetupFunc != nil { - t.Cleanup(tc.SetupFunc(t)) + t.Cleanup(tc.SetupFunc(t, "")) } conf := testutil.GetConfig(t, &tc, ".") diff --git a/validate/validate_test/validate_test.go b/validate/validate_test/validate_test.go index 2ab0967..00a36d4 100644 --- a/validate/validate_test/validate_test.go +++ b/validate/validate_test/validate_test.go @@ -45,7 +45,7 @@ func validateTest(t *testing.T, cases []testutil.TestCase) { t.Run(tc.Name, func(t *testing.T) { if tc.SetupFunc != nil { - t.Cleanup(tc.SetupFunc(t)) + t.Cleanup(tc.SetupFunc(t, "")) } if len(tc.Args) == 0 {