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"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var mockTime = time.Unix(1337888823, 0)
@ -76,87 +78,49 @@ func TestCheckFsEval(t *testing.T) {
content := []byte("If you hide your ignorance, no one will hit you and you'll never learn.")
tmpfn := filepath.Join(dir, "tmpfile")
if err := os.WriteFile(tmpfn, content, 0451); err != nil {
t.Fatal(err)
}
require.NoError(t, os.WriteFile(tmpfn, content, 0451))
// Walk this tempdir
mock := &MockFsEval{}
dh, err := Walk(dir, nil, append(DefaultKeywords, "sha1"), mock)
if err != nil {
t.Fatal(err)
}
require.NoErrorf(t, err, "walk %s (mock FsEval)", dir)
// Make sure that mock functions have been called.
if mock.open == 0 {
t.Errorf("mock.Open not called")
}
if mock.lstat == 0 {
t.Errorf("mock.Lstat not called")
}
if mock.readdir == 0 {
t.Errorf("mock.Readdir not called")
}
if mock.keywordFunc == 0 {
t.Errorf("mock.KeywordFunc not called")
}
assert.NotZero(t, mock.open, "mock.Open not called")
assert.NotZero(t, mock.lstat, "mock.Lstat not called")
assert.NotZero(t, mock.readdir, "mock.Readdir not called")
assert.NotZero(t, mock.keywordFunc, "mock.KeywordFunc not called")
// Check for sanity. This ought to pass.
mock = &MockFsEval{}
res, err := Check(dir, dh, nil, mock)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
t.Errorf("%#v", res)
require.NoErrorf(t, err, "check %s (mock FsEval)", dir)
if !assert.Empty(t, res) {
pprintInodeDeltas(t, res)
}
// Make sure that mock functions have been called.
if mock.open == 0 {
t.Errorf("mock.Open not called")
}
if mock.lstat == 0 {
t.Errorf("mock.Lstat not called")
}
if mock.readdir == 0 {
t.Errorf("mock.Readdir not called")
}
if mock.keywordFunc == 0 {
t.Errorf("mock.KeywordFunc not called")
}
assert.NotZero(t, mock.open, "mock.Open not called")
assert.NotZero(t, mock.lstat, "mock.Lstat not called")
assert.NotZero(t, mock.readdir, "mock.Readdir not called")
assert.NotZero(t, mock.keywordFunc, "mock.KeywordFunc not called")
// This should FAIL.
res, err = Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) == 0 {
t.Errorf("expected Check to fail")
require.NoErrorf(t, err, "walk %s", dir)
if !assert.NotEmpty(t, res) {
pprintInodeDeltas(t, res)
}
// Modify the metadata so you can get the right output.
if err := os.Chmod(tmpfn, 0777); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(tmpfn, mockTime, mockTime); err != nil {
t.Fatal(err)
}
if err := os.Chmod(dir, 0777); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(dir, mockTime, mockTime); err != nil {
t.Fatal(err)
}
require.NoError(t, os.Chmod(tmpfn, 0777))
require.NoError(t, os.Chtimes(tmpfn, mockTime, mockTime))
require.NoError(t, os.Chmod(dir, 0777))
require.NoError(t, os.Chtimes(dir, mockTime, mockTime))
// It should now succeed.
res, err = Check(dir, dh, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
buf, err := json.MarshalIndent(res, "", " ")
if err != nil {
t.Errorf("%#v", res)
} else {
t.Errorf("%s", buf)
}
require.NoErrorf(t, err, "check %s", dir)
if !assert.Empty(t, res) {
pprintInodeDeltas(t, res)
}
}