mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-16 11:09:33 +02:00
f5be111078
* Fixed deriving stage name from the orchestrator via env var * make the stageName the leading parameter * Added tests * Update cmd/checkIfStepActive.go * Update cmd/checkIfStepActive_test.go * Update cmd/checkIfStepActive_test.go * Update cmd/checkIfStepActive.go Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
flag "github.com/spf13/pflag"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func checkStepActiveOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) {
|
|
var fileContent string
|
|
switch name {
|
|
case ".pipeline/defaults.yaml":
|
|
fileContent = `
|
|
general:
|
|
stages:
|
|
steps:`
|
|
case "stage-config.yml":
|
|
fileContent = `
|
|
stages:
|
|
testStage:
|
|
stepConditions:
|
|
testStep:
|
|
config: testConfig`
|
|
case ".pipeline/config.yml":
|
|
fileContent = `
|
|
steps:
|
|
testStep:
|
|
testConfig: 'testValue'`
|
|
default:
|
|
fileContent = ""
|
|
}
|
|
return ioutil.NopCloser(strings.NewReader(fileContent)), nil
|
|
}
|
|
|
|
func TestCheckStepActiveCommand(t *testing.T) {
|
|
cmd := CheckStepActiveCommand()
|
|
|
|
gotReq := []string{}
|
|
gotOpt := []string{}
|
|
|
|
cmd.Flags().VisitAll(func(pflag *flag.Flag) {
|
|
annotations, found := pflag.Annotations[cobra.BashCompOneRequiredFlag]
|
|
if found && annotations[0] == "true" {
|
|
gotReq = append(gotReq, pflag.Name)
|
|
} else {
|
|
gotOpt = append(gotOpt, pflag.Name)
|
|
}
|
|
})
|
|
|
|
t.Run("Required flags", func(t *testing.T) {
|
|
exp := []string{"step"}
|
|
assert.Equal(t, exp, gotReq, "required flags incorrect")
|
|
})
|
|
|
|
t.Run("Optional flags", func(t *testing.T) {
|
|
exp := []string{"stage", "stageConfig", "stageOutputFile", "stepOutputFile", "useV1"}
|
|
assert.Equal(t, exp, gotOpt, "optional flags incorrect")
|
|
})
|
|
|
|
t.Run("Run", func(t *testing.T) {
|
|
t.Run("Success case - set stage and stageName parameters", func(t *testing.T) {
|
|
checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
|
|
checkStepActiveOptions.stageName = "testStage"
|
|
checkStepActiveOptions.stepName = "testStep"
|
|
checkStepActiveOptions.stageConfigFile = "stage-config.yml"
|
|
GeneralConfig.CustomConfig = ".pipeline/config.yml"
|
|
GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
|
|
GeneralConfig.StageName = "testStage1"
|
|
cmd.Run(cmd, []string{})
|
|
})
|
|
t.Run("Success case - set only stage parameter", func(t *testing.T) {
|
|
checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
|
|
checkStepActiveOptions.stageName = "testStage"
|
|
checkStepActiveOptions.stepName = "testStep"
|
|
checkStepActiveOptions.stageConfigFile = "stage-config.yml"
|
|
GeneralConfig.CustomConfig = ".pipeline/config.yml"
|
|
GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
|
|
cmd.Run(cmd, []string{})
|
|
})
|
|
t.Run("Success case - set only stageName parameter", func(t *testing.T) {
|
|
checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
|
|
checkStepActiveOptions.stepName = "testStep"
|
|
checkStepActiveOptions.stageConfigFile = "stage-config.yml"
|
|
GeneralConfig.CustomConfig = ".pipeline/config.yml"
|
|
GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
|
|
GeneralConfig.StageName = "testStage"
|
|
cmd.Run(cmd, []string{})
|
|
})
|
|
})
|
|
}
|