aboutsummaryrefslogtreecommitdiffstats
path: root/skate/testutil/extra.go
diff options
context:
space:
mode:
Diffstat (limited to 'skate/testutil/extra.go')
-rw-r--r--skate/testutil/extra.go36
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
+}