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

@ -1,11 +1,13 @@
package mtree
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vbatts/go-mtree/xattr"
)
@ -19,73 +21,47 @@ func TestXattrUpdate(t *testing.T) {
// a bit dirty to create/destroy a directory in cwd, but often /tmp is
// mounted tmpfs and doesn't support xattrs
dir, err := os.MkdirTemp(".", "test.xattr.restore.")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(dir) // clean up
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))
if err := xattr.Set(dir, "user.test", []byte("directory")); err != nil {
t.Skipf("skipping: %q does not support xattrs", dir)
}
if err := xattr.Set(tmpfn, "user.test", []byte("regular file")); err != nil {
t.Fatal(err)
}
require.NoError(t, xattr.Set(tmpfn, "user.test", []byte("regular file")))
// Walk this tempdir
dh, err := Walk(dir, nil, append(DefaultKeywords, []Keyword{"xattr", "sha1"}...), nil)
if err != nil {
t.Fatal(err)
}
require.NoErrorf(t, err, "walk %s", dir)
// Now check that we're sane
res, err := Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) != 0 {
t.Errorf("expecting no failures, but got %q", res)
require.NoErrorf(t, err, "check %s", dir)
if !assert.Empty(t, res) {
pprintInodeDeltas(t, res)
}
if err := xattr.Set(tmpfn, "user.test", []byte("let it fly")); err != nil {
t.Fatal(err)
}
require.NoError(t, xattr.Set(tmpfn, "user.test", []byte("let it fly")))
// Now check that we fail the check
res, err = Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) == 0 {
t.Error("expected failures (like xattrs), but got none")
}
require.NoErrorf(t, err, "check %s", dir)
assert.NotEmpty(t, res, "should see xattr deltas from check")
// restore the xattrs to original
res, err = Update(dir, dh, append(DefaultUpdateKeywords, "xattr"), nil)
if err != nil {
t.Error(err)
}
if len(res) != 0 {
t.Errorf("expecting no failures, but got %q", res)
require.NoErrorf(t, err, "update %s", dir)
if !assert.Empty(t, res, "update implied check") {
pprintInodeDeltas(t, res)
}
// Now check that we're sane again
res, err = Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) != 0 {
// pretty this shit up
buf, err := json.MarshalIndent(res, "", " ")
if err != nil {
t.Errorf("expecting no failures, but got %q", res)
} else {
t.Errorf("expecting no failures, but got %s", string(buf))
}
require.NoErrorf(t, err, "update %s", dir)
if !assert.Empty(t, res, "post-update check") {
pprintInodeDeltas(t, res)
}
// TODO make a test for xattr here. Likely in the user space for privileges. Even still this may be prone to error for some tmpfs don't act right with xattrs. :-\