diff --git a/ci_build_process.go b/ci_build_process.go index 205ba62f..e0dde177 100644 --- a/ci_build_process.go +++ b/ci_build_process.go @@ -1,7 +1,6 @@ package npminstall import ( - "bytes" "errors" "fmt" "os" @@ -80,16 +79,14 @@ func (r CIBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath string, args := []string{"ci", "--unsafe-perm", "--cache", cacheDir} r.logger.Subprocess("Running 'npm %s'", strings.Join(args, " ")) - buffer := bytes.NewBuffer(nil) err = r.executable.Execute(pexec.Execution{ Args: args, Dir: workingDir, - Stdout: buffer, - Stderr: buffer, + Stdout: r.logger.ActionWriter, + Stderr: r.logger.ActionWriter, Env: environment, }) if err != nil { - r.logger.Subprocess("%s", buffer.String()) return fmt.Errorf("npm ci failed: %w", err) } diff --git a/ci_build_process_test.go b/ci_build_process_test.go index 190f74ee..0b8eeb97 100644 --- a/ci_build_process_test.go +++ b/ci_build_process_test.go @@ -15,21 +15,21 @@ import ( "github.com/sclevine/spec" . "github.com/onsi/gomega" + . "github.com/paketo-buildpacks/occam/matchers" ) func testCIBuildProcess(t *testing.T, context spec.G, it spec.S) { var ( Expect = NewWithT(t).Expect - modulesDir string - cacheDir string - workingDir string - executable *fakes.Executable - executions []pexec.Execution - summer *fakes.Summer - environment *fakes.EnvironmentConfig - buffer *bytes.Buffer - commandOutput *bytes.Buffer + modulesDir string + cacheDir string + workingDir string + executable *fakes.Executable + executions []pexec.Execution + summer *fakes.Summer + environment *fakes.EnvironmentConfig + buffer *bytes.Buffer process npminstall.CIBuildProcess ) @@ -48,6 +48,8 @@ func testCIBuildProcess(t *testing.T, context spec.G, it spec.S) { executable = &fakes.Executable{} executable.ExecuteCall.Stub = func(execution pexec.Execution) error { executions = append(executions, execution) + fmt.Fprintln(execution.Stdout, "stdout output") + fmt.Fprintln(execution.Stderr, "stderr output") return nil } @@ -58,7 +60,6 @@ func testCIBuildProcess(t *testing.T, context spec.G, it spec.S) { environment.LookupCall.Returns.Found = true buffer = bytes.NewBuffer(nil) - commandOutput = bytes.NewBuffer(nil) process = npminstall.NewCIBuildProcess(executable, summer, environment, scribe.NewLogger(buffer)) }) @@ -192,13 +193,14 @@ func testCIBuildProcess(t *testing.T, context spec.G, it spec.S) { it("succeeds", func() { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", false)).To(Succeed()) - Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{ - Args: []string{"ci", "--unsafe-perm", "--cache", cacheDir}, - Dir: workingDir, - Stdout: commandOutput, - Stderr: commandOutput, - Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NODE_ENV=development"), - })) + Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{"ci", "--unsafe-perm", "--cache", cacheDir})) + Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workingDir)) + Expect(executable.ExecuteCall.Receives.Execution.Env).To(Equal(append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NODE_ENV=development"))) + Expect(buffer.String()).To(ContainLines( + fmt.Sprintf(" Running 'npm ci --unsafe-perm --cache %s'", cacheDir), + " stdout output", + " stderr output", + )) path, err := os.Readlink(filepath.Join(workingDir, "node_modules")) Expect(err).NotTo(HaveOccurred()) @@ -210,13 +212,14 @@ func testCIBuildProcess(t *testing.T, context spec.G, it spec.S) { it("succeeds", func() { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", true)).To(Succeed()) - Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{ - Args: []string{"ci", "--unsafe-perm", "--cache", cacheDir}, - Dir: workingDir, - Stdout: commandOutput, - Stderr: commandOutput, - Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - })) + Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{"ci", "--unsafe-perm", "--cache", cacheDir})) + Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workingDir)) + Expect(executable.ExecuteCall.Receives.Execution.Env).To(Equal(append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + fmt.Sprintf(" Running 'npm ci --unsafe-perm --cache %s'", cacheDir), + " stdout output", + " stderr output", + )) path, err := os.Readlink(filepath.Join(workingDir, "node_modules")) Expect(err).NotTo(HaveOccurred()) @@ -266,7 +269,11 @@ func testCIBuildProcess(t *testing.T, context spec.G, it spec.S) { it("returns an error", func() { err := process.Run(modulesDir, cacheDir, workingDir, "", false) - Expect(buffer.String()).To(ContainSubstring(" ci failure on stdout\n ci failure on stderr")) + Expect(buffer.String()).To(ContainLines( + fmt.Sprintf(" Running 'npm ci --unsafe-perm --cache %s'", cacheDir), + " ci failure on stdout", + " ci failure on stderr", + )) Expect(err).To(MatchError("npm ci failed: failed to execute")) }) }) diff --git a/install_build_process.go b/install_build_process.go index 70244fd5..bf4d5ca9 100644 --- a/install_build_process.go +++ b/install_build_process.go @@ -1,7 +1,6 @@ package npminstall import ( - "bytes" "errors" "fmt" "os" @@ -53,16 +52,14 @@ func (r InstallBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath str args := []string{"install", "--unsafe-perm", "--cache", cacheDir} r.logger.Subprocess("Running 'npm %s'", strings.Join(args, " ")) - buffer := bytes.NewBuffer(nil) err = r.executable.Execute(pexec.Execution{ Args: args, Dir: workingDir, - Stdout: buffer, - Stderr: buffer, + Stdout: r.logger.ActionWriter, + Stderr: r.logger.ActionWriter, Env: environment, }) if err != nil { - r.logger.Subprocess("%s", buffer.String()) return fmt.Errorf("npm install failed: %w", err) } diff --git a/install_build_process_test.go b/install_build_process_test.go index b540bee0..eb824f8a 100644 --- a/install_build_process_test.go +++ b/install_build_process_test.go @@ -15,19 +15,19 @@ import ( "github.com/sclevine/spec" . "github.com/onsi/gomega" + . "github.com/paketo-buildpacks/occam/matchers" ) func testInstallBuildProcess(t *testing.T, context spec.G, it spec.S) { var ( Expect = NewWithT(t).Expect - modulesDir string - cacheDir string - workingDir string - executable *fakes.Executable - environment *fakes.EnvironmentConfig - buffer *bytes.Buffer - commandOutput *bytes.Buffer + modulesDir string + cacheDir string + workingDir string + executable *fakes.Executable + environment *fakes.EnvironmentConfig + buffer *bytes.Buffer process npminstall.InstallBuildProcess ) @@ -44,13 +44,17 @@ func testInstallBuildProcess(t *testing.T, context spec.G, it spec.S) { Expect(err).NotTo(HaveOccurred()) executable = &fakes.Executable{} + executable.ExecuteCall.Stub = func(execution pexec.Execution) error { + fmt.Fprintln(execution.Stdout, "stdout output") + fmt.Fprintln(execution.Stderr, "stderr output") + return nil + } environment = &fakes.EnvironmentConfig{} environment.LookupCall.Returns.Value = "some-val" environment.LookupCall.Returns.Found = true buffer = bytes.NewBuffer(nil) - commandOutput = bytes.NewBuffer(nil) process = npminstall.NewInstallBuildProcess(executable, environment, scribe.NewLogger(buffer)) }) @@ -78,13 +82,14 @@ func testInstallBuildProcess(t *testing.T, context spec.G, it spec.S) { context("launch is false", func() { it("succeeds", func() { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", false)).To(Succeed()) - Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{ - Args: []string{"install", "--unsafe-perm", "--cache", cacheDir}, - Dir: workingDir, - Stdout: commandOutput, - Stderr: commandOutput, - Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NODE_ENV=development"), - })) + Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{"install", "--unsafe-perm", "--cache", cacheDir})) + Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workingDir)) + Expect(executable.ExecuteCall.Receives.Execution.Env).To(Equal(append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NODE_ENV=development"))) + Expect(buffer.String()).To(ContainLines( + fmt.Sprintf(" Running 'npm install --unsafe-perm --cache %s'", cacheDir), + " stdout output", + " stderr output", + )) path, err := os.Readlink(filepath.Join(workingDir, "node_modules")) Expect(err).NotTo(HaveOccurred()) @@ -95,13 +100,14 @@ func testInstallBuildProcess(t *testing.T, context spec.G, it spec.S) { context("launch is true", func() { it("succeeds", func() { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", true)).To(Succeed()) - Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{ - Args: []string{"install", "--unsafe-perm", "--cache", cacheDir}, - Dir: workingDir, - Stdout: commandOutput, - Stderr: commandOutput, - Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - })) + Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{"install", "--unsafe-perm", "--cache", cacheDir})) + Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workingDir)) + Expect(executable.ExecuteCall.Receives.Execution.Env).To(Equal(append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + fmt.Sprintf(" Running 'npm install --unsafe-perm --cache %s'", cacheDir), + " stdout output", + " stderr output", + )) path, err := os.Readlink(filepath.Join(workingDir, "node_modules")) Expect(err).NotTo(HaveOccurred()) @@ -148,7 +154,11 @@ func testInstallBuildProcess(t *testing.T, context spec.G, it spec.S) { it("returns an error", func() { err := process.Run(modulesDir, cacheDir, workingDir, "", true) - Expect(buffer.String()).To(ContainSubstring(" install error on stdout\n install error on stderr\n")) + Expect(buffer.String()).To(ContainLines( + fmt.Sprintf(" Running 'npm install --unsafe-perm --cache %s'", cacheDir), + " install error on stdout", + " install error on stderr", + )) Expect(err).To(MatchError("npm install failed: failed to execute")) }) }) diff --git a/integration/caching_test.go b/integration/caching_test.go index a61bc5b0..9a9c5fe1 100644 --- a/integration/caching_test.go +++ b/integration/caching_test.go @@ -147,12 +147,13 @@ func testCaching(t *testing.T, context spec.G, it spec.S) { " npm-cache -> \"Not found\"", " package-lock.json -> \"Found\"", "", - " Selected NPM build process: 'npm ci'", - "", + " Selected NPM build process: 'npm ci'")) + Expect(logs).To(ContainLines( " Executing launch environment install process", fmt.Sprintf(" Running 'npm ci --unsafe-perm --cache /layers/%s/npm-cache'", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")), - MatchRegexp(` Completed in (\d+\.\d+|\d{3})`), - "", + )) + Expect(logs).To(ContainLines(MatchRegexp(` Completed in (\d+\.\d+|\d{3})`))) + Expect(logs).To(ContainLines( " Configuring launch environment", " NODE_PROJECT_PATH -> \"/workspace\"", " NPM_CONFIG_LOGLEVEL -> \"error\"", diff --git a/integration/logging_test.go b/integration/logging_test.go index 893a5eed..4feb618b 100644 --- a/integration/logging_test.go +++ b/integration/logging_test.go @@ -72,16 +72,19 @@ func testLogging(t *testing.T, context spec.G, it spec.S) { " package-lock.json -> \"Not found\"", "", " Selected NPM build process: 'npm install'", - "", + )) + Expect(logs).To(ContainLines( " Executing launch environment install process", fmt.Sprintf(" Running 'npm install --unsafe-perm --cache /layers/%s/npm-cache'", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")), + )) + Expect(logs).To(ContainLines( MatchRegexp(` Completed in (\d+\.\d+|\d{3})`), - "", + )) + Expect(logs).To(ContainLines( " Configuring launch environment", " NODE_PROJECT_PATH -> \"/workspace\"", " NPM_CONFIG_LOGLEVEL -> \"error\"", fmt.Sprintf(" PATH -> \"$PATH:/layers/%s/launch-modules/node_modules/.bin\"", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")), - "", )) }) @@ -110,23 +113,27 @@ func testLogging(t *testing.T, context spec.G, it spec.S) { " npm-cache -> \"Not found\"", " package-lock.json -> \"Not found\"", "", - " Selected NPM build process: 'npm install'", - "", + " Selected NPM build process: 'npm install'")) + Expect(logs).To(ContainLines( " Executing build environment install process", fmt.Sprintf(" Running 'npm install --unsafe-perm --cache /layers/%s/npm-cache'", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")), + )) + Expect(logs).To(ContainLines( MatchRegexp(` Completed in (\d+\.\d+|\d{3})`), - "", + )) + Expect(logs).To(ContainLines( " Configuring build environment", " NODE_ENV -> \"development\"", fmt.Sprintf(" PATH -> \"$PATH:/layers/%s/build-modules/node_modules/.bin\"", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")), "", fmt.Sprintf(` Generating SBOM for /layers/%s/build-modules`, strings.ReplaceAll(settings.Buildpack.ID, "/", "_")), MatchRegexp(` Completed in (\d+)(\.\d+)?(ms|s)`), - "", + )) + Expect(logs).To(ContainLines( " Executing launch environment install process", " Running 'npm prune'", - MatchRegexp(` Completed in (\d+\.\d+|\d{3})`), - "", + )) + Expect(logs).To(ContainLines( " Configuring launch environment", " NODE_PROJECT_PATH -> \"/workspace\"", " NPM_CONFIG_LOGLEVEL -> \"error\"", @@ -134,7 +141,6 @@ func testLogging(t *testing.T, context spec.G, it spec.S) { "", fmt.Sprintf(` Generating SBOM for /layers/%s/launch-modules`, strings.ReplaceAll(settings.Buildpack.ID, "/", "_")), MatchRegexp(` Completed in (\d+)(\.\d+)?(ms|s)`), - "", )) }) diff --git a/integration/vendored_test.go b/integration/vendored_test.go index eba149d5..bd518d7e 100644 --- a/integration/vendored_test.go +++ b/integration/vendored_test.go @@ -96,16 +96,11 @@ func testVendored(t *testing.T, context spec.G, it spec.S) { " npm-cache -> \"Not found\"", " package-lock.json -> \"Found\"", "", - " Selected NPM build process: 'npm rebuild'", - "", - " Executing launch environment install process", - " Running 'npm run-script preinstall --if-present'", - MatchRegexp(` Running 'npm rebuild --nodedir=/layers/.+/node'`), - " Running 'npm run-script postinstall --if-present'", - )) - Expect(logs).To(ContainLines( - MatchRegexp(` Completed in (\d+\.\d+|\d{3})`), - )) + " Selected NPM build process: 'npm rebuild'")) + Expect(logs).To(ContainLines(" Executing launch environment install process")) + Expect(logs).To(ContainLines(" Running 'npm run-script preinstall --if-present'")) + Expect(logs).To(ContainLines(MatchRegexp(` Running 'npm rebuild --nodedir=/layers/.+/node'`))) + Expect(logs).To(ContainLines(" Running 'npm run-script postinstall --if-present'")) Expect(logs).To(ContainLines( " Configuring launch environment", " NODE_PROJECT_PATH -> \"/workspace\"", diff --git a/prune_build_process.go b/prune_build_process.go index 8bfa87c7..0d0fe811 100644 --- a/prune_build_process.go +++ b/prune_build_process.go @@ -1,7 +1,6 @@ package npminstall import ( - "bytes" "fmt" "os" "strings" @@ -41,16 +40,14 @@ func (r PruneBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath strin args := []string{"prune"} r.logger.Subprocess("Running 'npm %s'", strings.Join(args, " ")) - buffer := bytes.NewBuffer(nil) err := r.executable.Execute(pexec.Execution{ Args: args, Dir: workingDir, - Stdout: buffer, - Stderr: buffer, + Stdout: r.logger.ActionWriter, + Stderr: r.logger.ActionWriter, Env: environment, }) if err != nil { - r.logger.Subprocess("%s", buffer.String()) return fmt.Errorf("npm install failed: %w", err) } diff --git a/prune_build_process_test.go b/prune_build_process_test.go index bcd94fdc..fc04b2a3 100644 --- a/prune_build_process_test.go +++ b/prune_build_process_test.go @@ -14,19 +14,19 @@ import ( "github.com/sclevine/spec" . "github.com/onsi/gomega" + . "github.com/paketo-buildpacks/occam/matchers" ) func testPruneBuildProcess(t *testing.T, context spec.G, it spec.S) { var ( Expect = NewWithT(t).Expect - modulesDir string - cacheDir string - workingDir string - executable *fakes.Executable - environment *fakes.EnvironmentConfig - buffer *bytes.Buffer - commandOutput *bytes.Buffer + modulesDir string + cacheDir string + workingDir string + executable *fakes.Executable + environment *fakes.EnvironmentConfig + buffer *bytes.Buffer process npminstall.PruneBuildProcess ) @@ -43,13 +43,17 @@ func testPruneBuildProcess(t *testing.T, context spec.G, it spec.S) { Expect(err).NotTo(HaveOccurred()) executable = &fakes.Executable{} + executable.ExecuteCall.Stub = func(execution pexec.Execution) error { + fmt.Fprintln(execution.Stdout, "stdout output") + fmt.Fprintln(execution.Stderr, "stderr output") + return nil + } environment = &fakes.EnvironmentConfig{} environment.LookupCall.Returns.Value = "some-val" environment.LookupCall.Returns.Found = true buffer = bytes.NewBuffer(nil) - commandOutput = bytes.NewBuffer(nil) process = npminstall.NewPruneBuildProcess(executable, environment, scribe.NewLogger(buffer)) }) @@ -72,13 +76,14 @@ func testPruneBuildProcess(t *testing.T, context spec.G, it spec.S) { context("Run", func() { it("succeeds", func() { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", true)).To(Succeed()) - Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{ - Args: []string{"prune"}, - Dir: workingDir, - Stdout: commandOutput, - Stderr: commandOutput, - Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - })) + Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{"prune"})) + Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workingDir)) + Expect(executable.ExecuteCall.Receives.Execution.Env).To(Equal(append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm prune'", + " stdout output", + " stderr output", + )) }) context("failure cases", func() { @@ -93,7 +98,11 @@ func testPruneBuildProcess(t *testing.T, context spec.G, it spec.S) { it("returns an error", func() { err := process.Run(modulesDir, cacheDir, workingDir, "", true) - Expect(buffer.String()).To(ContainSubstring(" install error on stdout\n install error on stderr\n")) + Expect(buffer.String()).To(ContainLines( + " Running 'npm prune'", + " install error on stdout", + " install error on stderr", + )) Expect(err).To(MatchError("npm install failed: failed to execute")) }) }) diff --git a/rebuild_build_process.go b/rebuild_build_process.go index 114fd4aa..9aa9c089 100644 --- a/rebuild_build_process.go +++ b/rebuild_build_process.go @@ -1,7 +1,6 @@ package npminstall import ( - "bytes" "errors" "fmt" "os" @@ -55,7 +54,6 @@ func (r RebuildBuildProcess) ShouldRun(workingDir string, metadata map[string]in } func (r RebuildBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath string, launch bool) error { - buffer := bytes.NewBuffer(nil) environment := os.Environ() if npmrcPath != "" { environment = append(environment, fmt.Sprintf("NPM_CONFIG_GLOBALCONFIG=%s", npmrcPath)) @@ -65,11 +63,10 @@ func (r RebuildBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath str Args: []string{"list"}, Dir: workingDir, Env: environment, - Stdout: buffer, - Stderr: buffer, + Stdout: r.logger.ActionWriter, + Stderr: r.logger.ActionWriter, }) if err != nil { - r.logger.Subprocess("%s", buffer.String()) return fmt.Errorf("vendored node_modules have unmet dependencies: npm list failed: %w", err) } @@ -79,12 +76,11 @@ func (r RebuildBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath str Args: args, Dir: workingDir, Env: environment, - Stdout: buffer, - Stderr: buffer, + Stdout: r.logger.ActionWriter, + Stderr: r.logger.ActionWriter, }) if err != nil { - r.logger.Subprocess("%s", buffer.String()) return fmt.Errorf("preinstall script failed on rebuild: %s", err) } @@ -103,12 +99,11 @@ func (r RebuildBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath str err = r.executable.Execute(pexec.Execution{ Args: args, Dir: workingDir, - Stdout: buffer, - Stderr: buffer, + Stdout: r.logger.ActionWriter, + Stderr: r.logger.ActionWriter, Env: env, }) if err != nil { - r.logger.Subprocess("%s", buffer.String()) return fmt.Errorf("npm rebuild failed: %s", err) } @@ -118,12 +113,11 @@ func (r RebuildBuildProcess) Run(modulesDir, cacheDir, workingDir, npmrcPath str Args: args, Dir: workingDir, Env: environment, - Stdout: buffer, - Stderr: buffer, + Stdout: r.logger.ActionWriter, + Stderr: r.logger.ActionWriter, }) if err != nil { - r.logger.Subprocess("%s", buffer.String()) return fmt.Errorf("postinstall script failed on rebuild: %s", err) } diff --git a/rebuild_build_process_test.go b/rebuild_build_process_test.go index 182f1011..45a603d4 100644 --- a/rebuild_build_process_test.go +++ b/rebuild_build_process_test.go @@ -16,6 +16,7 @@ import ( "github.com/sclevine/spec" . "github.com/onsi/gomega" + . "github.com/paketo-buildpacks/occam/matchers" ) func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { @@ -32,8 +33,7 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { summer *fakes.Summer environment *fakes.EnvironmentConfig - buffer *bytes.Buffer - commandOutput *bytes.Buffer + buffer *bytes.Buffer process npminstall.RebuildBuildProcess ) @@ -57,6 +57,8 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { executable = &fakes.Executable{} executable.ExecuteCall.Stub = func(execution pexec.Execution) error { executions = append(executions, execution) + fmt.Fprintln(execution.Stdout, "stdout output") + fmt.Fprintln(execution.Stderr, "stderr output") return nil } @@ -73,7 +75,6 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { } buffer = bytes.NewBuffer(nil) - commandOutput = bytes.NewBuffer(nil) process = npminstall.NewRebuildBuildProcess(executable, summer, environment, scribe.NewLogger(buffer)) }) @@ -188,34 +189,36 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", false)).To(Succeed()) Expect(executable.ExecuteCall.CallCount).To(Equal(4)) - Expect(executions[0]).To(Equal(pexec.Execution{ - Args: []string{"list"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - })) - Expect(executions[1]).To(Equal(pexec.Execution{ - Args: []string{"run-script", "preinstall", "--if-present"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - })) - Expect(executions[2]).To(Equal(pexec.Execution{ - Args: []string{"rebuild", "--nodedir="}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NPM_CONFIG_LOGLEVEL=some-val", "NODE_ENV=development"), - Stdout: commandOutput, - Stderr: commandOutput, - })) - Expect(executions[3]).To(Equal(pexec.Execution{ - Args: []string{"run-script", "postinstall", "--if-present"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - })) + Expect(executions[0].Args).To(Equal([]string{"list"})) + Expect(executions[0].Dir).To(Equal(workingDir)) + Expect(executions[0].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + + Expect(executions[1].Args).To(Equal([]string{"run-script", "preinstall", "--if-present"})) + Expect(executions[1].Dir).To(Equal(workingDir)) + Expect(executions[1].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script preinstall --if-present'", + " stdout output", + " stderr output", + )) + + Expect(executions[2].Args).To(Equal([]string{"rebuild", "--nodedir="})) + Expect(executions[2].Dir).To(Equal(workingDir)) + Expect(executions[2].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NPM_CONFIG_LOGLEVEL=some-val", "NODE_ENV=development"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm rebuild --nodedir='", + " stdout output", + " stderr output", + )) + + Expect(executions[3].Args).To(Equal([]string{"run-script", "postinstall", "--if-present"})) + Expect(executions[3].Dir).To(Equal(workingDir)) + Expect(executions[3].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script postinstall --if-present'", + " stdout output", + " stderr output", + )) path, err := os.Readlink(filepath.Join(workingDir, "node_modules")) Expect(err).NotTo(HaveOccurred()) @@ -228,34 +231,36 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", true)).To(Succeed()) Expect(executable.ExecuteCall.CallCount).To(Equal(4)) - Expect(executions[0]).To(Equal(pexec.Execution{ - Args: []string{"list"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - })) - Expect(executions[1]).To(Equal(pexec.Execution{ - Args: []string{"run-script", "preinstall", "--if-present"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - })) - Expect(executions[2]).To(Equal(pexec.Execution{ - Args: []string{"rebuild", "--nodedir="}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NPM_CONFIG_LOGLEVEL=some-val"), - Stdout: commandOutput, - Stderr: commandOutput, - })) - Expect(executions[3]).To(Equal(pexec.Execution{ - Args: []string{"run-script", "postinstall", "--if-present"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - })) + Expect(executions[0].Args).To(Equal([]string{"list"})) + Expect(executions[0].Dir).To(Equal(workingDir)) + Expect(executions[0].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + + Expect(executions[1].Args).To(Equal([]string{"run-script", "preinstall", "--if-present"})) + Expect(executions[1].Dir).To(Equal(workingDir)) + Expect(executions[1].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script preinstall --if-present'", + " stdout output", + " stderr output", + )) + + Expect(executions[2].Args).To(Equal([]string{"rebuild", "--nodedir="})) + Expect(executions[2].Dir).To(Equal(workingDir)) + Expect(executions[2].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NPM_CONFIG_LOGLEVEL=some-val"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm rebuild --nodedir='", + " stdout output", + " stderr output", + )) + + Expect(executions[3].Args).To(Equal([]string{"run-script", "postinstall", "--if-present"})) + Expect(executions[3].Dir).To(Equal(workingDir)) + Expect(executions[3].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script postinstall --if-present'", + " stdout output", + " stderr output", + )) path, err := os.Readlink(filepath.Join(workingDir, "node_modules")) Expect(err).NotTo(HaveOccurred()) @@ -268,36 +273,36 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", true)).To(Succeed()) Expect(executable.ExecuteCall.CallCount).To(Equal(4)) - Expect(executions).To(Equal([]pexec.Execution{ - { - Args: []string{"list"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - }, - { - Args: []string{"run-script", "preinstall", "--if-present"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - }, - { - Args: []string{"rebuild", "--nodedir="}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NPM_CONFIG_LOGLEVEL=some-val"), - Stdout: commandOutput, - Stderr: commandOutput, - }, - { - Args: []string{"run-script", "postinstall", "--if-present"}, - Dir: workingDir, - Env: append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"), - Stdout: commandOutput, - Stderr: commandOutput, - }, - })) + Expect(executions[0].Args).To(Equal([]string{"list"})) + Expect(executions[0].Dir).To(Equal(workingDir)) + Expect(executions[0].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + + Expect(executions[1].Args).To(Equal([]string{"run-script", "preinstall", "--if-present"})) + Expect(executions[1].Dir).To(Equal(workingDir)) + Expect(executions[1].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script preinstall --if-present'", + " stdout output", + " stderr output", + )) + + Expect(executions[2].Args).To(Equal([]string{"rebuild", "--nodedir="})) + Expect(executions[2].Dir).To(Equal(workingDir)) + Expect(executions[2].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NPM_CONFIG_LOGLEVEL=some-val"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm rebuild --nodedir='", + " stdout output", + " stderr output", + )) + + Expect(executions[3].Args).To(Equal([]string{"run-script", "postinstall", "--if-present"})) + Expect(executions[3].Dir).To(Equal(workingDir)) + Expect(executions[3].Env).To(Equal(append(os.Environ(), "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"))) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script postinstall --if-present'", + " stdout output", + " stderr output", + )) path, err := os.Readlink(filepath.Join(workingDir, "node_modules")) Expect(err).NotTo(HaveOccurred()) @@ -319,7 +324,10 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { } err := process.Run(modulesDir, cacheDir, workingDir, "", true) - Expect(buffer.String()).To(ContainSubstring(" stdout output\n stderr output\n")) + Expect(buffer.String()).To(ContainLines( + " stdout output", + " stderr output", + )) Expect(err).To(MatchError("vendored node_modules have unmet dependencies: npm list failed: exit status 1")) }) }) @@ -354,7 +362,11 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { it("returns an error", func() { err := process.Run(modulesDir, cacheDir, workingDir, "", true) - Expect(buffer.String()).To(ContainSubstring(" pre-install on stdout\n pre-install on stderr\n")) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script preinstall --if-present'", + " pre-install on stdout", + " pre-install on stderr", + )) Expect(err).To(MatchError("preinstall script failed on rebuild: an actual error")) }) }) @@ -374,7 +386,11 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { it("returns an error", func() { err := process.Run(modulesDir, cacheDir, workingDir, "", true) - Expect(buffer.String()).To(ContainSubstring(" rebuild error on stdout\n rebuild error on stderr\n")) + Expect(buffer.String()).To(ContainLines( + " Running 'npm rebuild --nodedir='", + " rebuild error on stdout", + " rebuild error on stderr", + )) Expect(err).To(MatchError("npm rebuild failed: failed to rebuild")) }) }) @@ -394,7 +410,11 @@ func testRebuildBuildProcess(t *testing.T, context spec.G, it spec.S) { it("returns an error", func() { err := process.Run(modulesDir, cacheDir, workingDir, "", true) - Expect(buffer.String()).To(ContainSubstring(" postinstall on stdout\n postinstall on stderr\n")) + Expect(buffer.String()).To(ContainLines( + " Running 'npm run-script postinstall --if-present'", + " postinstall on stdout", + " postinstall on stderr", + )) Expect(err).To(MatchError("postinstall script failed on rebuild: an actual error")) }) })