aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/exec_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-01-06 18:06:56 +0900
committerOphestra <cat@gensokyo.uk>2026-01-06 18:06:56 +0900
commit0741a614edc4b67fc849f6c1688bc77139cd1e80 (patch)
tree4e2ecd48fe1dcc5cef3895bfab3eeb84fa29d361 /internal/pkg/exec_test.go
parente7e9b4caea779d2f24be8f213d45b5fa6282db00 (diff)
internal/pkg: relocate testtool workaround
This can be reused in other test cases. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/exec_test.go')
-rw-r--r--internal/pkg/exec_test.go89
1 files changed, 50 insertions, 39 deletions
diff --git a/internal/pkg/exec_test.go b/internal/pkg/exec_test.go
index 25fe8894..0ac06010 100644
--- a/internal/pkg/exec_test.go
+++ b/internal/pkg/exec_test.go
@@ -27,26 +27,9 @@ func TestExec(t *testing.T) {
t.Parallel()
checkWithCache(t, []cacheTestCase{
- {"container", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
+ {"offline", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
c.SetStrict(true)
-
- // this is built during go:generate and is not deterministic
- testtool := overrideIdent{pkg.ID{0xfe, 0xff}, stubArtifact{
- kind: pkg.KindTar,
- cure: func(c *pkg.CureContext) error {
- work := c.GetWorkDir()
- if err := os.MkdirAll(
- work.Append("bin").String(),
- 0700,
- ); err != nil {
- return err
- }
- return os.WriteFile(c.GetWorkDir().Append(
- "bin",
- "testtool",
- ).String(), testtoolBin, 0500)
- },
- }}
+ testtool, testtoolDestroy := newTesttool()
msg := message.New(log.New(os.Stderr, "container: ", 0))
msg.SwapVerbose(testing.Verbose())
@@ -125,26 +108,54 @@ func TestExec(t *testing.T) {
t.Fatalf("Cure: error = %v, want init exit status 1", err)
}
- // the testtool is not deterministic
- if pathname, checksum, err := c.Cure(testtool); err != nil {
- t.Fatalf("Cure: error = %v", err)
- } else if err = os.Remove(pathname.String()); err != nil {
- t.Fatal(err)
- } else {
- p := base.Append(
- "checksum",
- pkg.Encode(checksum),
- )
- if err = os.Chmod(p.Append("bin").String(), 0700); err != nil {
- t.Fatal(err)
- }
- if err = os.Chmod(p.String(), 0700); err != nil {
- t.Fatal(err)
- }
- if err = os.RemoveAll(p.String()); err != nil {
- t.Fatal(err)
- }
- }
+ testtoolDestroy(t, base, c)
}, pkg.MustDecode("7PoPpWLjFPXIymbuIYLZAzOpCYr-2PN4CZ11jFdO-mDlnZNgFO3JyOtK8HW8Jxvm")},
})
}
+
+// newTesttool returns an [Artifact] that cures into testtoolBin. The returned
+// function must be called at the end of the test but not deferred.
+func newTesttool() (
+ testtool pkg.Artifact,
+ testtoolDestroy func(t *testing.T, base *check.Absolute, c *pkg.Cache),
+) {
+ // testtoolBin is built during go:generate and is not deterministic
+ testtool = overrideIdent{pkg.ID{0xfe, 0xff}, stubArtifact{
+ kind: pkg.KindTar,
+ cure: func(c *pkg.CureContext) error {
+ work := c.GetWorkDir()
+ if err := os.MkdirAll(
+ work.Append("bin").String(),
+ 0700,
+ ); err != nil {
+ return err
+ }
+ return os.WriteFile(c.GetWorkDir().Append(
+ "bin",
+ "testtool",
+ ).String(), testtoolBin, 0500)
+ },
+ }}
+ testtoolDestroy = func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
+ if pathname, checksum, err := c.Cure(testtool); err != nil {
+ t.Fatalf("Cure: error = %v", err)
+ } else if err = os.Remove(pathname.String()); err != nil {
+ t.Fatal(err)
+ } else {
+ p := base.Append(
+ "checksum",
+ pkg.Encode(checksum),
+ )
+ if err = os.Chmod(p.Append("bin").String(), 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err = os.Chmod(p.String(), 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err = os.RemoveAll(p.String()); err != nil {
+ t.Fatal(err)
+ }
+ }
+ }
+ return
+}