aboutsummaryrefslogtreecommitdiffstats
path: root/skate/testutil/extra.go
blob: 7537f75fcb0876e0feecb30d7c7b6fd3183f195e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package testutil

import (
	"bytes"
	"io/ioutil"
	"reflect"

	"git.archive.org/martin/cgraph/skate/atomic"
)

// BufferFileEquals returns true, if the contents of a given buffer matches the
// contents of a file given by filename.
func BufferFileEquals(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
}

// BufferToTemp writes the content of a buffer to a temporary file and returns
// its path.
func BufferToTemp(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
}