From 71f89dc7ec7cf993d1eca98771212afe6310e9c8 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 10 Oct 2021 19:42:48 +0300 Subject: refactor --- integrationtests/fileutils.go | 99 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 integrationtests/fileutils.go (limited to 'integrationtests/fileutils.go') diff --git a/integrationtests/fileutils.go b/integrationtests/fileutils.go new file mode 100644 index 0000000..d771607 --- /dev/null +++ b/integrationtests/fileutils.go @@ -0,0 +1,99 @@ +package integrationtests + +import ( + "bufio" + "crypto/sha256" + "encoding/base64" + "fmt" + "io/ioutil" + "os" + "os/exec" + "testing" +) + +// Checks whether both files have the same lines (order doesn't matter) +func compareFilesContents(t *testing.T, fileA, fileB string) error { + mapFile := func(file string) (map[string]int, error) { + t.Log("Reading", file) + contents := make(map[string]int) + fd, err := os.Open(file) + if err != nil { + return contents, err + } + defer fd.Close() + + scanner := bufio.NewScanner(fd) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + count, _ := contents[line] + contents[line] = count + 1 + } + + return contents, nil + } + + compareMaps := func(a, b map[string]int) error { + for line, countA := range a { + countB, ok := b[line] + if !ok { + return fmt.Errorf("Files differ, line '%s' is missing in one of them", line) + } + if countA != countB { + return fmt.Errorf("Files differ, count of line '%s' is %d in one but %d in another", + line, countA, countB) + } + } + return nil + } + + a, err := mapFile(fileA) + if err != nil { + return err + } + b, err := mapFile(fileB) + if err != nil { + return err + } + + // The mapreduce result can be in a different order each time (Golang maps are not sorted). + t.Log(fmt.Sprintf("Checking whether %s has same lines as file %s (ignoring line order)", + fileA, fileB)) + if err := compareMaps(a, b); err != nil { + return err + } + t.Log(fmt.Sprintf("Checking whether %s has same lines as file %s (ignoring line order)", + fileB, fileA)) + if err := compareMaps(b, a); err != nil { + return err + } + + return nil +} + +func compareFiles(t *testing.T, fileA, fileB string) error { + t.Log("Comparing files", fileA, fileB) + shaFileA := shaOfFile(t, fileA) + shaFileB := shaOfFile(t, fileB) + + if shaFileA != shaFileB { + t.Errorf("Expected SHA %s but got %s", shaFileA, shaFileB) + if bytes, err := exec.Command("diff", "-u", fileA, fileB).Output(); err != nil { + return fmt.Errorf(string(bytes)) + } + } + + return nil +} + +func shaOfFile(t *testing.T, file string) string { + bytes, err := ioutil.ReadFile(file) + if err != nil { + t.Error(err) + } + hasher := sha256.New() + hasher.Write(bytes) + sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + t.Log("SHA", file, sha) + return sha +} -- cgit v1.2.3 From 5f3e6b8569b5b71853208949506bbcd3c44488b5 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 13 Oct 2021 20:39:00 +0300 Subject: backport docs from master --- integrationtests/fileutils.go | 1 + 1 file changed, 1 insertion(+) (limited to 'integrationtests/fileutils.go') diff --git a/integrationtests/fileutils.go b/integrationtests/fileutils.go index d771607..8ab66a0 100644 --- a/integrationtests/fileutils.go +++ b/integrationtests/fileutils.go @@ -47,6 +47,7 @@ func compareFilesContents(t *testing.T, fileA, fileB string) error { return nil } + // Read files into maps. a, err := mapFile(fileA) if err != nil { return err -- cgit v1.2.3 From 698fb76b98c46c677abe13fdc93afc6c4f38c39e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 14 Oct 2021 20:55:35 +0300 Subject: refactor --- integrationtests/fileutils.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'integrationtests/fileutils.go') diff --git a/integrationtests/fileutils.go b/integrationtests/fileutils.go index 8ab66a0..1a55732 100644 --- a/integrationtests/fileutils.go +++ b/integrationtests/fileutils.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "os/exec" + "strings" "testing" ) @@ -78,10 +79,12 @@ func compareFiles(t *testing.T, fileA, fileB string) error { shaFileB := shaOfFile(t, fileB) if shaFileA != shaFileB { - t.Errorf("Expected SHA %s but got %s", shaFileA, shaFileB) + var sb strings.Builder + sb.WriteString(fmt.Sprintf("Expected SHA %s but got %s:\n", shaFileA, shaFileB)) if bytes, err := exec.Command("diff", "-u", fileA, fileB).Output(); err != nil { - return fmt.Errorf(string(bytes)) + sb.Write(bytes) } + return fmt.Errorf(sb.String()) } return nil -- cgit v1.2.3