Skip to content

Commit

Permalink
Fail to build when buildpack.yml is present (#285)
Browse files Browse the repository at this point in the history
* fail detection when bp-yml is present

per [Go Build RFC 0003](https://github.com/paketo-buildpacks/rfcs/blob/d4fde0656a823330f78a77e69a81d2d6f7768daf/text/go/go-build/0003-buildpack-yml-to-env-var.md)

* add integration test asserting build failure

* use fs.Exists()
  • Loading branch information
Frankie G-J authored Mar 8, 2022
1 parent e2ca5f2 commit 46313a7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
11 changes: 9 additions & 2 deletions build_configuration_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/mattn/go-shellwords"
"github.com/paketo-buildpacks/packit/v2/fs"
)

//go:generate faux --interface TargetManager --output fakes/target_manager.go
Expand All @@ -32,13 +33,19 @@ func NewBuildConfigurationParser(targetManager TargetManager) BuildConfiguration
}

func (p BuildConfigurationParser) Parse(buildpackVersion, workingDir string) (BuildConfiguration, error) {
var buildConfiguration BuildConfiguration
bpYML, err := fs.Exists(filepath.Join(workingDir, "buildpack.yml"))
if err != nil {
return BuildConfiguration{}, fmt.Errorf("failed to check for buildpack.yml: %w", err)
}
if bpYML {
return BuildConfiguration{}, fmt.Errorf("working directory contains deprecated 'buildpack.yml'; use environment variables for configuration")
}

var buildConfiguration BuildConfiguration
if val, ok := os.LookupEnv("BP_GO_TARGETS"); ok {
buildConfiguration.Targets = filepath.SplitList(val)
}

var err error
if len(buildConfiguration.Targets) > 0 {
buildConfiguration.Targets, err = p.targetManager.CleanAndValidate(buildConfiguration.Targets, workingDir)
if err != nil {
Expand Down
27 changes: 23 additions & 4 deletions build_configuration_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gobuild_test
import (
"errors"
"os"
"path/filepath"
"testing"

gobuild "github.com/paketo-buildpacks/go-build"
Expand Down Expand Up @@ -195,18 +196,36 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {
})

context("failure cases", func() {
context("when the working directory contains a buildpack.yml", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := parser.Parse("1.2.3", workingDir)
Expect(err).To(MatchError("working directory contains deprecated 'buildpack.yml'; use environment variables for configuration"))
})
context("and it's not readable", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})

it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})
it("returns the error from stat", func() {
_, err := parser.Parse("1.2.3", workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
context("go targets fail to be cleaned an validated", func() {
it.Before(func() {
os.Setenv("BP_GO_TARGETS", "./some/target")

targetManager.CleanAndValidateCall.Returns.Error = errors.New("failed to clean and validate targets")

})

it.After(func() {
os.Unsetenv("BP_GO_TARGETS")
})

it("returns an error", func() {
_, err := parser.Parse("1.2.3", workingDir)
Expect(err).To(MatchError("failed to clean and validate targets"))
Expand Down
41 changes: 41 additions & 0 deletions integration/build_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,45 @@ func testBuildFailure(t *testing.T, context spec.G, it spec.S) {
))
})
})

context("when building an app that has a buildpack.yml", func() {
var (
name string
source string
)

it.Before(func() {
var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expect(docker.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
Expect(os.RemoveAll(source)).To(Succeed())
})

it("fails the build", func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "default"))
Expect(err).NotTo(HaveOccurred())

Expect(os.WriteFile(filepath.Join(source, "buildpack.yml"), nil, os.ModePerm)).To(Succeed())

_, logs, err := pack.Build.
WithPullPolicy("never").
WithBuildpacks(
settings.Buildpacks.GoDist.Online,
settings.Buildpacks.GoBuild.Online,
).
Execute(name, source)
Expect(err).To(HaveOccurred(), logs.String)

Expect(logs).To(ContainSubstring("working directory contains deprecated 'buildpack.yml'; use environment variables for configuration"))
Expect(logs).NotTo(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Executing build process",
))
})
})
}

0 comments on commit 46313a7

Please sign in to comment.