From cec63f2f139feb66c3eda5e2b17321b514a1e76c Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 2 May 2023 12:53:47 -0400 Subject: [PATCH] use shared libnodejs function to find applications Leverage the shared function in libnodejs to find applications which was extracted out of node-start. Signed-off-by: Michael Dawson --- build.go | 7 +- build_test.go | 17 +++-- detect.go | 4 +- detect_test.go | 22 ++---- fakes/application_finder.go | 33 --------- go.mod | 2 +- go.sum | 2 + init_test.go | 1 - node_application_finder.go | 47 ------------- node_application_finder_test.go | 120 -------------------------------- run/main.go | 4 +- 11 files changed, 23 insertions(+), 236 deletions(-) delete mode 100644 fakes/application_finder.go delete mode 100644 node_application_finder.go delete mode 100644 node_application_finder_test.go diff --git a/build.go b/build.go index d9882bf..64e3348 100644 --- a/build.go +++ b/build.go @@ -1,18 +1,17 @@ package nodestart import ( - "os" - + "github.com/paketo-buildpacks/libnodejs" "github.com/paketo-buildpacks/libreload-packit" "github.com/paketo-buildpacks/packit/v2" "github.com/paketo-buildpacks/packit/v2/scribe" ) -func Build(applicationFinder ApplicationFinder, logger scribe.Emitter, reloader Reloader) packit.BuildFunc { +func Build(logger scribe.Emitter, reloader Reloader) packit.BuildFunc { return func(context packit.BuildContext) (packit.BuildResult, error) { logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version) - file, err := applicationFinder.Find(context.WorkingDir, os.Getenv("BP_LAUNCHPOINT"), os.Getenv("BP_NODE_PROJECT_PATH")) + file, err := libnodejs.FindNodeApplication(context.WorkingDir) if err != nil { return packit.BuildResult{}, err } diff --git a/build_test.go b/build_test.go index f5fafc4..fc1d1da 100644 --- a/build_test.go +++ b/build_test.go @@ -3,6 +3,9 @@ package nodestart_test import ( "bytes" "errors" + "fmt" + "os" + "path/filepath" "testing" "github.com/paketo-buildpacks/libreload-packit" @@ -24,8 +27,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { cnbDir string buffer *bytes.Buffer - applicationFinder *fakes.ApplicationFinder - reloader *fakes.Reloader + reloader *fakes.Reloader buildContext packit.BuildContext build packit.BuildFunc @@ -36,8 +38,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { cnbDir = t.TempDir() workingDir = t.TempDir() - applicationFinder = &fakes.ApplicationFinder{} - applicationFinder.FindCall.Returns.String = "server.js" + Expect(os.WriteFile(filepath.Join(workingDir, "server.js"), nil, 0600)).To(Succeed()) reloader = &fakes.Reloader{} @@ -57,7 +58,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { }, Layers: packit.Layers{Path: layersDir}, } - build = nodestart.Build(applicationFinder, logger, reloader) + build = nodestart.Build(logger, reloader) }) it("returns a result that provides a node start command", func() { @@ -82,8 +83,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { }, })) - Expect(applicationFinder.FindCall.Receives.WorkingDir).To(Equal(workingDir)) - Expect(buffer.String()).To(ContainSubstring("Some Buildpack some-version")) Expect(buffer.String()).To(ContainSubstring("Assigning launch processes")) Expect(buffer.String()).To(ContainSubstring("node server.js")) @@ -137,12 +136,12 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { context("failure cases", func() { context("when the application finding fails", func() { it.Before(func() { - applicationFinder.FindCall.Returns.Error = errors.New("failed to find application") + Expect(os.Remove(filepath.Join(workingDir, "server.js"))).To(Succeed()) }) it("returns an error", func() { _, err := build(buildContext) - Expect(err).To(MatchError("failed to find application")) + Expect(err).To(MatchError(fmt.Errorf("could not find app in %s: expected one of server.js | app.js | main.js | index.js", workingDir))) }) }) diff --git a/detect.go b/detect.go index 25fb3e0..15ce86e 100644 --- a/detect.go +++ b/detect.go @@ -17,9 +17,9 @@ type Reloader libreload.Reloader //go:generate faux --interface Reloader --output fakes/reloader.go -func Detect(applicationFinder ApplicationFinder, reloader Reloader) packit.DetectFunc { +func Detect(reloader Reloader) packit.DetectFunc { return func(context packit.DetectContext) (packit.DetectResult, error) { - _, err := applicationFinder.Find(context.WorkingDir, os.Getenv("BP_LAUNCHPOINT"), os.Getenv("BP_NODE_PROJECT_PATH")) + _, err := libnodejs.FindNodeApplication(context.WorkingDir) if err != nil { return packit.DetectResult{}, err } diff --git a/detect_test.go b/detect_test.go index ed2a0be..df772bd 100644 --- a/detect_test.go +++ b/detect_test.go @@ -2,6 +2,7 @@ package nodestart_test import ( "errors" + "fmt" "os" "path/filepath" "testing" @@ -18,8 +19,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { var ( Expect = NewWithT(t).Expect - applicationFinder *fakes.ApplicationFinder - reloader *fakes.Reloader + reloader *fakes.Reloader detect packit.DetectFunc workingDir string @@ -28,12 +28,9 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { it.Before(func() { workingDir = t.TempDir() - applicationFinder = &fakes.ApplicationFinder{} - applicationFinder.FindCall.Returns.String = "./src/server.js" - reloader = &fakes.Reloader{} - detect = nodestart.Detect(applicationFinder, reloader) + detect = nodestart.Detect(reloader) }) context("when an application is detected in the working dir", func() { @@ -59,10 +56,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { }, }, })) - - Expect(applicationFinder.FindCall.Receives.WorkingDir).To(Equal(workingDir)) - Expect(applicationFinder.FindCall.Receives.Launchpoint).To(Equal("./src/server.js")) - Expect(applicationFinder.FindCall.Receives.ProjectPath).To(Equal("./src")) }) context("when live reload is enabled", func() { @@ -98,7 +91,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { t.Setenv("BP_NODE_PROJECT_PATH", "./src") Expect(os.MkdirAll(filepath.Join(workingDir, "src"), os.ModePerm)).To(Succeed()) Expect(os.WriteFile(filepath.Join(workingDir, "src", "package.json"), []byte(`{}`), 0600)).To(Succeed()) - + Expect(os.WriteFile(filepath.Join(workingDir, "src", "server.js"), nil, 0600)).To(Succeed()) }) it("requires node_modules", func() { @@ -125,15 +118,11 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { context("failure cases", func() { context("when the application finder fails", func() { - it.Before(func() { - applicationFinder.FindCall.Returns.Error = errors.New("application finder failed") - }) - it("fails with helpful error", func() { _, err := detect(packit.DetectContext{ WorkingDir: workingDir, }) - Expect(err).To(MatchError("application finder failed")) + Expect(err).To(MatchError(fmt.Errorf("could not find app in %s: expected one of server.js | app.js | main.js | index.js", workingDir))) }) }) @@ -156,6 +145,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) { context("when the reloader returns an error", func() { it.Before(func() { + Expect(os.WriteFile(filepath.Join(workingDir, "server.js"), nil, 0600)).To(Succeed()) reloader.ShouldEnableLiveReloadCall.Returns.Error = errors.New("reloader error") }) diff --git a/fakes/application_finder.go b/fakes/application_finder.go deleted file mode 100644 index 510619a..0000000 --- a/fakes/application_finder.go +++ /dev/null @@ -1,33 +0,0 @@ -package fakes - -import "sync" - -type ApplicationFinder struct { - FindCall struct { - mutex sync.Mutex - CallCount int - Receives struct { - WorkingDir string - Launchpoint string - ProjectPath string - } - Returns struct { - String string - Error error - } - Stub func(string, string, string) (string, error) - } -} - -func (f *ApplicationFinder) Find(param1 string, param2 string, param3 string) (string, error) { - f.FindCall.mutex.Lock() - defer f.FindCall.mutex.Unlock() - f.FindCall.CallCount++ - f.FindCall.Receives.WorkingDir = param1 - f.FindCall.Receives.Launchpoint = param2 - f.FindCall.Receives.ProjectPath = param3 - if f.FindCall.Stub != nil { - return f.FindCall.Stub(param1, param2, param3) - } - return f.FindCall.Returns.String, f.FindCall.Returns.Error -} diff --git a/go.mod b/go.mod index bdc19aa..b90cf2a 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/BurntSushi/toml v1.3.2 github.com/onsi/gomega v1.27.8 - github.com/paketo-buildpacks/libnodejs v0.0.2 + github.com/paketo-buildpacks/libnodejs v0.1.0 github.com/paketo-buildpacks/libreload-packit v0.0.1 github.com/paketo-buildpacks/occam v0.17.0 github.com/paketo-buildpacks/packit/v2 v2.11.0 diff --git a/go.sum b/go.sum index 87b4a27..3f203f2 100644 --- a/go.sum +++ b/go.sum @@ -2489,6 +2489,8 @@ github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/paketo-buildpacks/libnodejs v0.0.2 h1:3pPWg0b2uzaPkKNqVRT3dCGJ6XheuMZpJw8mBp/bYLc= github.com/paketo-buildpacks/libnodejs v0.0.2/go.mod h1:D69iwG2Uu9Y0VHOrs2xDk9Oy91qMo9j+gLaRwP2jINw= +github.com/paketo-buildpacks/libnodejs v0.1.0 h1:MaZXwK5QOn2pwrVya2i2PogQDo8n8pFefCvZIjGHo2U= +github.com/paketo-buildpacks/libnodejs v0.1.0/go.mod h1:6zYvjp6d0mw2Sw/3ITF1CTk7dLQigpI37QGoG2IFeRE= github.com/paketo-buildpacks/libreload-packit v0.0.1 h1:K1HhNAqBSzRpefwGOcvdchZwyeNTgNJL9SC7V4paYt8= github.com/paketo-buildpacks/libreload-packit v0.0.1/go.mod h1:ZWE3U94Z18yJk8Pc1mP852l9phQsrsZ+An2U98g0rWw= github.com/paketo-buildpacks/occam v0.17.0 h1:2Xt4Vc4MbSAsLyu+WLttd6gpR4ORsBwcoqAfWz4QaD4= diff --git a/init_test.go b/init_test.go index 0a82d11..cb2ea7e 100644 --- a/init_test.go +++ b/init_test.go @@ -11,6 +11,5 @@ func TestUnitNodeStart(t *testing.T) { suite := spec.New("node-start", spec.Report(report.Terminal{}), spec.Sequential()) suite("Build", testBuild) suite("Detect", testDetect) - suite("ApplicationFinder", testNodeApplicationFinder) suite.Run(t) } diff --git a/node_application_finder.go b/node_application_finder.go deleted file mode 100644 index e964f26..0000000 --- a/node_application_finder.go +++ /dev/null @@ -1,47 +0,0 @@ -package nodestart - -import ( - "errors" - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/paketo-buildpacks/packit/v2" -) - -type NodeApplicationFinder struct{} - -func NewNodeApplicationFinder() NodeApplicationFinder { - return NodeApplicationFinder{} -} - -func (n NodeApplicationFinder) Find(workingDir, launchpoint, projectPath string) (string, error) { - if launchpoint != "" { - if _, err := os.Stat(filepath.Join(workingDir, launchpoint)); err != nil { - if errors.Is(err, os.ErrNotExist) { - return "", fmt.Errorf("expected value derived from BP_LAUNCHPOINT [%s] to be an existing file", launchpoint) - } - - return "", err - } - - return filepath.Clean(launchpoint), nil - } - - files := []string{"server.js", "app.js", "main.js", "index.js"} - for _, file := range files { - _, err := os.Stat(filepath.Join(workingDir, projectPath, file)) - if err != nil { - if errors.Is(err, os.ErrNotExist) { - continue - } - - return "", err - } - - return filepath.Join(projectPath, file), nil - } - - return "", packit.Fail.WithMessage("could not find app in %s: expected one of %s", filepath.Clean(filepath.Join(workingDir, projectPath)), strings.Join(files, " | ")) -} diff --git a/node_application_finder_test.go b/node_application_finder_test.go deleted file mode 100644 index 283b6b3..0000000 --- a/node_application_finder_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package nodestart_test - -import ( - "os" - "path/filepath" - "testing" - - nodestart "github.com/paketo-buildpacks/node-start" - "github.com/paketo-buildpacks/packit/v2" - "github.com/sclevine/spec" - - . "github.com/onsi/gomega" -) - -func testNodeApplicationFinder(t *testing.T, context spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - workingDir string - nodeApplicationFinder nodestart.NodeApplicationFinder - ) - - it.Before(func() { - workingDir = t.TempDir() - - Expect(os.WriteFile(filepath.Join(workingDir, "server.js"), nil, 0600)).To(Succeed()) - Expect(os.WriteFile(filepath.Join(workingDir, "app.js"), nil, 0600)).To(Succeed()) - }) - - it("finds the application entrypoint", func() { - file, err := nodeApplicationFinder.Find(workingDir, "", "") - Expect(err).NotTo(HaveOccurred()) - Expect(file).To(Equal(filepath.Join("server.js"))) - }) - - context("when there is a launchpoint", func() { - it.Before(func() { - Expect(os.Mkdir(filepath.Join(workingDir, "src"), os.ModePerm)).To(Succeed()) - Expect(os.WriteFile(filepath.Join(workingDir, "src", "launchpoint.js"), nil, 0600)).To(Succeed()) - }) - - it.After(func() { - Expect(os.RemoveAll(filepath.Join(workingDir, "src"))).To(Succeed()) - }) - - context("when the launchpoint file exists", func() { - it("returns the highest priority file", func() { - file, err := nodeApplicationFinder.Find(workingDir, "./src/launchpoint.js", "") - Expect(err).NotTo(HaveOccurred()) - Expect(file).To(Equal(filepath.Join("src", "launchpoint.js"))) - }) - }) - - context("when the launchpoint file does not exist", func() { - it("returns the empty string and no error", func() { - file, err := nodeApplicationFinder.Find(workingDir, "./no-such-file.js", "") - Expect(err).To(MatchError(ContainSubstring("expected value derived from BP_LAUNCHPOINT [./no-such-file.js] to be an existing file"))) - Expect(file).To(Equal("")) - }) - }) - }) - - context("when there is a project path", func() { - it.Before(func() { - Expect(os.Mkdir(filepath.Join(workingDir, "frontend"), os.ModePerm)).To(Succeed()) - Expect(os.WriteFile(filepath.Join(workingDir, "frontend", "server.js"), nil, 0600)).To(Succeed()) - Expect(os.WriteFile(filepath.Join(workingDir, "frontend", "app.js"), nil, 0600)).To(Succeed()) - }) - - it("returns the highest priority file", func() { - file, err := nodeApplicationFinder.Find(workingDir, "", "frontend") - Expect(err).NotTo(HaveOccurred()) - Expect(file).To(Equal(filepath.Join("frontend", "server.js"))) - }) - }) - - context("when no application can be found", func() { - it.Before(func() { - Expect(os.RemoveAll(workingDir)).To(Succeed()) - Expect(os.MkdirAll(workingDir, os.ModePerm)).To(Succeed()) - }) - - it("returns a packit failure", func() { - _, err := nodeApplicationFinder.Find(workingDir, "", "") - Expect(err).To(MatchError(packit.Fail.WithMessage("could not find app in %s: expected one of server.js | app.js | main.js | index.js", workingDir))) - }) - }) - - context("failure cases", func() { - context("when the launchpoint cannot be stat'd", func() { - it.Before(func() { - Expect(os.Chmod(workingDir, 0000)).To(Succeed()) - }) - - it.After(func() { - Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed()) - }) - - it("fails with helpful error", func() { - _, err := nodeApplicationFinder.Find(workingDir, "something.js", "") - Expect(err).To(MatchError(ContainSubstring("permission denied"))) - }) - }) - - context("when the working dir cannot be stat'd", func() { - it.Before(func() { - Expect(os.Chmod(workingDir, 0000)).To(Succeed()) - }) - - it.After(func() { - Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed()) - }) - - it("fails with helpful error", func() { - _, err := nodeApplicationFinder.Find(workingDir, "", "") - Expect(err).To(MatchError(ContainSubstring("permission denied"))) - }) - }) - }) -} diff --git a/run/main.go b/run/main.go index 3684eee..25e79fb 100644 --- a/run/main.go +++ b/run/main.go @@ -10,15 +10,13 @@ import ( ) func main() { - nodeApplicationFinder := nodestart.NewNodeApplicationFinder() logger := scribe.NewEmitter(os.Stdout).WithLevel(os.Getenv("BP_LOG_LEVEL")) reloader := watchexec.NewWatchexecReloader() packit.Run( - nodestart.Detect(nodeApplicationFinder, reloader), + nodestart.Detect(reloader), nodestart.Build( - nodeApplicationFinder, logger, reloader, ),