Skip to content

Commit

Permalink
Fix utk_test
Browse files Browse the repository at this point in the history
utk_test was calling sed and diff in non-portable ways; it did not work
any more, and it would never work on osx.

Remove calls to diff and sed, and just do the equivalent using Go packages.

Go test now works on osx.

Signed-off-by: Ronald G. Minnich <[email protected]>
  • Loading branch information
rminnich authored and orangecms committed Sep 16, 2024
1 parent 923daa8 commit 2e510d1
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions integration/utk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package utk_test
import (
"bytes"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -58,6 +60,11 @@ func TestExtractAssembleExtract(t *testing.T) {
// For debugging, uncomment the next line and comment out os.RemoveAll
// t.Logf("temp %v", tmpDir)
defer os.RemoveAll(tmpDir)
re := `"Size".*:.*[0-9].*\n`
szre, err := regexp.Compile(re)
if err != nil {
t.Fatalf("Compiling re %q: got %v, want nil", re, err)
}

for _, tt := range romList(t) {
t.Run(tt, func(t *testing.T) {
Expand All @@ -68,11 +75,9 @@ func TestExtractAssembleExtract(t *testing.T) {

// Test paths
var (
dir1 = filepath.Join(tmpDirT, "dir1")
tmpRom = filepath.Join(tmpDirT, "tmp.rom")
dir2 = filepath.Join(tmpDirT, "dir2")
summary1Json = filepath.Join(dir1, "summary.json")
summary2Json = filepath.Join(dir2, "summary.json")
dir1 = filepath.Join(tmpDirT, "dir1")
tmpRom = filepath.Join(tmpDirT, "tmp.rom")
dir2 = filepath.Join(tmpDirT, "dir2")
)

// Extract
Expand All @@ -99,27 +104,44 @@ func TestExtractAssembleExtract(t *testing.T) {
}
}

sedRemove := func(path string) {
sedCmd := exec.Command("sed", "-i", "/\"Size\": [0-9]*.*/d", path)
sedCmd.Stderr = os.Stderr
sedCmd.Stdout = os.Stdout
if err := sedCmd.Run(); err != nil {
t.Error(fmt.Sprintf("Sed failed for %s, error: %s", path, err.Error()))
t.Logf("walk %q and check against %q", dir1, dir2)
if err := filepath.Walk(dir1, func(n1 string, s1 fs.FileInfo, err error) error {
if err != nil {
return err
}
n2, err := filepath.Rel(dir1, n1)
if err != nil {
return fmt.Errorf("%q: can not get relative to %q:%v", n1, dir1, err)
}
n2 = filepath.Join(dir2, n2)
s2, err := os.Stat(n2)
if err != nil {
return fmt.Errorf("%q: expected it to exist, got %w", n2, err)
}
if s2.IsDir() != s1.IsDir() {
return fmt.Errorf("%q.IsDir() != %q.Isdir:%w", s1, s2, os.ErrInvalid)
}
if s2.IsDir() {
return nil
}
d1, err := os.ReadFile(n1)
if err != nil {
return fmt.Errorf("Reading %q:%v", n1, err)
}
d1 = szre.ReplaceAll(d1, []byte{})
d2, err := os.ReadFile(n2)
if err != nil {
return fmt.Errorf("Reading %q:%v", n2, err)
}
d2 = szre.ReplaceAll(d2, []byte{})
if string(d1) != string(d2) {
return fmt.Errorf("%q has value %q; %q has value %q; expected the to be equal:%w", n1, d1, n2, d2, os.ErrInvalid)
}
return nil
}); err != nil {
t.Fatalf("%q and %q are not equal:%v", dir1, dir2, os.ErrInvalid)
}
// Remove all occurences of Size from JSON file
// compressed sizes are different
// diff will always fail if this is not done.
sedRemove(summary1Json)
sedRemove(summary2Json)

// Recursively test for equality.
cmd := exec.Command("diff", "-r", dir1, dir2)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
t.Error("directories did not recursively compare equal")
}

})
}
}
Expand All @@ -128,7 +150,7 @@ func TestExtractAssembleExtract(t *testing.T) {
// which affects the tree, you must commit changes to the golden JSON files
// with:
//
// utk integration/roms/OVMF.rom json > integration/roms/OVMF.json
// utk integration/roms/OVMF.rom json > integration/roms/OVMF.json
//
// Otherwise, this test will fail. This gives you a chance to review how your
// code affects the tree and identify any mistakes.
Expand Down

0 comments on commit 2e510d1

Please sign in to comment.