Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Oct 12, 2024
1 parent 9009742 commit 9f6104b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 84 deletions.
48 changes: 48 additions & 0 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
43 changes: 18 additions & 25 deletions replace/replace_test/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package replace_test

import (
"errors"
"path/filepath"
"testing"

"github.com/ayoisaiah/f2/internal/file"
Expand All @@ -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)
}
},
)
}
}

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 @@ -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)
}
}
}
Expand Down
94 changes: 36 additions & 58 deletions validate/validate_test/validate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package validate_test

import (
"path/filepath"
"testing"

"github.com/jinzhu/copier"
Expand All @@ -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)
}
},
)
}
}

Expand Down

0 comments on commit 9f6104b

Please sign in to comment.