Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Sep 8, 2024
1 parent 27b9086 commit d6dad38
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 28 deletions.
2 changes: 1 addition & 1 deletion find/find_test/find_csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 13 additions & 7 deletions find/find_test/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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,
},

{
Expand Down Expand Up @@ -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,
},

{
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions find/find_test/find_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"},
},
Expand Down
62 changes: 59 additions & 3 deletions find/find_test/find_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion find/find_test/testdata/input.csv
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 10 additions & 10 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion replace/replace_test/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".")
Expand Down
2 changes: 1 addition & 1 deletion replace/replace_test/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion report/report_test/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".")
Expand Down
2 changes: 1 addition & 1 deletion validate/validate_test/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d6dad38

Please sign in to comment.