1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2025-10-04 04:31:00 +00:00

test: migrate most tests to testify

testify makes most bog-standard test checks much easier to read and
maintain, and is quite widely used. It wasn't really well known back
when go-mtree was first written, but the migration is fairly
straight-forward for most tests.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai 2025-09-20 03:00:06 +10:00
parent f2b48a0e2f
commit 3252a4ad82
No known key found for this signature in database
GPG key ID: 2897FAD2B7E9446F
55 changed files with 22620 additions and 886 deletions

View file

@ -6,23 +6,22 @@ import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// simple walk of current directory, and immediately check it.
// may not be parallelizable.
func TestCheck(t *testing.T) {
dh, err := Walk(".", nil, append(DefaultKeywords, []Keyword{"sha1", "xattr"}...), nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "walk .")
res, err := Check(".", dh, nil, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "check .")
if len(res) > 0 {
t.Errorf("%#v", res)
if !assert.Empty(t, res, "check after no changes should have no diff") {
pprintInodeDeltas(t, res)
}
}
@ -33,50 +32,46 @@ func TestCheckKeywords(t *testing.T) {
dir := t.TempDir()
tmpfn := filepath.Join(dir, "tmpfile")
if err := os.WriteFile(tmpfn, content, 0666); err != nil {
t.Fatal(err)
}
require.NoError(t, os.WriteFile(tmpfn, content, 0666))
// Walk this tempdir
dh, err := Walk(dir, nil, append(DefaultKeywords, "sha1"), nil)
if err != nil {
t.Fatal(err)
}
require.NoErrorf(t, err, "walk %s", dir)
// Check for sanity. This ought to pass.
res, err := Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
t.Errorf("%#v", res)
require.NoErrorf(t, err, "check %s", dir)
if !assert.Empty(t, res, "check after no changes should have no diff") {
pprintInodeDeltas(t, res)
}
// Touch a file, so the mtime changes.
newtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC)
if err := os.Chtimes(tmpfn, newtime, newtime); err != nil {
t.Fatal(err)
}
require.NoError(t, os.Chtimes(tmpfn, newtime, newtime))
// Check again. This ought to fail.
res, err = Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) != 1 {
t.Fatal("expected to get 1 delta on changed mtimes, but did not")
}
if res[0].Type() != Modified {
t.Errorf("expected to get modified delta on changed mtimes, but did not")
require.NoErrorf(t, err, "check %s", dir)
if assert.NotEmpty(t, res, "should get a delta after mtime change") {
assert.Len(t, res, 1, "should only be one changed file entry")
diff := res[0]
assert.Equal(t, Modified, diff.Type(), "expected to get modified entry")
kds := diff.Diff()
if assert.Len(t, kds, 1, "should have one key different after mtime change") {
kd := kds[0]
assert.Equal(t, Modified, kd.Type(), "after mtime change key delta type should be modified")
assert.Equal(t, Keyword("time"), kd.Name(), "after mtime change key delta should be 'time'")
assert.NotNil(t, kd.Old(), "after mtime change key delta Old")
assert.NotNil(t, kd.New(), "after mtime change key delta New")
}
}
// Check again, but only sha1 and mode. This ought to pass.
res, err = Check(dir, dh, []Keyword{"sha1", "mode"}, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
t.Errorf("%#v", res)
require.NoErrorf(t, err, "check .", err)
if !assert.Empty(t, res, "check (~time) should have no diff") {
pprintInodeDeltas(t, res)
}
}
@ -98,18 +93,16 @@ func ExampleCheck() {
// Tests default action for evaluating a symlink, which is just to compare the
// link itself, not to follow it
func TestDefaultBrokenLink(t *testing.T) {
dh, err := Walk("./testdata/dirwithbrokenlink", nil, append(DefaultKeywords, "sha1"), nil)
if err != nil {
t.Fatal(err)
}
res, err := Check("./testdata/dirwithbrokenlink", dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
for _, delta := range res {
t.Error(delta)
}
dir := "./testdata/dirwithbrokenlink"
dh, err := Walk(dir, nil, append(DefaultKeywords, "sha1"), nil)
require.NoErrorf(t, err, "walk %s", dir)
res, err := Check(dir, dh, nil, nil)
require.NoErrorf(t, err, "check %s", dir)
if !assert.Empty(t, res, "check after no changes should have no diff") {
pprintInodeDeltas(t, res)
}
}
@ -126,32 +119,21 @@ func TestTimeComparison(t *testing.T) {
`
fh, err := os.Create(filepath.Join(dir, "file"))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// This is what mode we're checking for. Round integer of epoch seconds
epoch := time.Unix(5, 0)
if err := os.Chtimes(fh.Name(), epoch, epoch); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(dir, epoch, epoch); err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
require.NoError(t, os.Chtimes(fh.Name(), epoch, epoch))
require.NoError(t, os.Chtimes(dir, epoch, epoch))
require.NoError(t, fh.Close())
dh, err := ParseSpec(bytes.NewBufferString(spec))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "parse specfile")
res, err := Check(dir, dh, nil, nil)
if err != nil {
t.Error(err)
}
if len(res) > 0 {
t.Fatal(res)
require.NoErrorf(t, err, "check %s against spec", dir)
if !assert.Empty(t, res) {
pprintInodeDeltas(t, res)
}
}
@ -167,41 +149,30 @@ func TestTarTime(t *testing.T) {
`
fh, err := os.Create(filepath.Join(dir, "file"))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// This is what mode we're checking for. Round integer of epoch seconds
epoch := time.Unix(5, 0)
if err := os.Chtimes(fh.Name(), epoch, epoch); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(dir, epoch, epoch); err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
require.NoError(t, os.Chtimes(fh.Name(), epoch, epoch))
require.NoError(t, os.Chtimes(dir, epoch, epoch))
require.NoError(t, fh.Close())
dh, err := ParseSpec(bytes.NewBufferString(spec))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "parse specfile")
keywords := dh.UsedKeywords()
assert.ElementsMatch(t, keywords, []Keyword{"type", "time"}, "UsedKeywords")
// make sure "time" keyword works
_, err = Check(dir, dh, keywords, nil)
if err != nil {
t.Error(err)
}
res1, err := Check(dir, dh, keywords, nil)
require.NoErrorf(t, err, "check %s (UsedKeywords)", dir)
assert.NotEmpty(t, res1, "check should have errors when time mismatched")
// make sure tar_time wins
res, err := Check(dir, dh, append(keywords, "tar_time"), nil)
if err != nil {
t.Error(err)
}
if len(res) > 0 {
t.Fatal(res)
res2, err := Check(dir, dh, append(keywords, "tar_time"), nil)
require.NoErrorf(t, err, "check %s (UsedKeywords + tar_time)", dir)
if !assert.Empty(t, res2, "tar_time should check against truncated timestamp") {
pprintInodeDeltas(t, res2)
}
}
@ -217,33 +188,20 @@ func TestIgnoreComments(t *testing.T) {
`
fh, err := os.Create(filepath.Join(dir, "file1"))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// This is what mode we're checking for. Round integer of epoch seconds
epoch := time.Unix(5, 0)
if err := os.Chtimes(fh.Name(), epoch, epoch); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(dir, epoch, epoch); err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
require.NoError(t, os.Chtimes(fh.Name(), epoch, epoch))
require.NoError(t, os.Chtimes(dir, epoch, epoch))
require.NoError(t, fh.Close())
dh, err := ParseSpec(bytes.NewBufferString(spec))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "parse specfile")
res, err := Check(dir, dh, nil, nil)
if err != nil {
t.Error(err)
}
if len(res) > 0 {
t.Fatal(res)
require.NoErrorf(t, err, "check %s", dir)
if !assert.Empty(t, res) {
pprintInodeDeltas(t, res)
}
// now change the spec to a comment that looks like an actual Entry but has
@ -256,17 +214,12 @@ func TestIgnoreComments(t *testing.T) {
..
`
dh, err = ParseSpec(bytes.NewBufferString(spec))
if err != nil {
t.Error(err)
}
require.NoError(t, err, "parse specfile")
res, err = Check(dir, dh, nil, nil)
if err != nil {
t.Error(err)
}
if len(res) > 0 {
t.Fatal(res)
require.NoErrorf(t, err, "check %s", dir)
if !assert.Empty(t, res) {
pprintInodeDeltas(t, res)
}
}
@ -274,29 +227,19 @@ func TestCheckNeedsEncoding(t *testing.T) {
dir := t.TempDir()
fh, err := os.Create(filepath.Join(dir, "file[ "))
if err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
require.NoError(t, err)
require.NoError(t, fh.Close())
fh, err = os.Create(filepath.Join(dir, " , should work"))
if err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
require.NoError(t, err)
require.NoError(t, fh.Close())
dh, err := Walk(dir, nil, DefaultKeywords, nil)
if err != nil {
t.Fatal(err)
}
require.NoErrorf(t, err, "walk %s", dir)
res, err := Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
t.Fatal(res)
require.NoErrorf(t, err, "check %s", dir)
if !assert.Empty(t, res) {
pprintInodeDeltas(t, res)
}
}