From 042ed5693d951205f5153c1868eeb29f8398f688 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Tue, 27 Jul 2021 22:32:16 +0200 Subject: move test utilities into testutil --- skate/reduce_test.go | 37 +++++-------------------------------- skate/testutil/extra.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 skate/testutil/extra.go (limited to 'skate') 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 +} -- cgit v1.2.3