internal/pkg: relocate cache test helper
All checks were successful
Test / Flake checks (push) Successful in 1m37s
Test / Create distribution (push) Successful in 43s
Test / Sandbox (push) Successful in 2m28s
Test / Hakurei (push) Successful in 3m28s
Test / ShareFS (push) Successful in 3m24s
Test / Hpkg (push) Successful in 4m11s
Test / Sandbox (race detector) (push) Successful in 4m46s
Test / Hakurei (race detector) (push) Successful in 5m37s
All checks were successful
Test / Flake checks (push) Successful in 1m37s
Test / Create distribution (push) Successful in 43s
Test / Sandbox (push) Successful in 2m28s
Test / Hakurei (push) Successful in 3m28s
Test / ShareFS (push) Successful in 3m24s
Test / Hpkg (push) Successful in 4m11s
Test / Sandbox (race detector) (push) Successful in 4m46s
Test / Hakurei (race detector) (push) Successful in 5m37s
This is useful for other tests that need a cache instance. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -17,6 +17,64 @@ import (
|
|||||||
"hakurei.app/internal/pkg"
|
"hakurei.app/internal/pkg"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// cacheTestCase is a test case passed to checkWithCache where a new instance
|
||||||
|
// of [pkg.Cache] is prepared for the test case, and is validated and removed
|
||||||
|
// on test completion.
|
||||||
|
type cacheTestCase struct {
|
||||||
|
name string
|
||||||
|
early func(t *testing.T, base *check.Absolute)
|
||||||
|
f func(t *testing.T, base *check.Absolute, c *pkg.Cache)
|
||||||
|
want pkg.Checksum
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkWithCache runs a slice of cacheTestCase.
|
||||||
|
func checkWithCache(t *testing.T, testCases []cacheTestCase) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := check.MustAbs(t.TempDir())
|
||||||
|
if err := os.Chmod(base.String(), 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := filepath.WalkDir(base.String(), func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !d.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return os.Chmod(path, 0700)
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if c, err := pkg.New(base); err != nil {
|
||||||
|
t.Fatalf("New: error = %v", err)
|
||||||
|
} else {
|
||||||
|
if tc.early != nil {
|
||||||
|
tc.early(t, base)
|
||||||
|
}
|
||||||
|
tc.f(t, base, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
if checksum, err := pkg.HashDir(base); err != nil {
|
||||||
|
t.Fatalf("HashDir: error = %v", err)
|
||||||
|
} else if checksum != tc.want {
|
||||||
|
t.Fatalf("HashDir: %v", &pkg.ChecksumMismatchError{
|
||||||
|
Got: checksum,
|
||||||
|
Want: tc.want,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCache(t *testing.T) {
|
func TestCache(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -35,12 +93,7 @@ func TestCache(t *testing.T) {
|
|||||||
|
|
||||||
testdataChecksumString := base64.URLEncoding.EncodeToString(testdataChecksum[:])
|
testdataChecksumString := base64.URLEncoding.EncodeToString(testdataChecksum[:])
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []cacheTestCase{
|
||||||
name string
|
|
||||||
early func(t *testing.T, base *check.Absolute)
|
|
||||||
f func(t *testing.T, base *check.Absolute, c *pkg.Cache)
|
|
||||||
check func(t *testing.T, base *check.Absolute)
|
|
||||||
}{
|
|
||||||
{"file", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
|
{"file", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
|
||||||
wantErrNonexistent := &os.PathError{
|
wantErrNonexistent := &os.PathError{
|
||||||
Op: "open",
|
Op: "open",
|
||||||
@@ -209,54 +262,9 @@ func TestCache(t *testing.T) {
|
|||||||
if _, _, err := c.LoadFile(zeroIdent); !reflect.DeepEqual(err, wantErrNonexistentZero) {
|
if _, _, err := c.LoadFile(zeroIdent); !reflect.DeepEqual(err, wantErrNonexistentZero) {
|
||||||
t.Fatalf("LoadFile: error = %#v, want %#v", err, wantErrNonexistentZero)
|
t.Fatalf("LoadFile: error = %#v, want %#v", err, wantErrNonexistentZero)
|
||||||
}
|
}
|
||||||
}, func(t *testing.T, base *check.Absolute) {
|
}, pkg.MustDecode("ZNSQH-mjhtIbFvi51lQ0UjatjoS8_5ILrBPNWlO2LWTq9P6MJEnekYzP0esUJnVr")},
|
||||||
wantChecksum := pkg.MustDecode(
|
|
||||||
"ZNSQH-mjhtIbFvi51lQ0UjatjoS8_5ILrBPNWlO2LWTq9P6MJEnekYzP0esUJnVr",
|
|
||||||
)
|
|
||||||
if checksum, err := pkg.HashDir(base); err != nil {
|
|
||||||
t.Fatalf("HashDir: error = %v", err)
|
|
||||||
} else if checksum != wantChecksum {
|
|
||||||
t.Fatalf("HashDir: %v", &pkg.ChecksumMismatchError{
|
|
||||||
Got: checksum,
|
|
||||||
Want: wantChecksum,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}},
|
|
||||||
}
|
|
||||||
for _, tc := range testCases {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
base := check.MustAbs(t.TempDir())
|
|
||||||
if err := os.Chmod(base.String(), 0700); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
if err := filepath.WalkDir(base.String(), func(path string, d fs.DirEntry, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if !d.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return os.Chmod(path, 0700)
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if c, err := pkg.New(base); err != nil {
|
|
||||||
t.Fatalf("New: error = %v", err)
|
|
||||||
} else {
|
|
||||||
if tc.early != nil {
|
|
||||||
tc.early(t, base)
|
|
||||||
}
|
|
||||||
tc.f(t, base, c)
|
|
||||||
tc.check(t, base)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
checkWithCache(t, testCases)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrors(t *testing.T) {
|
func TestErrors(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user