Skip to content

Commit

Permalink
use shared libnodejs function to find applications (#356)
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]>
Co-authored-by: TisVictress <[email protected]>
  • Loading branch information
mhdawson and TisVictress authored Jul 24, 2023
1 parent db5a68a commit f5e4590
Show file tree
Hide file tree
Showing 10 changed files with 22 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.

2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2487,6 +2487,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
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=
Expand Down
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 f5e4590

Please sign in to comment.