From 90be7e43adfe2c1e3fcab40448f7eebbf0199e7f Mon Sep 17 00:00:00 2001 From: Srinikitha Kondreddy Date: Mon, 16 Sep 2024 12:32:15 +0200 Subject: [PATCH] feat: fail if script is not found in package.json file (#5029) * feat: fail if script is not found * fix tests * Fix integration tests --------- Co-authored-by: Oliver Feldmann --- cmd/npmExecuteScripts_test.go | 34 +++++++++++++++++++++-------- integration/integration_npm_test.go | 24 ++++++++++---------- pkg/npm/npm.go | 4 ++-- pkg/npm/npm_test.go | 6 ++--- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/cmd/npmExecuteScripts_test.go b/cmd/npmExecuteScripts_test.go index 878dc8f245..af1b7f828a 100644 --- a/cmd/npmExecuteScripts_test.go +++ b/cmd/npmExecuteScripts_test.go @@ -158,36 +158,52 @@ func TestNpmExecuteScripts(t *testing.T) { t.Run("Call with createBOM", func(t *testing.T) { cfg := npmExecuteScriptsOptions{CreateBOM: true, RunScripts: []string{"ci-build", "ci-test"}} - options := npm.ExecutorOptions{DefaultNpmRegistry: cfg.DefaultNpmRegistry} - - utils := newNpmMockUtilsBundle() - utils.AddFile("package.json", []byte("{\"name\": \"Test\" }")) - utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }")) + utils := npm.NewNpmMockUtilsBundle() + utils.AddFile("package.json", []byte("{\"name\": \"Test\", \"scripts\": \"ci-build\" }")) + utils.AddFile("src/package.json", []byte("{\"name\": \"Test\", \"scripts\": \"ci-test\" }")) SetConfigOptions(ConfigCommandOptions{ OpenFile: config.OpenPiperFile, }) - npmExecutor := npm.Execute{Utils: &utils, Options: options} + npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: cfg.Install, RunScripts: cfg.RunScripts}} err := runNpmExecuteScripts(&npmExecutor, &cfg, &cpe) assert.NoError(t, err) }) - t.Run("Call with production", func(t *testing.T) { - cfg := npmExecuteScriptsOptions{Production: true, RunScripts: []string{"ci-build", "ci-test"}} + t.Run("fail if script not found", func(t *testing.T) { + cfg := npmExecuteScriptsOptions{RunScripts: []string{"ci-build"}} options := npm.ExecutorOptions{DefaultNpmRegistry: cfg.DefaultNpmRegistry} utils := newNpmMockUtilsBundle() utils.AddFile("package.json", []byte("{\"name\": \"Test\" }")) - utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }")) SetConfigOptions(ConfigCommandOptions{ OpenFile: config.OpenPiperFile, }) npmExecutor := npm.Execute{Utils: &utils, Options: options} + wantError := "could not find any package.json file with script : ci-build " + err := runNpmExecuteScripts(&npmExecutor, &cfg, &cpe) + assert.EqualErrorf(t, err, wantError, "expected to exit with error") + }) + + t.Run("Call with production", func(t *testing.T) { + + cfg := npmExecuteScriptsOptions{Production: true, RunScripts: []string{"ci-build", "ci-test"}} + + utils := npm.NewNpmMockUtilsBundle() + utils.AddFile("package.json", []byte("{\"name\": \"Test\", \"scripts\": \"ci-build\" }")) + utils.AddFile("src/package.json", []byte("{\"name\": \"Test\", \"scripts\": \"ci-test\" }")) + + SetConfigOptions(ConfigCommandOptions{ + OpenFile: config.OpenPiperFile, + }) + + npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: cfg.Install, RunScripts: cfg.RunScripts}} + err := runNpmExecuteScripts(&npmExecutor, &cfg, &cpe) assert.NoError(t, err) diff --git a/integration/integration_npm_test.go b/integration/integration_npm_test.go index 5dd25ff3b2..6083798abd 100644 --- a/integration/integration_npm_test.go +++ b/integration/integration_npm_test.go @@ -37,7 +37,7 @@ func TestNPMIntegrationRunScriptsWithOptions(t *testing.T) { //workaround to use test script util it is possible to set workdir for Exec call testScript := `#!/bin/sh cd /test -/piperbin/piper npmExecuteScripts --runScripts=start --scriptOptions=--tag,tag1 >test-log.txt 2>&1 +/piperbin/piper npmExecuteScripts --runScripts=start --scriptOptions=--tag,tag1 >test-log-runScriptWithOptions.txt 2>&1 ` os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) @@ -60,9 +60,9 @@ cd /test assert.NoError(t, err) assert.Equal(t, 0, code) - content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) + content, err := os.ReadFile(filepath.Join(tempDir, "/test-log-runScriptWithOptions.txt")) if err != nil { - t.Fatal("Could not read test-log.txt.", err) + t.Fatal("Could not read test-log-runScriptWithOptions.txt.", err) } output := string(content) assert.Contains(t, output, "info npmExecuteScripts - running command: npm run start -- --tag tag1") @@ -89,7 +89,7 @@ func TestNPMIntegrationRegistrySetInFlags(t *testing.T) { //workaround to use test script util it is possible to set workdir for Exec call testScript := `#!/bin/sh cd /test -/piperbin/piper npmExecuteScripts --install --runScripts=ci-build,ci-backend-unit-test --defaultNpmRegistry=https://foo.bar >test-log.txt 2>&1 +/piperbin/piper npmExecuteScripts --install --runScripts=ci-build --defaultNpmRegistry=https://foo.bar >test-log-registrySetInFlags.txt 2>&1 ` os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) @@ -112,9 +112,9 @@ cd /test assert.NoError(t, err) assert.Equal(t, 0, code) - content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) + content, err := os.ReadFile(filepath.Join(tempDir, "/test-log-registrySetInFlags.txt")) if err != nil { - t.Fatal("Could not read test-log.txt.", err) + t.Fatal("Could not read test-log-registrySetInFlags.txt.", err) } output := string(content) assert.Contains(t, output, "info npmExecuteScripts - https://foo.bar") @@ -140,7 +140,7 @@ func TestNPMIntegrationRegistrySetInNpmrc(t *testing.T) { //workaround to use test script util it is possible to set workdir for Exec call testScript := `#!/bin/sh cd /test -/piperbin/piper npmExecuteScripts --install --runScripts=ci-build,ci-backend-unit-test >test-log.txt 2>&1 +/piperbin/piper npmExecuteScripts --install --runScripts=ci-build >test-log-registrySetInNpmrc.txt 2>&1 ` os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) @@ -163,9 +163,9 @@ cd /test assert.NoError(t, err) assert.Equal(t, 0, code) - content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) + content, err := os.ReadFile(filepath.Join(tempDir, "/test-log-registrySetInNpmrc.txt")) if err != nil { - t.Fatal("Could not read test-log.txt.", err) + t.Fatal("Could not read test-log-registrySetInNpmrc.txt.", err) } output := string(content) assert.Contains(t, output, "info npmExecuteScripts - https://example.com") @@ -191,7 +191,7 @@ func TestNPMIntegrationRegistryWithTwoModules(t *testing.T) { //workaround to use test script util it is possible to set workdir for Exec call testScript := `#!/bin/sh cd /test -/piperbin/piper npmExecuteScripts --install --runScripts=ci-build,ci-backend-unit-test --defaultNpmRegistry=https://foo.bar >test-log.txt 2>&1 +/piperbin/piper npmExecuteScripts --install --runScripts=ci-build --defaultNpmRegistry=https://foo.bar >test-log-registryWithTwoModules.txt 2>&1 ` os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) @@ -214,9 +214,9 @@ cd /test assert.NoError(t, err) assert.Equal(t, 0, code) - content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) + content, err := os.ReadFile(filepath.Join(tempDir, "/test-log-registryWithTwoModules.txt")) if err != nil { - t.Fatal("Could not read test-log.txt.", err) + t.Fatal("Could not read test-log-registryWithTwoModules.txt.", err) } output := string(content) assert.Contains(t, output, "info npmExecuteScripts - https://example.com") diff --git a/pkg/npm/npm.go b/pkg/npm/npm.go index 45433e5891..4c8130c9b6 100644 --- a/pkg/npm/npm.go +++ b/pkg/npm/npm.go @@ -158,8 +158,8 @@ func (exec *Execute) RunScriptsInAllPackages(runScripts []string, runOptions []s } if len(packagesWithScript) == 0 { - log.Entry().Warnf("could not find any package.json file with script " + script) - continue + return fmt.Errorf("could not find any package.json file with script : %s ", script) + } for _, packageJSON := range packagesWithScript { diff --git a/pkg/npm/npm_test.go b/pkg/npm/npm_test.go index b207a60d28..52ef6e085a 100644 --- a/pkg/npm/npm_test.go +++ b/pkg/npm/npm_test.go @@ -280,7 +280,7 @@ func TestNpm(t *testing.T) { options := ExecutorOptions{} runScripts := []string{"ci-lint", "ci-build"} - buildDescriptorList := []string{filepath.Join("src", "package.json")} + buildDescriptorList := []string{filepath.Join("src", "package.json"), "package.json"} exec := &Execute{ Utils: &utils, @@ -289,8 +289,8 @@ func TestNpm(t *testing.T) { err := exec.RunScriptsInAllPackages(runScripts, nil, nil, false, nil, buildDescriptorList) if assert.NoError(t, err) { - if assert.Equal(t, 2, len(utils.execRunner.Calls)) { - assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-build"}}, utils.execRunner.Calls[1]) + if assert.Equal(t, 4, len(utils.execRunner.Calls)) { + assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-lint"}}, utils.execRunner.Calls[1]) } } })