Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing functionality that conditionally provides Node.js #56

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions detect.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
package ubinodejsextension

import (
"os"
"path/filepath"

"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
)

func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
// likely move these to main.go ?
workingDir := context.WorkingDir

projectPath, err := libnodejs.FindProjectPath(context.WorkingDir)
if err != nil {
return packit.DetectResult{}, err
}

pkg, err := libnodejs.ParsePackageJSON(filepath.Join(projectPath))
if err != nil && !os.IsNotExist(err) {
return packit.DetectResult{}, packit.Fail
}

if err != nil || !pkg.HasStartScript() {
// no package.json so look for Node.js application files
path, err := libnodejs.FindNodeApplication(workingDir)
if err != nil {
return packit.DetectResult{}, err
}
// if no application was found then we don't need to provide node
if path == "" {
return packit.DetectResult{}, packit.Fail.WithMessage("Not a Node.js application")
}
}

// if we get here we either found a pacakge.json or Node.js application file
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
Expand Down
227 changes: 9 additions & 218 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (

ubinodejsextension "github.com/paketo-community/ubi-nodejs-extension"

// "github.com/paketo-buildpacks/packit/v2"
"os"
"path/filepath"

. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/packit/v2"
"github.com/sclevine/spec"
Expand All @@ -28,224 +24,19 @@ var expectedDetectBuildPlan packit.BuildPlan = packit.BuildPlan{
},
}

var expectedNotDetectBuildPlan packit.BuildPlan = packit.BuildPlan{
Provides: nil,
}

func testDetect(t *testing.T, context spec.G, it spec.S) {

var (
Expect = NewWithT(t).Expect
workingDir string
detectContext packit.DetectContext
err error
detectResult packit.DetectResult
Expect = NewWithT(t).Expect
err error
detectResult packit.DetectResult
)

context("when no application is detected", func() {
it.Before(func() {
workingDir = t.TempDir()
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("indicates it does not participate", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).To(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedNotDetectBuildPlan))
})
}, spec.Sequential())

context("when an application is auto detected in the default working dir", func() {
it.Before(func() {
workingDir = t.TempDir()
Expect(os.WriteFile(filepath.Join(workingDir, "server.js"), nil, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("detects", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}, spec.Sequential())

context("when an application is auto detected in directory set by BP_NODE_PROJECT_PATH", func() {
it.Before(func() {
workingDir = t.TempDir()
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", "server.js"), nil, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("detects", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}, spec.Sequential())

context("when an application is detected based on BP_LAUNCHPOINT in the default working dir", func() {
it.Before(func() {
workingDir = t.TempDir()
t.Setenv("BP_LAUNCHPOINT", "not_a_known_name.js")
Expect(os.WriteFile(filepath.Join(workingDir, "not_a_known_name.js"), nil, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("detects", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}, spec.Sequential())

context("when an application is detected based on BP_LAUNCHPOINT in directory set by BP_NODE_PROJECT_PATH", func() {
it.Before(func() {
workingDir = t.TempDir()
t.Setenv("BP_NODE_PROJECT_PATH", "./src")
t.Setenv("BP_LAUNCHPOINT", "./src/not_a_known_name.js")
Expect(os.MkdirAll(filepath.Join(workingDir, "src"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "src", "not_a_known_name.js"), nil, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
it("it returns a plan that provides node and/or npm", func() {
detectResult, err = ubinodejsextension.Detect()(packit.DetectContext{
WorkingDir: "/working-dir",
})

it("detects", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}, spec.Sequential())

context("when there is a package.json without a start script and no application", func() {
it.Before(func() {
workingDir = t.TempDir()
Expect(os.WriteFile(filepath.Join(workingDir, "package.json"), []byte(`{
"scripts": {
"prestart": "npm run lint",
"poststart": "npm run test"
}
}`), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("indicates it does not participate", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).To(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedNotDetectBuildPlan))
})
}, spec.Sequential())

context("when there is a package.json with start script in default directory", func() {
it.Before(func() {
workingDir = t.TempDir()
Expect(os.WriteFile(filepath.Join(workingDir, "package.json"), []byte(`{
"scripts": {
"start": "node server.js"
}
}`), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("detects", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}, spec.Sequential())

context("when there is a package.json with start script in BP_NODE_PROJECT_PATH", func() {
it.Before(func() {
workingDir = t.TempDir()
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(`{
"scripts": {
"start": "node server.js"
}
}`), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("detects", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}, spec.Sequential())

context("when there is a package.json without a start script, with application", func() {
it.Before(func() {
workingDir = t.TempDir()
Expect(os.WriteFile(filepath.Join(workingDir, "package.json"), []byte(`{
"scripts": {
"prestart": "npm run lint",
"poststart": "npm run test"
}
}`), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "server.js"), []byte(`dummy`), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "plan"), nil, 0600)).To(Succeed())

err = os.Chdir(workingDir)
Expect(err).NotTo(HaveOccurred())
detectContext = packit.DetectContext{
WorkingDir: workingDir,
}
})

it("detects", func() {
detectResult, err = ubinodejsextension.Detect()(detectContext)
Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}, spec.Sequential())

Expect(err).NotTo(HaveOccurred())
Expect(detectResult.Plan).To(Equal(expectedDetectBuildPlan))
})
}
Loading