diff --git a/goBuild/build_test.go b/goBuild/build_test.go index 316619d1..5e9963c0 100644 --- a/goBuild/build_test.go +++ b/goBuild/build_test.go @@ -8,7 +8,6 @@ import ( "testing" - "github.com/KyleKing/recipes/goBuild/testUtils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -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) } diff --git a/goBuild/testUtils/testUtils.go b/goBuild/testUtils/testUtils.go deleted file mode 100644 index 87cc6bdc..00000000 --- a/goBuild/testUtils/testUtils.go +++ /dev/null @@ -1,85 +0,0 @@ -package testUtils - -import ( - "bytes" - "io" - "os" -) - -// Decide if two files have the same contents or not. -// chunkSize is the size of the blocks to scan by; pass 0 to get a sensible default. -// *Follows* symlinks. -// -// May return an error if something else goes wrong; in this case, you should ignore the value of 'same'. -// -// Copied from https://stackoverflow.com/a/73411967/3219667 -// under CC-BY-SA-4.0 by several contributors -func FileCmp(file1, file2 string, chunkSize int) (same bool, err error) { - - if chunkSize == 0 { - chunkSize = 4 * 1024 - } - - // shortcuts: check file metadata - stat1, err := os.Stat(file1) - if err != nil { - return false, err - } - - stat2, err := os.Stat(file2) - if err != nil { - return false, err - } - - // are inputs are literally the same file? - if os.SameFile(stat1, stat2) { - return true, nil - } - - // do inputs at least have the same size? - if stat1.Size() != stat2.Size() { - return false, nil - } - - // long way: compare contents - f1, err := os.Open(file1) - if err != nil { - return false, err - } - defer f1.Close() - - f2, err := os.Open(file2) - if err != nil { - return false, err - } - defer f2.Close() - - b1 := make([]byte, chunkSize) - b2 := make([]byte, chunkSize) - for { - n1, err1 := io.ReadFull(f1, b1) - n2, err2 := io.ReadFull(f2, b2) - - // https://pkg.go.dev/io#Reader - // > Callers should always process the n > 0 bytes returned - // > before considering the error err. Doing so correctly - // > handles I/O errors that happen after reading some bytes - // > and also both of the allowed EOF behaviors. - - if !bytes.Equal(b1[:n1], b2[:n2]) { - return false, nil - } - - if (err1 == io.EOF && err2 == io.EOF) || (err1 == io.ErrUnexpectedEOF && err2 == io.ErrUnexpectedEOF) { - return true, nil - } - - // some other error, like a dropped network connection or a bad transfer - if err1 != nil { - return false, err1 - } - if err2 != nil { - return false, err2 - } - } -}