Skip to content

Commit 2963072

Browse files
mhdawsonthitch97
authored andcommitted
use function in libnodejs to get project path
Signed-off-by: Michael Dawson <[email protected]>
1 parent cc3eed5 commit 2963072

File tree

11 files changed

+20
-193
lines changed

11 files changed

+20
-193
lines changed

build.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"time"
77

8+
"github.com/paketo-buildpacks/libnodejs"
89
"github.com/paketo-buildpacks/packit/v2/sbom"
910

1011
"github.com/paketo-buildpacks/packit/v2"
@@ -50,8 +51,7 @@ type SymlinkResolver interface {
5051
Resolve(lockfilePath, layerPath string) error
5152
}
5253

53-
func Build(projectPathParser PathParser,
54-
entryResolver EntryResolver,
54+
func Build(entryResolver EntryResolver,
5555
configurationManager ConfigurationManager,
5656
buildManager BuildManager,
5757
pruneProcess PruneProcess,
@@ -72,13 +72,11 @@ func Build(projectPathParser PathParser,
7272

7373
logger.Process("Resolving installation process")
7474

75-
projectPath, err := projectPathParser.Get(context.WorkingDir)
75+
projectPath, err := libnodejs.FindProjectPath(context.WorkingDir)
7676
if err != nil {
7777
return packit.BuildResult{}, err
7878
}
7979

80-
projectPath = filepath.Join(context.WorkingDir, projectPath)
81-
8280
npmCacheLayer, err := context.Layers.Get(LayerNameCache)
8381
if err != nil {
8482
return packit.BuildResult{}, err

build_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package npminstall_test
33
import (
44
"bytes"
55
"errors"
6+
"fmt"
67
"io"
78
"os"
89
"path/filepath"
@@ -33,7 +34,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
3334
processCacheDir string
3435
processNpmrcPath string
3536

36-
projectPathParser *fakes.PathParser
3737
buildProcess *fakes.BuildProcess
3838
buildManager *fakes.BuildManager
3939
configurationManager *fakes.ConfigurationManager
@@ -60,8 +60,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
6060
cnbDir, err = os.MkdirTemp("", "cnb")
6161
Expect(err).NotTo(HaveOccurred())
6262

63-
projectPathParser = &fakes.PathParser{}
64-
projectPathParser.GetCall.Returns.ProjectPath = ""
63+
t.Setenv("BP_NODE_PROJECT_PATH", "")
6564

6665
buildProcess = &fakes.BuildProcess{}
6766
buildProcess.ShouldRunCall.Returns.Run = true
@@ -107,7 +106,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
107106
symlinkResolver = &fakes.SymlinkResolver{}
108107

109108
build = npminstall.Build(
110-
projectPathParser,
111109
entryResolver,
112110
configurationManager,
113111
buildManager,
@@ -255,8 +253,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
255253
Expect(cacheLayer.Launch).To(BeFalse())
256254
Expect(cacheLayer.Cache).To(BeTrue())
257255

258-
Expect(projectPathParser.GetCall.Receives.Path).To(Equal(workingDir))
259-
260256
Expect(configurationManager.DeterminePathCall.Receives.Typ).To(Equal("npmrc"))
261257
Expect(configurationManager.DeterminePathCall.Receives.PlatformDir).To(Equal("some-platform-path"))
262258
Expect(configurationManager.DeterminePathCall.Receives.Entry).To(Equal(".npmrc"))
@@ -406,8 +402,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
406402
Expect(cacheLayer.Launch).To(BeFalse())
407403
Expect(cacheLayer.Cache).To(BeTrue())
408404

409-
Expect(projectPathParser.GetCall.Receives.Path).To(Equal(workingDir))
410-
411405
Expect(configurationManager.DeterminePathCall.Receives.Typ).To(Equal("npmrc"))
412406
Expect(configurationManager.DeterminePathCall.Receives.PlatformDir).To(Equal("some-platform-path"))
413407
Expect(configurationManager.DeterminePathCall.Receives.Entry).To(Equal(".npmrc"))
@@ -715,7 +709,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
715709
})
716710
Expect(err).NotTo(HaveOccurred())
717711

718-
Expect(projectPathParser.GetCall.Receives.Path).To(Equal(workingDir))
719712
Expect(buildManager.ResolveCall.Receives.WorkingDir).To(Equal(workingDir))
720713
Expect(buildProcess.RunCall.CallCount).To(Equal(0))
721714

@@ -726,7 +719,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
726719
context("when BP_NODE_PROJECT_PATH is set", func() {
727720
it.Before(func() {
728721
buildProcess.ShouldRunCall.Returns.Run = true
729-
projectPathParser.GetCall.Returns.ProjectPath = "some-dir"
722+
t.Setenv("BP_NODE_PROJECT_PATH", "some-dir")
730723
Expect(os.MkdirAll(filepath.Join(workingDir, "some-dir", "node_modules"), os.ModePerm))
731724
})
732725

@@ -746,8 +739,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
746739
})
747740
Expect(err).NotTo(HaveOccurred())
748741

749-
Expect(projectPathParser.GetCall.Receives.Path).To(Equal(workingDir))
750-
751742
Expect(buildManager.ResolveCall.Receives.WorkingDir).To(Equal(filepath.Join(workingDir, "some-dir")))
752743

753744
Expect(processLayerDir).To(Equal(filepath.Join(layersDir, "launch-modules")))
@@ -852,7 +843,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
852843

853844
context("when the project path parser provided fails", func() {
854845
it.Before(func() {
855-
projectPathParser.GetCall.Returns.Err = errors.New("failed to parse project path")
846+
t.Setenv("BP_NODE_PROJECT_PATH", "does_not_exist")
856847
})
857848

858849
it("returns an error", func() {
@@ -866,7 +857,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
866857
},
867858
},
868859
})
869-
Expect(err).To(MatchError("failed to parse project path"))
860+
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("could not find project path \"%s/does_not_exist\": stat %s/does_not_exist: no such file or directory", workingDir, workingDir))))
870861
})
871862
})
872863

detect.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77

8+
"github.com/paketo-buildpacks/libnodejs"
89
"github.com/paketo-buildpacks/packit/v2"
910
)
1011

@@ -20,23 +21,18 @@ type VersionParser interface {
2021
ParseVersion(path string) (version string, err error)
2122
}
2223

23-
//go:generate faux --interface PathParser --output fakes/path_parser.go
24-
type PathParser interface {
25-
Get(path string) (projectPath string, err error)
26-
}
27-
28-
func Detect(projectPathParser PathParser, packageJSONParser VersionParser) packit.DetectFunc {
24+
func Detect(packageJSONParser VersionParser) packit.DetectFunc {
2925
return func(context packit.DetectContext) (packit.DetectResult, error) {
3026

31-
projectPath, err := projectPathParser.Get(context.WorkingDir)
27+
projectPath, err := libnodejs.FindProjectPath(context.WorkingDir)
3228
if err != nil {
3329
return packit.DetectResult{}, err
3430
}
3531

36-
version, err := packageJSONParser.ParseVersion(filepath.Join(context.WorkingDir, projectPath, "package.json"))
32+
version, err := packageJSONParser.ParseVersion(filepath.Join(projectPath, "package.json"))
3733
if err != nil {
3834
if errors.Is(err, os.ErrNotExist) {
39-
return packit.DetectResult{}, packit.Fail.WithMessage("no 'package.json' found in project path %s", filepath.Join(context.WorkingDir, projectPath))
35+
return packit.DetectResult{}, packit.Fail.WithMessage("no 'package.json' found in project path %s", filepath.Join(projectPath))
4036
}
4137

4238
return packit.DetectResult{}, err

detect_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
1818
Expect = NewWithT(t).Expect
1919

2020
packageJSONParser *fakes.VersionParser
21-
projectPathParser *fakes.PathParser
2221
detect packit.DetectFunc
2322
)
2423

2524
it.Before(func() {
2625
packageJSONParser = &fakes.VersionParser{}
2726
packageJSONParser.ParseVersionCall.Returns.Version = "1.2.3"
2827

29-
projectPathParser = &fakes.PathParser{}
30-
projectPathParser.GetCall.Returns.ProjectPath = ""
28+
t.Setenv("BP_NODE_PROJECT_PATH", "")
3129

32-
detect = npminstall.Detect(projectPathParser, packageJSONParser)
30+
detect = npminstall.Detect(packageJSONParser)
3331
})
3432

3533
it("returns a plan that provides node_modules", func() {
@@ -60,7 +58,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
6058
}))
6159

6260
Expect(packageJSONParser.ParseVersionCall.Receives.Path).To(Equal("/working-dir/package.json"))
63-
Expect(projectPathParser.GetCall.Receives.Path).To(Equal("/working-dir"))
6461
})
6562

6663
context("when the package.json does not declare a node engine version", func() {
@@ -94,7 +91,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
9491
}))
9592

9693
Expect(packageJSONParser.ParseVersionCall.Receives.Path).To(Equal("/working-dir/package.json"))
97-
Expect(projectPathParser.GetCall.Receives.Path).To(Equal("/working-dir"))
9894
})
9995
})
10096

@@ -128,14 +124,14 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
128124

129125
context("when the project path parser fails", func() {
130126
it.Before(func() {
131-
projectPathParser.GetCall.Returns.Err = errors.New("some-error")
127+
t.Setenv("BP_NODE_PROJECT_PATH", "does_not_exist")
132128
})
133129

134130
it("returns an error", func() {
135131
_, err := detect(packit.DetectContext{
136132
WorkingDir: "/working-dir",
137133
})
138-
Expect(err).To(MatchError("some-error"))
134+
Expect(err).To(MatchError("could not find project path \"/working-dir/does_not_exist\": stat /working-dir/does_not_exist: no such file or directory"))
139135
})
140136
})
141137
})

fakes/path_parser.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ require (
9090
github.com/opencontainers/go-digest v1.0.0 // indirect
9191
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
9292
github.com/opencontainers/runc v1.1.5 // indirect
93+
github.com/paketo-buildpacks/libnodejs v0.0.2 // indirect
9394
github.com/pelletier/go-toml v1.9.5 // indirect
9495
github.com/pierrec/lz4/v4 v4.1.17 // indirect
9596
github.com/pjbgf/sha1cd v0.3.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,6 +2556,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
25562556
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
25572557
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
25582558
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
2559+
github.com/paketo-buildpacks/libnodejs v0.0.2 h1:3pPWg0b2uzaPkKNqVRT3dCGJ6XheuMZpJw8mBp/bYLc=
2560+
github.com/paketo-buildpacks/libnodejs v0.0.2/go.mod h1:D69iwG2Uu9Y0VHOrs2xDk9Oy91qMo9j+gLaRwP2jINw=
25592561
github.com/paketo-buildpacks/occam v0.16.0 h1:NZ206CO4HWX735PM8WrqM+8DvMhb05P7RELVovRKb/E=
25602562
github.com/paketo-buildpacks/occam v0.16.0/go.mod h1:1EO702a0MwsDB9VFYi2oZ7weIL/eRO9SHPu1bNc+t/k=
25612563
github.com/paketo-buildpacks/packit/v2 v2.6.1/go.mod h1:iBArWOfC5xZQF01o+zwnVKS+/hUBuFG+O1jCvzqBujs=

init_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func TestUnitNPMInstall(t *testing.T) {
2222
suite("Linker", testLinker)
2323
suite("PackageJSONParser", testPackageJSONParser)
2424
suite("PackageManangerConfigurationManager", testPackageManagerConfigurationManager)
25-
suite("ProjectPathParser", testProjectPathParser)
2625
suite("PruneBuildProcess", testPruneBuildProcess)
2726
suite("RebuildBuildProcess", testRebuildBuildProcess)
2827
suite("UpdateNpmCacheLayer", testUpdateNpmCache)

project_path_parser.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

project_path_parser_test.go

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)