Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline test setup #14

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions profile/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,40 @@ import (
"testing"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"

"github.com/buildpacks/profile/profile"

"github.com/buildpacks/libcnb"
)

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"
Expand All @@ -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{
Expand Down
29 changes: 11 additions & 18 deletions profile/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down