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 | |
parent | 3078e0c4227b395ddec143c09004026bd057d0dd (diff) | |
download | refcat-042ed5693d951205f5153c1868eeb29f8398f688.tar.gz refcat-042ed5693d951205f5153c1868eeb29f8398f688.zip |
move test utilities into testutil
Diffstat (limited to 'skate')
-rw-r--r-- | skate/reduce_test.go | 37 | ||||
-rw-r--r-- | skate/testutil/extra.go | 36 |
2 files changed, 41 insertions, 32 deletions
diff --git a/skate/reduce_test.go b/skate/reduce_test.go index 78939a1..4ff2508 100644 --- a/skate/reduce_test.go +++ b/skate/reduce_test.go @@ -2,12 +2,11 @@ package skate import ( "bytes" - "io/ioutil" "reflect" "testing" "time" - "git.archive.org/martin/cgraph/skate/atomic" + "git.archive.org/martin/cgraph/skate/testutil" "git.archive.org/martin/cgraph/skate/xio" "github.com/kr/pretty" ) @@ -421,47 +420,21 @@ func TestZippyExact(t *testing.T) { if err != c.err { t.Errorf("[%d] got %v, want %v", i, err, c.err) } - ok, err := equalsFilename(&buf, c.out) + ok, err := testutil.EqualsFilename(&buf, c.out) if err != nil { t.Errorf("failed to open test file: %v", c.out) } if !ok { - filename, err := tempWriteFile(&buf) + filename, err := testutil.TempWriteFile(&buf) if err != nil { t.Logf("could not write temp file") } - t.Errorf("[%d] output mismatch (buffer length=%d, content=%v), want %v", i, buf.Len(), filename, c.out) + t.Errorf("[%d] output mismatch (buffer length=%d, content=%v), want %v", + i, buf.Len(), filename, c.out) } } } -// 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 -} - func TestCutBatch(t *testing.T) { var cases = []struct { lines []string 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 +} |