Skip to content

Commit

Permalink
Tidy Up (#32)
Browse files Browse the repository at this point in the history
* Add .idea/ to .gitignore

* Bump Go to 1.17

* Bump Golang to 1.18

* Add packit.Fail message to indicate why detection failed

* Remove unused clock parameter

* Use draft.Planner from packit directly

- See paketo-buildpacks/cpython#327 for context

* Extract variables to simplify build_test.go

* Simplify code in Build()
  • Loading branch information
joshuatcasey authored Jun 6, 2022
1 parent 903f9ef commit f7bd658
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 149 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/build
/bin/
*.tgz
.idea/
18 changes: 4 additions & 14 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ package phphttpd

import (
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/draft"
"github.com/paketo-buildpacks/packit/v2/scribe"
)

//go:generate faux --interface EntryResolver --output fakes/entry_resolver.go

// EntryResolver defines the interface for picking the most relevant entry from
// the Buildpack Plan entries.
type EntryResolver interface {
MergeLayerTypes(name string, entries []packit.BuildpackPlanEntry) (launch, build bool)
}

//go:generate faux --interface ConfigWriter --output fakes/config_writer.go

// ConfigWriter sets up the HTTPD configuration file with defaults, and adds in
Expand All @@ -29,7 +21,7 @@ type ConfigWriter interface {
// settings, incorporate other configuration sources, and make the
// configuration available at both build-time and
// launch-time.
func Build(entryResolver EntryResolver, config ConfigWriter, clock chronos.Clock, logger scribe.Emitter) packit.BuildFunc {
func Build(config ConfigWriter, logger scribe.Emitter) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)

Expand All @@ -53,11 +45,9 @@ func Build(entryResolver EntryResolver, config ConfigWriter, clock chronos.Clock
}
logger.Break()

launch, build := entryResolver.MergeLayerTypes(PhpHttpdConfig, context.Plan.Entries)
phpHttpdLayer.Launch = launch
phpHttpdLayer.Build = build
planner := draft.NewPlanner()
phpHttpdLayer.Launch, phpHttpdLayer.Build = planner.MergeLayerTypes(PhpHttpdConfig, context.Plan.Entries)

// test this
phpHttpdLayer.SharedEnv.Default("PHP_HTTPD_PATH", httpdConfigPath)
logger.EnvironmentVariables(phpHttpdLayer)

Expand Down
137 changes: 48 additions & 89 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"

"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/scribe"
phphttpd "github.com/paketo-buildpacks/php-httpd"
"github.com/paketo-buildpacks/php-httpd/fakes"
Expand All @@ -26,9 +25,11 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
workingDir string
cnbDir string

buffer *bytes.Buffer
config *fakes.ConfigWriter
entryResolver *fakes.EntryResolver
buffer *bytes.Buffer
config *fakes.ConfigWriter

buildContext packit.BuildContext
expectedPhpLayer packit.Layer

build packit.BuildFunc
)
Expand All @@ -44,26 +45,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

clock := chronos.DefaultClock

buffer = bytes.NewBuffer(nil)
logEmitter := scribe.NewEmitter(buffer)

entryResolver = &fakes.EntryResolver{}

config = &fakes.ConfigWriter{}
config.WriteCall.Returns.String = "some-workspace/httpd.conf"

build = phphttpd.Build(entryResolver, config, clock, logEmitter)
})

it.After(func() {
Expect(os.RemoveAll(cnbDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})

it("writes a config file into its layer", func() {
result, err := build(packit.BuildContext{
buildContext = packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
Expand All @@ -73,66 +61,63 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "some-entry"},
{
Name: phphttpd.PhpHttpdConfig,
},
},
},
Layers: packit.Layers{Path: layerDir},
})
}

expectedPhpLayer = packit.Layer{
Path: filepath.Join(layerDir, phphttpd.PhpHttpdConfigLayer),
Name: phphttpd.PhpHttpdConfigLayer,
Build: false,
Launch: false,
Cache: false,
SharedEnv: packit.Environment{
"PHP_HTTPD_PATH.default": "some-workspace/httpd.conf",
},
BuildEnv: packit.Environment{},
LaunchEnv: packit.Environment{},
ProcessLaunchEnv: map[string]packit.Environment{},
}

build = phphttpd.Build(config, logEmitter)
})

it.After(func() {
Expect(os.RemoveAll(cnbDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})

it("writes a config file into its layer", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())

Expect(config.WriteCall.Receives.LayerPath).To(Equal(filepath.Join(layerDir, "php-httpd-config")))
Expect(config.WriteCall.Receives.WorkingDir).To(Equal(workingDir))
Expect(config.WriteCall.Receives.CnbPath).To(Equal(cnbDir))

Expect(entryResolver.MergeLayerTypesCall.Receives.Name).To(Equal(phphttpd.PhpHttpdConfig))
Expect(entryResolver.MergeLayerTypesCall.Receives.Entries).To(Equal([]packit.BuildpackPlanEntry{
{Name: "some-entry"},
}))

Expect(result.Layers).To(HaveLen(1))
Expect(result.Layers[0].Name).To(Equal("php-httpd-config"))
Expect(result.Layers[0].Path).To(Equal(filepath.Join(layerDir, "php-httpd-config")))
Expect(result.Layers[0].SharedEnv).To(Equal(packit.Environment{
"PHP_HTTPD_PATH.default": "some-workspace/httpd.conf",
}))

Expect(result.Layers[0].Build).To(BeFalse())
Expect(result.Layers[0].Cache).To(BeFalse())
Expect(result.Layers[0].Launch).To(BeFalse())
Expect(result.Layers[0]).To(Equal(expectedPhpLayer))
})

context("when httpd-config is required at launch time", func() {
it.Before(func() {
entryResolver.MergeLayerTypesCall.Returns.Launch = true
buildContext.Plan.Entries[0].Metadata = map[string]interface{}{
"launch": true,
}

expectedPhpLayer.Launch = true
})

it("makes the layer available at launch time", func() {
result, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "some-entry"},
},
},
Layers: packit.Layers{Path: layerDir},
})
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(1))
Expect(result.Layers[0].Name).To(Equal("php-httpd-config"))
Expect(result.Layers[0].Path).To(Equal(filepath.Join(layerDir, "php-httpd-config")))
Expect(result.Layers[0].SharedEnv).To(Equal(packit.Environment{
"PHP_HTTPD_PATH.default": "some-workspace/httpd.conf",
}))

Expect(result.Layers[0].Launch).To(BeTrue())
Expect(result.Layers[0].Build).To(BeFalse())
Expect(result.Layers[0].Cache).To(BeFalse())
Expect(result.Layers[0]).To(Equal(expectedPhpLayer))
})
})

Expand All @@ -142,22 +127,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
err := os.WriteFile(filepath.Join(layerDir, fmt.Sprintf("%s.toml", phphttpd.PhpHttpdConfigLayer)), nil, 0000)
Expect(err).NotTo(HaveOccurred())
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: phphttpd.PhpHttpdConfigLayer},
},
},
Layers: packit.Layers{Path: layerDir},
})
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("failed to parse layer content metadata")))
})
})
Expand All @@ -166,22 +138,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
config.WriteCall.Returns.Error = errors.New("config writing error")
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: phphttpd.PhpHttpdConfigLayer},
},
},
Layers: packit.Layers{Path: layerDir},
})
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("config writing error")))
})
})
Expand Down
5 changes: 2 additions & 3 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {

// only pass detection if $BP_PHP_SERVER is set to httpd
server := os.Getenv("BP_PHP_SERVER")
if server != "httpd" {
return packit.DetectResult{}, packit.Fail
if os.Getenv("BP_PHP_SERVER") != "httpd" {
return packit.DetectResult{}, packit.Fail.WithMessage("BP_PHP_SERVER is not set to 'httpd'")
}

return packit.DetectResult{
Expand Down
3 changes: 2 additions & 1 deletion detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
Expect(os.Setenv("BP_PHP_SERVER", "httpd")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("BP_PHP_SERVER")).To(Succeed())
})
Expand All @@ -61,7 +62,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
_, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).To(MatchError(packit.Fail))
Expect(err).To(MatchError(packit.Fail.WithMessage("BP_PHP_SERVER is not set to 'httpd'")))
})
})
}
35 changes: 0 additions & 35 deletions fakes/entry_resolver.go

This file was deleted.

45 changes: 44 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/paketo-buildpacks/php-httpd

go 1.16
go 1.18

require (
github.com/BurntSushi/toml v1.1.0
Expand All @@ -9,3 +9,46 @@ require (
github.com/paketo-buildpacks/packit/v2 v2.3.0
github.com/sclevine/spec v1.4.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/ForestEckhardt/freezer v0.0.11 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/hcsshim v0.8.23 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/containerd/cgroups v1.0.1 // indirect
github.com/containerd/containerd v1.5.10 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.12+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/moby/sys/mount v0.2.0 // indirect
github.com/moby/sys/mountinfo v0.5.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20220114050600-8b9d41f48198 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/testcontainers/testcontainers-go v0.13.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220218161850-94dd64e39d7c // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.0.3/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.1.0/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
Expand Down Expand Up @@ -408,7 +407,6 @@ github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n
github.com/cavaliercoder/badio v0.0.0-20160213150051-ce5280129e9e/go.mod h1:V284PjgVwSk4ETmz84rpu9ehpGg7swlIH8npP9k2bGw=
github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e/go.mod h1:oDpT4efm8tSYHXV5tHSdRvBet/b/QzxZ+XyyPehvm3A=
github.com/cavaliercoder/go-rpm v0.0.0-20200122174316-8cb9fd9c31a8/go.mod h1:AZIh1CCnMrcVm6afFf96PBvE2MRpWFco91z8ObJtgDY=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/cenkalti/backoff/v3 v3.2.2/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
Expand Down
Loading

0 comments on commit f7bd658

Please sign in to comment.