github.com/xgoffin/jenkins-library@v1.154.0/cmd/checkIfStepActive_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/spf13/cobra"
    10  	flag "github.com/spf13/pflag"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func checkStepActiveOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) {
    15  	var fileContent string
    16  	switch name {
    17  	case ".pipeline/defaults.yaml":
    18  		fileContent = `
    19  general:
    20  stages:
    21  steps:`
    22  	case "stage-config.yml":
    23  		fileContent = `
    24  stages:
    25    testStage:
    26      stepConditions:
    27        testStep:
    28          config: testConfig`
    29  	case ".pipeline/config.yml":
    30  		fileContent = `
    31  steps: 
    32    testStep: 
    33      testConfig: 'testValue'`
    34  	default:
    35  		fileContent = ""
    36  	}
    37  	return ioutil.NopCloser(strings.NewReader(fileContent)), nil
    38  }
    39  
    40  func TestCheckStepActiveCommand(t *testing.T) {
    41  	cmd := CheckStepActiveCommand()
    42  
    43  	gotReq := []string{}
    44  	gotOpt := []string{}
    45  
    46  	cmd.Flags().VisitAll(func(pflag *flag.Flag) {
    47  		annotations, found := pflag.Annotations[cobra.BashCompOneRequiredFlag]
    48  		if found && annotations[0] == "true" {
    49  			gotReq = append(gotReq, pflag.Name)
    50  		} else {
    51  			gotOpt = append(gotOpt, pflag.Name)
    52  		}
    53  	})
    54  
    55  	t.Run("Required flags", func(t *testing.T) {
    56  		exp := []string{"step"}
    57  		assert.Equal(t, exp, gotReq, "required flags incorrect")
    58  	})
    59  
    60  	t.Run("Optional flags", func(t *testing.T) {
    61  		exp := []string{"stage", "stageConfig", "stageOutputFile", "stepOutputFile", "useV1"}
    62  		assert.Equal(t, exp, gotOpt, "optional flags incorrect")
    63  	})
    64  
    65  	t.Run("Run", func(t *testing.T) {
    66  		t.Run("Success case - set stage and stageName parameters", func(t *testing.T) {
    67  			checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
    68  			checkStepActiveOptions.stageName = "testStage"
    69  			checkStepActiveOptions.stepName = "testStep"
    70  			checkStepActiveOptions.stageConfigFile = "stage-config.yml"
    71  			GeneralConfig.CustomConfig = ".pipeline/config.yml"
    72  			GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
    73  			GeneralConfig.StageName = "testStage1"
    74  			cmd.Run(cmd, []string{})
    75  		})
    76  		t.Run("Success case - set only stage parameter", func(t *testing.T) {
    77  			checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
    78  			checkStepActiveOptions.stageName = "testStage"
    79  			checkStepActiveOptions.stepName = "testStep"
    80  			checkStepActiveOptions.stageConfigFile = "stage-config.yml"
    81  			GeneralConfig.CustomConfig = ".pipeline/config.yml"
    82  			GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
    83  			cmd.Run(cmd, []string{})
    84  		})
    85  		t.Run("Success case - set only stageName parameter", func(t *testing.T) {
    86  			checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
    87  			checkStepActiveOptions.stepName = "testStep"
    88  			checkStepActiveOptions.stageConfigFile = "stage-config.yml"
    89  			GeneralConfig.CustomConfig = ".pipeline/config.yml"
    90  			GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
    91  			GeneralConfig.StageName = "testStage"
    92  			cmd.Run(cmd, []string{})
    93  		})
    94  	})
    95  }