diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index 4375770..4988adf 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -17,6 +17,7 @@ import ( "github.com/ayoisaiah/f2/internal/config" "github.com/ayoisaiah/f2/internal/file" "github.com/ayoisaiah/f2/internal/osutil" + "github.com/ayoisaiah/f2/internal/status" ) // TestCase represents a unique test case. @@ -194,6 +195,53 @@ func UpdateFileChanges(files file.Changes) { } } +func RunTestCase( + t *testing.T, + tc *TestCase, + runFunc func(t *testing.T, tc *TestCase), +) { + t.Helper() + + t.Run(tc.Name, func(t *testing.T) { + if tc.SetupFunc != nil { + t.Cleanup(tc.SetupFunc(t, "")) + } + + runFunc(t, tc) + }) +} + +func ProcessTestCaseChanges(t *testing.T, cases []TestCase) { + t.Helper() + + for i := range cases { + tc := cases[i] + for j := range tc.Changes { + ch := tc.Changes[j] + + if ch.Status == "" { + cases[i].Changes[j].Status = status.OK + } + + cases[i].Changes[j].OriginalName = ch.Source + + if cases[i].Changes[j].TargetPath == "" { + cases[i].Changes[j].SourcePath = filepath.Join( + ch.BaseDir, + ch.Source, + ) + } + + if cases[i].Changes[j].TargetPath == "" { + cases[i].Changes[j].TargetPath = filepath.Join( + ch.BaseDir, + ch.Target, + ) + } + } + } +} + // GetConfig constructs the app configuration from command-line arguments. func GetConfig(t *testing.T, tc *TestCase, testDir string) *config.Config { t.Helper() diff --git a/replace/replace_test/replace_test.go b/replace/replace_test/replace_test.go index 9ad2028..d126d09 100644 --- a/replace/replace_test/replace_test.go +++ b/replace/replace_test/replace_test.go @@ -2,7 +2,6 @@ package replace_test import ( "errors" - "path/filepath" "testing" "github.com/ayoisaiah/f2/internal/file" @@ -13,36 +12,30 @@ import ( func replaceTest(t *testing.T, cases []testutil.TestCase) { t.Helper() + testutil.ProcessTestCaseChanges(t, cases) + for i := range cases { tc := cases[i] - for j := range tc.Changes { - ch := tc.Changes[j] - - cases[i].Changes[j].OriginalName = ch.Source - cases[i].Changes[j].SourcePath = filepath.Join( - ch.BaseDir, - ch.Source, - ) - } + testutil.RunTestCase( + t, + &tc, + func(t *testing.T, tc *testutil.TestCase) { + t.Helper() - t.Run(tc.Name, func(t *testing.T) { - if tc.SetupFunc != nil { - t.Cleanup(tc.SetupFunc(t, "")) - } + conf := testutil.GetConfig(t, tc, ".") - config := testutil.GetConfig(t, &tc, ".") + changes, err := replace.Replace(conf, tc.Changes) + if err == nil { + testutil.CompareTargetPath(t, tc.Want, changes) + return + } - changes, err := replace.Replace(config, tc.Changes) - if err == nil { - testutil.CompareTargetPath(t, tc.Want, changes) - return - } - - if !errors.Is(err, tc.Error) { - t.Fatal(err) - } - }) + if !errors.Is(err, tc.Error) { + t.Fatal(err) + } + }, + ) } } diff --git a/replace/replace_test/variables_test.go b/replace/replace_test/variables_test.go index b44f52f..0754e7e 100644 --- a/replace/replace_test/variables_test.go +++ b/replace/replace_test/variables_test.go @@ -50,7 +50,7 @@ func createDateFile(t *testing.T, _ string) func() { return func() { err = os.Remove(dateFilePath) if err != nil { - t.Fatal(err) + t.Log(err) } } } diff --git a/validate/validate_test/validate_test.go b/validate/validate_test/validate_test.go index 38e7775..ee16d5c 100644 --- a/validate/validate_test/validate_test.go +++ b/validate/validate_test/validate_test.go @@ -1,7 +1,6 @@ package validate_test import ( - "path/filepath" "testing" "github.com/jinzhu/copier" @@ -17,76 +16,55 @@ var autoFixArgs = []string{"-r", "", "-F"} func validateTest(t *testing.T, cases []testutil.TestCase) { t.Helper() - for i := range cases { - tc := cases[i] - - for j := range tc.Changes { - ch := tc.Changes[j] - - if ch.Status == "" { - cases[i].Changes[j].Status = status.OK - } - - cases[i].Changes[j].OriginalName = ch.Source - cases[i].Changes[j].SourcePath = filepath.Join( - ch.BaseDir, - ch.Source, - ) - - if cases[i].Changes[j].TargetPath == "" { - cases[i].Changes[j].TargetPath = filepath.Join( - ch.BaseDir, - ch.Target, - ) - } - } - } + testutil.ProcessTestCaseChanges(t, cases) for i := range cases { tc := cases[i] - t.Run(tc.Name, func(t *testing.T) { - if tc.SetupFunc != nil { - t.Cleanup(tc.SetupFunc(t, "")) - } + testutil.RunTestCase( + t, + &tc, + func(t *testing.T, tc *testutil.TestCase) { + t.Helper() - if len(tc.Args) == 0 { - tc.Args = []string{"-r", ""} - } + if len(tc.Args) == 0 { + tc.Args = []string{"-r", ""} + } - config := testutil.GetConfig(t, &tc, ".") + conf := testutil.GetConfig(t, tc, ".") - var expectedChanges file.Changes + var expectedChanges file.Changes - err := copier.Copy(&expectedChanges, &tc.Changes) - if err != nil { - t.Fatal(err) - } + err := copier.Copy(&expectedChanges, &tc.Changes) + if err != nil { + t.Fatal(err) + } - for j := range tc.Changes { - tc.Changes[j].Status = status.OK - } + for j := range tc.Changes { + tc.Changes[j].Status = status.OK + } - conflictDetected := validate.Validate( - tc.Changes, - config.AutoFixConflicts, - config.AllowOverwrites, - ) + conflictDetected := validate.Validate( + tc.Changes, + conf.AutoFixConflicts, + conf.AllowOverwrites, + ) - if tc.ConflictDetected && !conflictDetected { - t.Fatal("expected a conflict, but got none") - } + if tc.ConflictDetected && !conflictDetected { + t.Fatal("expected a conflict, but got none") + } - if !tc.ConflictDetected && conflictDetected { - t.Fatal("did not expect a conflict, but got one") - } + if !tc.ConflictDetected && conflictDetected { + t.Fatal("did not expect a conflict, but got one") + } - if tc.ConflictDetected { - testutil.CompareChanges(t, expectedChanges, tc.Changes) - } else { - testutil.CompareTargetPath(t, tc.Want, tc.Changes) - } - }) + if tc.ConflictDetected { + testutil.CompareChanges(t, expectedChanges, tc.Changes) + } else { + testutil.CompareTargetPath(t, tc.Want, tc.Changes) + } + }, + ) } }