Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jk1484 committed Nov 9, 2023
1 parent a08e5e2 commit 23d8971
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions cmd/npmExecuteScripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package cmd

import (
"os"
"testing"

"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/npm"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -104,17 +106,21 @@ func TestNpmExecuteScripts(t *testing.T) {
})

t.Run("Test integration with npm pkg", func(t *testing.T) {
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build"}}
cfg := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build"}}

options := npm.ExecutorOptions{DefaultNpmRegistry: config.DefaultNpmRegistry}
options := npm.ExecutorOptions{DefaultNpmRegistry: cfg.DefaultNpmRegistry}

utils := newNpmMockUtilsBundle()
utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-build\": \"\" } }"))
utils.AddFile("package-lock.json", []byte(""))

npmExecutor := npm.Execute{Utils: &utils, Options: options}

err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
SetConfigOptions(ConfigCommandOptions{
OpenFile: config.OpenPiperFile,
})

err := runNpmExecuteScripts(&npmExecutor, &cfg, &cpe)

if assert.NoError(t, err) {
if assert.Equal(t, 4, len(utils.execRunner.Calls)) {
Expand All @@ -126,17 +132,42 @@ func TestNpmExecuteScripts(t *testing.T) {
})

t.Run("Call with createBOM", func(t *testing.T) {
config := npmExecuteScriptsOptions{CreateBOM: true, RunScripts: []string{"ci-build", "ci-test"}}
cfg := npmExecuteScriptsOptions{CreateBOM: true, RunScripts: []string{"ci-build", "ci-test"}}

options := npm.ExecutorOptions{DefaultNpmRegistry: config.DefaultNpmRegistry}
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}
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
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"}}

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}
err := runNpmExecuteScripts(&npmExecutor, &cfg, &cpe)
assert.NoError(t, err)

v := os.Getenv("NODE_ENV")
assert.Equal(t, "production", v)
})
}

0 comments on commit 23d8971

Please sign in to comment.