Skip to content

Commit

Permalink
Use relative path to set start command rel workingdir
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Sreedharan <[email protected]>
Co-authored-by: Arjun Sreedharan <[email protected]>
  • Loading branch information
thitch97 and arjun024 committed Mar 11, 2021
1 parent 8ceaca2 commit 93bb201
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 39 deletions.
6 changes: 4 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Build(pathParser PathParser, logger scribe.Logger) packit.BuildFunc {
return packit.BuildResult{}, err
}

file, err := os.Open(filepath.Join(projectPath, "package.json"))
file, err := os.Open(filepath.Join(context.WorkingDir, projectPath, "package.json"))
if err != nil {
return packit.BuildResult{}, fmt.Errorf("Unable to open package.json: %w", err)
}
Expand All @@ -53,7 +53,9 @@ func Build(pathParser PathParser, logger scribe.Logger) packit.BuildFunc {

// Ideally we would like the lifecycle to support setting a custom working
// directory to run the launch process. Until that happens we will cd in.
command = fmt.Sprintf("cd %s && %s", projectPath, command)
if projectPath != "" {
command = fmt.Sprintf("cd %s && %s", projectPath, command)
}

logger.Process("Assigning launch processes")
logger.Subprocess("web: %s", command)
Expand Down
59 changes: 54 additions & 5 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

pathParser = &fakes.PathParser{}

pathParser.GetCall.Returns.ProjectPath = filepath.Join(workingDir, "some-project-dir")
pathParser.GetCall.Returns.ProjectPath = "some-project-dir"
build = yarnstart.Build(pathParser, logger)
})

Expand Down Expand Up @@ -91,7 +91,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Processes: []packit.Process{
{
Type: "web",
Command: fmt.Sprintf("cd %s && some-prestart-command && some-start-command && some-poststart-command", filepath.Join(workingDir, "some-project-dir")),
Command: "cd some-project-dir && some-prestart-command && some-start-command && some-poststart-command",
},
},
},
Expand Down Expand Up @@ -134,7 +134,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Processes: []packit.Process{
{
Type: "web",
Command: fmt.Sprintf("cd %s && some-start-command && some-poststart-command", filepath.Join(workingDir, "some-project-dir")),
Command: "cd some-project-dir && some-start-command && some-poststart-command",
},
}},
}))
Expand Down Expand Up @@ -176,7 +176,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Processes: []packit.Process{
{
Type: "web",
Command: fmt.Sprintf("cd %s && some-prestart-command && some-start-command", filepath.Join(workingDir, "some-project-dir")),
Command: "cd some-project-dir && some-prestart-command && some-start-command",
},
},
},
Expand Down Expand Up @@ -219,7 +219,56 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Processes: []packit.Process{
{
Type: "web",
Command: fmt.Sprintf("cd %s && some-prestart-command && node server.js && some-poststart-command", filepath.Join(workingDir, "some-project-dir")),
Command: "cd some-project-dir && some-prestart-command && node server.js && some-poststart-command",
},
},
},
}))
})
})

context("when the project-path env var is not set", func() {
it.Before(func() {
pathParser.GetCall.Returns.ProjectPath = ""
err := ioutil.WriteFile(filepath.Join(workingDir, "package.json"), []byte(`{
"scripts": {
"prestart": "some-prestart-command",
"start": "some-start-command",
"poststart": "some-poststart-command"
}
}`), 0644)
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expect(os.Remove(filepath.Join(workingDir, "package.json"))).To(Succeed())
})

it("returns a result with a valid start command", func() {
result, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{},
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())

Expect(result).To(Equal(packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{},
},
Launch: packit.LaunchMetadata{
Processes: []packit.Process{
{
Type: "web",
Command: "some-prestart-command && some-start-command && some-poststart-command",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Detect(projectPathParser PathParser) packit.DetectFunc {
return packit.DetectResult{}, err
}

_, err = os.Stat(filepath.Join(projectPath, "yarn.lock"))
_, err = os.Stat(filepath.Join(context.WorkingDir, projectPath, "yarn.lock"))
if err != nil {
if os.IsNotExist(err) {
return packit.DetectResult{}, packit.Fail
Expand Down
2 changes: 1 addition & 1 deletion detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
Expect(os.Mkdir(filepath.Join(workingDir, "custom"), os.ModePerm)).To(Succeed())

projectPathParser = &fakes.PathParser{}
projectPathParser.GetCall.Returns.ProjectPath = filepath.Join(workingDir, "custom")
projectPathParser.GetCall.Returns.ProjectPath = "custom"

detect = yarnstart.Detect(projectPathParser)
})
Expand Down
14 changes: 7 additions & 7 deletions integration/custom_start_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ func testCustomStartCmd(t *testing.T, context spec.G, it spec.S) {
Execute(name, source)
Expect(err).NotTo(HaveOccurred(), logs.String())

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Assigning launch processes",
` web: echo "prestart" && echo "start" && node server.js && echo "poststart"`,
"",
))

container, err = docker.Container.Run.
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Expand All @@ -77,13 +84,6 @@ func testCustomStartCmd(t *testing.T, context spec.G, it spec.S) {

Eventually(container).Should(BeAvailable())

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Assigning launch processes",
` web: cd /workspace && echo "prestart" && echo "start" && node server.js && echo "poststart"`,
"",
))

response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort("8080")))
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()
Expand Down
14 changes: 7 additions & 7 deletions integration/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
Execute(name, source)
Expect(err).NotTo(HaveOccurred(), logs.String())

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Assigning launch processes",
" web: node server.js",
"",
))

container, err = docker.Container.Run.
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Expand All @@ -75,13 +82,6 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {

Eventually(container).Should(BeAvailable())

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Assigning launch processes",
" web: cd /workspace && node server.js",
"",
))

response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort("8080")))
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()
Expand Down
2 changes: 1 addition & 1 deletion integration/graceful_shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func testGracefulShutdown(t *testing.T, context spec.G, it spec.S) {
Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Assigning launch processes",
" web: cd /workspace && node server.js",
" web: node server.js",
"",
))

Expand Down
14 changes: 7 additions & 7 deletions integration/project_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func testProjectPath(t *testing.T, context spec.G, it spec.S) {
Execute(name, source)
Expect(err).NotTo(HaveOccurred(), logs.String())

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Assigning launch processes",
` web: cd hello_world_server && echo "prehello" && echo "starthello" && node server.js && echo "posthello"`,
"",
))

container, err = docker.Container.Run.
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Expand All @@ -78,13 +85,6 @@ func testProjectPath(t *testing.T, context spec.G, it spec.S) {

Eventually(container).Should(BeAvailable())

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Assigning launch processes",
` web: cd /workspace/hello_world_server && echo "prehello" && echo "starthello" && node server.js && echo "posthello"`,
"",
))

response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort("8080")))
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()
Expand Down
10 changes: 4 additions & 6 deletions project_path_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ func NewProjectPathParser() ProjectPathParser {
func (p ProjectPathParser) Get(path string) (string, error) {
customProjPath := os.Getenv("BP_NODE_PROJECT_PATH")
if customProjPath == "" {
return path, nil
return "", nil
}

customProjPath = filepath.Clean(customProjPath)
path = filepath.Join(path, customProjPath)
_, err := os.Stat(path)
_, err := os.Stat(filepath.Join(path, customProjPath))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("expected value derived from BP_NODE_PROJECT_PATH [%s] to be an existing directory", path)
return "", fmt.Errorf("expected value derived from BP_NODE_PROJECT_PATH [%s] to be an existing directory", customProjPath)
}
return "", err
}
return path, nil
return customProjPath, nil
}
4 changes: 2 additions & 2 deletions project_path_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func testProjectPathParser(t *testing.T, context spec.G, it spec.S) {
it("returns the set project path", func() {
result, err := projectPathParser.Get(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(projectDir))
Expect(result).To(Equal(filepath.Join("custom", "path")))
})
})

Expand Down Expand Up @@ -74,7 +74,7 @@ func testProjectPathParser(t *testing.T, context spec.G, it spec.S) {

it("returns an error", func() {
_, err := projectPathParser.Get(workingDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("expected value derived from BP_NODE_PROJECT_PATH [%s] to be an existing directory", filepath.Join(workingDir, "some-garbage")))))
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("expected value derived from BP_NODE_PROJECT_PATH [%s] to be an existing directory", "some-garbage"))))
})
})

Expand Down

0 comments on commit 93bb201

Please sign in to comment.