diff options
| author | Paul Buetow <paul@buetow.org> | 2021-10-02 21:25:34 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-02 21:27:50 +0300 |
| commit | 91ea8398ebc0febce20b9a460f9372998cd0b80f (patch) | |
| tree | 9898ebe8da115f433d2098a3dacf871f23a0ce73 /integrationtests/commons.go | |
| parent | 86ec83754e0ee7153ad55091f7b6da448bc529c5 (diff) | |
add dgrep test
Diffstat (limited to 'integrationtests/commons.go')
| -rw-r--r-- | integrationtests/commons.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/integrationtests/commons.go b/integrationtests/commons.go new file mode 100644 index 0000000..6422949 --- /dev/null +++ b/integrationtests/commons.go @@ -0,0 +1,61 @@ +package integrationtests + +import ( + "crypto/sha256" + "encoding/base64" + "fmt" + "io/ioutil" + "os" + "os/exec" + "strings" + "testing" +) + +func runCommand(t *testing.T, cmd string, args []string, stdoutFile string) error { + if _, err := os.Stat(cmd); err != nil { + return fmt.Errorf("No such binary %s, please compile first (%v)", cmd, err) + } + + t.Log("Executing command:", cmd, strings.Join(args, " ")) + bytes, err := exec.Command(cmd, args...).Output() + if err != nil { + return err + } + + t.Log("Writing stdout to file", stdoutFile) + fd, err := os.Create(stdoutFile) + if err != nil { + return err + } + fd.Write(bytes) + fd.Close() + + 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 +} |
