diff --git a/profile/build_test.go b/profile/build_test.go index 3cdcfc4..97b260a 100644 --- a/profile/build_test.go +++ b/profile/build_test.go @@ -22,7 +22,6 @@ import ( "testing" . "github.com/onsi/gomega" - "github.com/onsi/gomega/types" "github.com/buildpacks/profile/profile" @@ -30,28 +29,33 @@ import ( ) type BuildTest struct { + test *testing.T context libcnb.BuildContext - expect func(actual interface{}, extra ...interface{}) types.Assertion + expect ExpectFunc } -func (b BuildTest) SetupGomega(t *testing.T) BuildTest { - b.expect = NewGomegaWithT(t).Expect - return b +func NewBuildTestBuilder(t *testing.T) BuildTest { + return BuildTest{ + test: t, + expect: NewGomegaWithT(t).Expect, + } } func (b BuildTest) SetupWorkspace() BuildTest { var err error + b.context.Buildpack.Path = "../" - b.context.ApplicationPath, err = os.MkdirTemp("", "profile") - b.expect(err).NotTo(HaveOccurred()) - profilePath := filepath.Join(b.context.ApplicationPath, ".profile") + b.context.ApplicationPath = b.test.TempDir() + b.context.Layers.Path = b.test.TempDir() + + err = os.MkdirAll(b.context.Layers.Path, os.ModePerm) + b.expect(err).ToNot(HaveOccurred()) + profilePath := filepath.Join(b.context.ApplicationPath, ".profile") f, err := os.Create(profilePath) b.expect(err).NotTo(HaveOccurred()) defer f.Close() - b.expect(err).To(BeNil()) - _, err = f.WriteString( ` echo "Hello world" @@ -64,24 +68,13 @@ export HELLO return b } -func (b BuildTest) RemoveWorkspace() BuildTest { - b.expect(os.RemoveAll(b.context.ApplicationPath)).To(Succeed()) - return b -} - -func (b BuildTest) Build() (libcnb.BuildContext, ExpectFunc, AfterFunc) { - outputPath, _ := os.MkdirTemp("", "dotprofile_out") - os.MkdirAll(outputPath, os.ModePerm) - b.context.Layers.Path = outputPath - return b.context, b.expect, func() { - b.RemoveWorkspace() - os.RemoveAll(outputPath) - } +func (b BuildTest) Build() (libcnb.BuildContext, ExpectFunc) { + return b.context, b.expect } func TestBuildExecutes(t *testing.T) { - ctx, Expect, After := BuildTest{}.SetupGomega(t).SetupWorkspace().Build() - defer After() + ctx, Expect := NewBuildTestBuilder(t).SetupWorkspace().Build() + Expect(profile.Build(ctx)).To(Equal(libcnb.BuildResult{ Layers: []libcnb.Layer{{ LayerTypes: libcnb.LayerTypes{ diff --git a/profile/detect_test.go b/profile/detect_test.go index d50ae7e..45af5b1 100644 --- a/profile/detect_test.go +++ b/profile/detect_test.go @@ -30,44 +30,37 @@ import ( ) type ExpectFunc func(actual interface{}, extra ...interface{}) types.Assertion -type AfterFunc func() type DetectTest struct { + test *testing.T context libcnb.DetectContext expect ExpectFunc } -func (d DetectTest) SetupGomega(t *testing.T) DetectTest { - d.expect = NewGomegaWithT(t).Expect - return d +func NewDetectTestBuilder(t *testing.T) DetectTest { + return DetectTest{ + test: t, + expect: NewGomegaWithT(t).Expect, + } } func (d DetectTest) SetupWorkspace() DetectTest { - var err error - d.context.ApplicationPath, err = os.MkdirTemp("", "profile") - d.expect(err).NotTo(HaveOccurred()) - return d -} - -func (d DetectTest) RemoveWorkspace() DetectTest { - d.expect(os.RemoveAll(d.context.ApplicationPath)).To(Succeed()) + d.context.ApplicationPath = d.test.TempDir() return d } -func (d DetectTest) Build() (libcnb.DetectContext, ExpectFunc, AfterFunc) { - return d.context, d.expect, func() { d.RemoveWorkspace() } +func (d DetectTest) Build() (libcnb.DetectContext, ExpectFunc) { + return d.context, d.expect } func TestDetectFailsWithoutProfileScript(t *testing.T) { - ctx, Expect, After := DetectTest{}.SetupGomega(t).SetupWorkspace().Build() - defer After() + ctx, Expect := NewDetectTestBuilder(t).SetupWorkspace().Build() Expect(profile.Detect(ctx)).To(Equal(libcnb.DetectResult{})) } func TestDetectPassesWithProfileScript(t *testing.T) { - ctx, Expect, After := DetectTest{}.SetupGomega(t).SetupWorkspace().Build() - defer After() + ctx, Expect := NewDetectTestBuilder(t).SetupWorkspace().Build() Expect(os.WriteFile(filepath.Join(ctx.ApplicationPath, ".profile"), []byte(`echo "Hello World!"`), 0600))