From 73a572d06391b2b00709dcf39389bec8371afcb7 Mon Sep 17 00:00:00 2001 From: Costas Papastathis Date: Thu, 30 Nov 2023 10:38:27 +0200 Subject: [PATCH] removing conditionaly providing node --- detect.go | 30 ------- detect_test.go | 227 ++----------------------------------------------- 2 files changed, 9 insertions(+), 248 deletions(-) diff --git a/detect.go b/detect.go index bddc952..c728fb3 100644 --- a/detect.go +++ b/detect.go @@ -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{ diff --git a/detect_test.go b/detect_test.go index 03bbb11..55136d9 100644 --- a/detect_test.go +++ b/detect_test.go @@ -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" @@ -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)) + }) }