Skip to content

Commit

Permalink
feat: fail if script is not found in package.json file (#5029)
Browse files Browse the repository at this point in the history
* feat: fail if script is not found

* fix tests

* Fix integration tests

---------

Co-authored-by: Oliver Feldmann <[email protected]>
  • Loading branch information
srinikitha09 and o-liver authored Sep 16, 2024
1 parent a2bafe2 commit 90be7e4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
34 changes: 25 additions & 9 deletions cmd/npmExecuteScripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 12 additions & 12 deletions integration/integration_npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pkg/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/npm/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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])
}
}
})
Expand Down

0 comments on commit 90be7e4

Please sign in to comment.