mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-16 05:16:08 +02:00
Fixed deriving stage name from the orchestrator via env var (#3219)
* 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>
This commit is contained in:
parent
f9ad6dc048
commit
f5be111078
@ -32,7 +32,7 @@ func CheckStepActiveCommand() *cobra.Command {
|
||||
var checkStepActiveCmd = &cobra.Command{
|
||||
Use: "checkIfStepActive",
|
||||
Short: "Checks if a step is active in a defined stage.",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
PreRun: func(cmd *cobra.Command, _ []string) {
|
||||
path, _ := os.Getwd()
|
||||
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
||||
log.RegisterHook(fatalHook)
|
||||
@ -54,6 +54,13 @@ func CheckStepActiveCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
func checkIfStepActive(utils piperutils.FileUtils) error {
|
||||
// make the stageName the leading parameter
|
||||
if len(checkStepActiveOptions.stageName) == 0 && GeneralConfig.StageName != "" {
|
||||
checkStepActiveOptions.stageName = GeneralConfig.StageName
|
||||
}
|
||||
if checkStepActiveOptions.stageName == "" {
|
||||
return errors.New("stage name must not be empty")
|
||||
}
|
||||
var pConfig config.Config
|
||||
|
||||
// load project config and defaults
|
||||
@ -140,7 +147,6 @@ func addCheckStepActiveFlags(cmd *cobra.Command) {
|
||||
cmd.Flags().StringVar(&checkStepActiveOptions.stageOutputFile, "stageOutputFile", "", "Defines a file path. If set, the stage output will be written to the defined file")
|
||||
cmd.Flags().StringVar(&checkStepActiveOptions.stepOutputFile, "stepOutputFile", "", "Defines a file path. If set, the step output will be written to the defined file")
|
||||
cmd.MarkFlagRequired("step")
|
||||
cmd.MarkFlagRequired("stage")
|
||||
}
|
||||
|
||||
func initializeConfig(pConfig *config.Config) (*config.Config, error) {
|
||||
|
@ -53,17 +53,27 @@ func TestCheckStepActiveCommand(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Required flags", func(t *testing.T) {
|
||||
exp := []string{"stage", "step"}
|
||||
exp := []string{"step"}
|
||||
assert.Equal(t, exp, gotReq, "required flags incorrect")
|
||||
})
|
||||
|
||||
t.Run("Optional flags", func(t *testing.T) {
|
||||
exp := []string{"stageConfig", "stageOutputFile", "stepOutputFile", "useV1"}
|
||||
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", 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"
|
||||
@ -72,5 +82,14 @@ func TestCheckStepActiveCommand(t *testing.T) {
|
||||
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{})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user