Skip to content

Commit

Permalink
test: use diff to reduce first-party code
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Jan 16, 2025
1 parent b05d421 commit 9dcc90b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 104 deletions.
40 changes: 21 additions & 19 deletions goBuild/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"testing"

"github.com/KyleKing/recipes/goBuild/testUtils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -38,29 +37,32 @@ func initTestDir() (string, error) {
}

func TestReplaceDjWithHtml(t *testing.T) {
publicTestDir, err := initTestDir()
require.NoError(t, err)
publicTestDir, errInit := initTestDir()
require.NoError(t, errInit)
expectDir := filepath.Join(filepath.Dir(publicTestDir), "test_expected")

Build(publicTestDir)

validateFiles := func(path string, fileInfo os.FileInfo, inpErr error) error {
// Only compare files
stat, err := os.Stat(path)
require.NoError(t, err)
if !(stat.Mode().IsRegular()) {
return nil
// Verify content matches expected using diff (https://stackoverflow.com/a/1644641/3219667)
cmd := exec.Command("diff", "-arq", expectDir+"/", publicTestDir+"/")
outDiff, errDiff := cmd.Output()
assert.Equal(t, "", string(outDiff))
if errDiff != nil {
fmt.Println("Error running:", cmd, outDiff, errDiff)

// Replace expected directory with output from test for comparison in git
cmd = exec.Command("mv", expectDir+"/", expectDir+"-backup/")
out, err := cmd.Output()
if err != nil {
fmt.Println("Error running:", cmd, out, err)
}

rel, err := filepath.Rel(publicTestDir, path)
require.NoError(t, err)
expectedPath := filepath.Join(expectDir, rel)
// Verify content matches expected
same, err := testUtils.FileCmp(path, expectedPath, 0)
assert.Equal(t, err, nil, fmt.Sprintf("Error comparing files %s", rel))
assert.Equal(t, same, true, fmt.Sprintf("Error: use git to diff %s", rel))
return nil
cmd = exec.Command("cp", "-R", publicTestDir+"/", expectDir+"/")
out, err = cmd.Output()
if err != nil {
fmt.Println("Error running:", cmd, out, err)
}
fmt.Println("See git diff for changes")
}
err = filepath.Walk(publicTestDir, validateFiles)
require.NoError(t, err)
require.NoError(t, errDiff)
}
85 changes: 0 additions & 85 deletions goBuild/testUtils/testUtils.go

This file was deleted.

0 comments on commit 9dcc90b

Please sign in to comment.