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

feat: add env var to configure webroot #4

Merged
merged 1 commit into from
Dec 15, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This buildpack complements the
[`buildpack-static-confgen`](https://github.com/ninech/buildpack-static-confgen)
buildpack, see the `README.md` on why this can't be in a single buildpack.
This simply requires nginx if an `index.html` can be found in the workspace.
The build phase is a noop.
This simply requires nginx if an `index.html` can be found in the workspace or
if the env var `BP_STATIC_WEBROOT` is configured. The build phase is a noop.

To test the build locally, checkout this repository and then build it with:

Expand Down
10 changes: 9 additions & 1 deletion detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/paketo-buildpacks/packit/v2/scribe"
)

const indexFile = "index.html"
const (
indexFile = "index.html"
webRootEnv = "BP_STATIC_WEBROOT"
)

type BuildPlanMetadata struct {
Version string `toml:"version,omitempty"`
Expand Down Expand Up @@ -40,6 +43,11 @@ func Detect(logger scribe.Emitter) packit.DetectFunc {
}

func WebRoot(workingDir string) (string, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't part of this PR but I see that we never use the return string value. Maybe there is a plan to consume it somewhere in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use it in the other buildpack :)

// the webroot can be set using an env var
if v, ok := os.LookupEnv(webRootEnv); ok {
return v, nil
}

paths := []string{"./", "./public"}
publicDir := ""
for _, path := range paths {
Expand Down
16 changes: 16 additions & 0 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
Expect(err).To(MatchError(packit.Fail.WithMessage("no index.html found")))
})
})

context("when webroot is set via env", func() {
it.Before(func() {
t.Setenv(webRootEnv, "custom")
})

it("requires nginx", func() {
result, err := Detect(scribe.NewEmitter(buffer))(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).NotTo(HaveOccurred())
Expect(result.Plan.Requires).To(ContainElement(packit.BuildPlanRequirement{
Name: nginx.NGINX, Metadata: BuildPlanMetadata{Launch: true},
}))
})
})
}
2 changes: 1 addition & 1 deletion init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestUnitBuildpackStatic(t *testing.T) {
suite := spec.New("buildpack-static-require", spec.Report(report.Terminal{}), spec.Parallel())
suite := spec.New("buildpack-static-require", spec.Report(report.Terminal{}))
suite("Detect", testDetect)
suite.Run(t)
}