Skip to content

Commit

Permalink
use shared libnodejs function to find applications
Browse files Browse the repository at this point in the history
Leverage the shared function in libnodejs to find applications
which was extracted out of node-start.

Signed-off-by: Michael Dawson <[email protected]>
  • Loading branch information
mhdawson committed May 2, 2023
1 parent 6beac12 commit 5d6b152
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 235 deletions.
7 changes: 3 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
17 changes: 8 additions & 9 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package nodestart_test
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/paketo-buildpacks/libreload-packit"
Expand All @@ -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
Expand All @@ -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{}

Expand All @@ -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() {
Expand All @@ -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"))
Expand Down Expand Up @@ -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)))
})
})

Expand Down
4 changes: 2 additions & 2 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 6 additions & 16 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nodestart_test

import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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)))
})
})

Expand All @@ -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")
})

Expand Down
33 changes: 0 additions & 33 deletions fakes/application_finder.go

This file was deleted.

1 change: 0 additions & 1 deletion init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
47 changes: 0 additions & 47 deletions node_application_finder.go

This file was deleted.

120 changes: 0 additions & 120 deletions node_application_finder_test.go

This file was deleted.

Loading

0 comments on commit 5d6b152

Please sign in to comment.