summaryrefslogtreecommitdiff
path: root/integrationtests/commons.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-10-03 13:09:32 +0300
committerPaul Buetow <paul@buetow.org>2021-10-03 13:09:32 +0300
commit07e1470892beacf0722276f94bacbd822b002540 (patch)
treed8073ee564eb606fb27b35f4e63bf5780ccff212 /integrationtests/commons.go
parent91ea8398ebc0febce20b9a460f9372998cd0b80f (diff)
add dmap tests
Diffstat (limited to 'integrationtests/commons.go')
-rw-r--r--integrationtests/commons.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/integrationtests/commons.go b/integrationtests/commons.go
index 6422949..74eeac5 100644
--- a/integrationtests/commons.go
+++ b/integrationtests/commons.go
@@ -1,6 +1,7 @@
package integrationtests
import (
+ "bufio"
"crypto/sha256"
"encoding/base64"
"fmt"
@@ -33,6 +34,60 @@ func runCommand(t *testing.T, cmd string, args []string, stdoutFile string) erro
return nil
}
+// 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
+ }
+
+ if err := compareMaps(a, b); err != nil {
+ return err
+ }
+ 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)