From cb5fdbd6771b9fffc34f416023d959ad7b2d418a Mon Sep 17 00:00:00 2001 From: Cyrill Troxler Date: Fri, 15 Dec 2023 10:01:59 +0100 Subject: [PATCH] feat: add env var to configure webroot this adds a new env var that overrides the webroot and will also allow it to pass detection even if there is no `public.html` in any of the expected places. --- README.md | 4 ++-- detect.go | 10 +++++++++- detect_test.go | 16 ++++++++++++++++ init_test.go | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ba17240..5c31315 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/detect.go b/detect.go index 916bb05..254c736 100644 --- a/detect.go +++ b/detect.go @@ -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"` @@ -40,6 +43,11 @@ func Detect(logger scribe.Emitter) packit.DetectFunc { } func WebRoot(workingDir string) (string, error) { + // 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 { diff --git a/detect_test.go b/detect_test.go index 3b9299f..6991264 100644 --- a/detect_test.go +++ b/detect_test.go @@ -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}, + })) + }) + }) } diff --git a/init_test.go b/init_test.go index 034d07c..f28ed87 100644 --- a/init_test.go +++ b/init_test.go @@ -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) }