1
Fork 0

remove testing dependencies

main
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 0823f3e5bd
commit c042482a2f
5 changed files with 67 additions and 33 deletions

View File

@ -7,8 +7,6 @@ import (
"fmt" "fmt"
"io" "io"
"testing" "testing"
"github.com/stretchr/testify/require"
) )
type ( type (
@ -122,7 +120,9 @@ func Extract(t *testing.T, r io.Reader) []Extractable {
if err == io.EOF { if err == io.EOF {
break break
} }
require.NoError(t, err) if err != nil {
t.Errorf("expected nil error: %v", err)
}
var elem Extractable var elem Extractable
switch hdr.Typeflag { switch hdr.Typeflag {

View File

@ -4,10 +4,8 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"io" "io"
"reflect"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestTarball(t *testing.T) { func TestTarball(t *testing.T) {
@ -26,7 +24,10 @@ func TestTarball(t *testing.T) {
Dir{Name: "bin"}, Dir{Name: "bin"},
Hardlink{Name: "entrypoint2"}, Hardlink{Name: "entrypoint2"},
} }
assert.Equal(t, want, got) if !reflect.DeepEqual(want, got) {
t.Errorf("tarball mismatch. want: %+v, got: %+v", want, got)
}
} }
func TestGzip(t *testing.T) { func TestGzip(t *testing.T) {
@ -34,10 +35,14 @@ func TestGzip(t *testing.T) {
tbuf := tb.Buffer() tbuf := tb.Buffer()
tgz, err := gzip.NewReader(tb.Gzip()) tgz, err := gzip.NewReader(tb.Gzip())
require.NoError(t, err) if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var uncompressed bytes.Buffer var uncompressed bytes.Buffer
_, err = io.Copy(&uncompressed, tgz) if _, err := io.Copy(&uncompressed, tgz); err != nil {
require.NoError(t, err) t.Fatalf("unexpected error: %v", err)
assert.Equal(t, tbuf.Bytes(), uncompressed.Bytes()) }
if !bytes.Equal(tbuf.Bytes(), uncompressed.Bytes()) {
t.Errorf("tbuf and uncompressed bytes mismatch")
}
} }

View File

@ -5,10 +5,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"regexp"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestExecute(t *testing.T) { func TestExecute(t *testing.T) {
@ -26,7 +24,9 @@ func TestExecute(t *testing.T) {
infile: "t10-in.txt", infile: "t10-in.txt",
fixture: func(t *testing.T, dir string) { fixture: func(t *testing.T, dir string) {
fname := filepath.Join(dir, "t10-in.txt") fname := filepath.Join(dir, "t10-in.txt")
require.NoError(t, ioutil.WriteFile(fname, _foo, 0644)) if err := ioutil.WriteFile(fname, _foo, 0644); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}, },
outfile: "-", outfile: "-",
}, },
@ -35,7 +35,9 @@ func TestExecute(t *testing.T) {
infile: "t20-in.txt", infile: "t20-in.txt",
fixture: func(t *testing.T, dir string) { fixture: func(t *testing.T, dir string) {
fname := filepath.Join(dir, "t20-in.txt") fname := filepath.Join(dir, "t20-in.txt")
require.NoError(t, ioutil.WriteFile(fname, _foo, 0644)) if err := ioutil.WriteFile(fname, _foo, 0644); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}, },
outfile: "t20-out.txt", outfile: "t20-out.txt",
}, },
@ -67,19 +69,30 @@ func TestExecute(t *testing.T) {
err := c.execute(inf, tt.outfile) err := c.execute(inf, tt.outfile)
if tt.wantErr != "" { if tt.wantErr != "" {
require.Error(t, err) if err == nil {
assert.Regexp(t, tt.wantErr, err.Error()) t.Fatal("expected error, got nil")
}
r := regexp.MustCompile(tt.wantErr)
if r.FindStringIndex(err.Error()) == nil {
t.Errorf("%s not found in %s", tt.wantErr, err.Error())
}
return return
} }
var out []byte var out []byte
require.NoError(t, err) if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.outfile == "-" { if tt.outfile == "-" {
out = stdout.Bytes() out = stdout.Bytes()
} else { } else {
out, err = ioutil.ReadFile(tt.outfile) out, err = ioutil.ReadFile(tt.outfile)
require.NoError(t, err) if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
if !bytes.Equal([]byte("foo foo"), out) {
t.Errorf("out != foo foo: %s", string(out))
} }
assert.Equal(t, []byte("foo foo"), out)
}) })
} }
} }

View File

@ -4,11 +4,10 @@ import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"encoding/json" "encoding/json"
"reflect"
"testing" "testing"
"git.sr.ht/~motiejus/code/undocker/internal/tartest" "git.sr.ht/~motiejus/code/undocker/internal/tartest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
type ( type (
@ -193,13 +192,22 @@ func TestRootFS(t *testing.T) {
err := Flatten(in, &out) err := Flatten(in, &out)
if tt.wantErr != "" { if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr) if err == nil {
t.Fatal("expected error, got nil")
}
if tt.wantErr != err.Error() {
t.Errorf("want != got: %s != %s", tt.wantErr, err.Error())
}
return return
} }
outb := out.Bytes() outb := out.Bytes()
require.NoError(t, err) if err != nil {
t.Fatal("expected error, got nil")
}
got := tartest.Extract(t, bytes.NewReader(outb)) got := tartest.Extract(t, bytes.NewReader(outb))
assert.Equal(t, tt.want, got) if !reflect.DeepEqual(tt.want, got) {
t.Errorf("want != got: %v != %v", tt.want, got)
}
}) })
} }
} }

View File

@ -37,15 +37,17 @@ func TestTree(t *testing.T) {
for _, path := range tt.matchTrue { for _, path := range tt.matchTrue {
t.Run(path, func(t *testing.T) { t.Run(path, func(t *testing.T) {
assert.True(t, tree.HasPrefix(path), if !tree.HasPrefix(path) {
"expected %s to be a prefix of %s", path, tree) t.Errorf("expected %s to be a prefix of %s", path, tree)
}
}) })
} }
for _, path := range tt.matchFalse { for _, path := range tt.matchFalse {
t.Run(path, func(t *testing.T) { t.Run(path, func(t *testing.T) {
assert.False(t, tree.HasPrefix(path), if tree.HasPrefix(path) {
"expected %s to not be a prefix of %s", path, tree) t.Errorf("expected %s to not be a prefix of %s", path, tree)
}
}) })
} }
}) })
@ -56,8 +58,14 @@ func TestTreeMerge(t *testing.T) {
tree1 := newTree("bin/ar", "var/cache/apt") tree1 := newTree("bin/ar", "var/cache/apt")
tree2 := newTree("bin/ar", "bin/busybox", "usr/share/doc") tree2 := newTree("bin/ar", "bin/busybox", "usr/share/doc")
tree1.Merge(tree2) tree1.Merge(tree2)
assert.Equal(t, "./bin/ar:./bin/busybox:./usr/share/doc:./var/cache/apt", tree1.String()) want1 := "./bin/ar:./bin/busybox:./usr/share/doc:./var/cache/apt"
assert.Equal(t, "./bin/ar:./bin/busybox:./usr/share/doc", tree2.String()) if got := tree1.String(); want1 != got {
t.Errorf("want != got: %q != %q", want1, got)
}
want2 := "./bin/ar:./bin/busybox:./usr/share/doc"
if got := tree2.String(); want2 != got {
t.Errorf("want != got: %q != %q", want2, got)
}
} }
func TestString(t *testing.T) { func TestString(t *testing.T) {