diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-07-27 22:32:16 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-07-27 22:32:16 +0200 |
commit | 042ed5693d951205f5153c1868eeb29f8398f688 (patch) | |
tree | cb0832bf9b0517659af96f399f7ad54189957953 /skate/testutil/extra.go | |
parent | 3078e0c4227b395ddec143c09004026bd057d0dd (diff) | |
download | refcat-042ed5693d951205f5153c1868eeb29f8398f688.tar.gz refcat-042ed5693d951205f5153c1868eeb29f8398f688.zip |
move test utilities into testutil
Diffstat (limited to 'skate/testutil/extra.go')
-rw-r--r-- | skate/testutil/extra.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/skate/testutil/extra.go b/skate/testutil/extra.go new file mode 100644 index 0000000..cb533c3 --- /dev/null +++ b/skate/testutil/extra.go @@ -0,0 +1,36 @@ +package testutil + +import ( + "bytes" + "io/ioutil" + "reflect" + + "git.archive.org/martin/cgraph/skate/atomic" +) + +// EqualsFilename returns true, if the contents of a given buffer matches the +// contents of a file given by filename. +func EqualsFilename(buf *bytes.Buffer, filename string) (bool, error) { + b, err := ioutil.ReadFile(filename) + if err != nil { + return false, err + } + bb := buf.Bytes() + if len(bb) == 0 && len(b) == 0 { + return true, nil + } + return reflect.DeepEqual(b, bb), nil +} + +// TempWriteFile writes the content of a buffer to a temporary file and returns +// its path. +func TempWriteFile(buf *bytes.Buffer) (string, error) { + f, err := ioutil.TempFile("", "skate-test-*") + if err != nil { + return "", err + } + if err = atomic.WriteFile(f.Name(), buf.Bytes(), 0755); err != nil { + return "", err + } + return f.Name(), nil +} |