From 0741a614edc4b67fc849f6c1688bc77139cd1e80 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Tue, 6 Jan 2026 18:06:56 +0900 Subject: [PATCH] internal/pkg: relocate testtool workaround This can be reused in other test cases. Signed-off-by: Ophestra --- internal/pkg/exec_test.go | 89 ++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/internal/pkg/exec_test.go b/internal/pkg/exec_test.go index 25fe889..0ac0601 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 +}