1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/getDefaults_test.go
Christopher Fenner ebcbb73b46
feat: add command to fetch default files (#3445)
* some ideas..

* Add getDefaults command (WIP) (#3444)

* add getYAML function for configs

* create getDefaults command(based on getConfig)

* add getDefaults command to CLI

* read defaults files, using github tokens as well

* write defaults to stdout as JSON object with YAMLs embedded

* catch case where no input files are given

* actually write output to file if outputFile is specified

* mark defaultsFile flag as required

* add basic tests

* add output (string)  test

* adapt generateDefaults() to return output (used for test of previous commit)

* Changes to getDefaults() JSON output (#3449)

* change JSON output to contain separate fields

* filename -> filepath

* Apply suggestions from code review

* Apply suggestions from code review

* Update pkg/config/config_test.go

Co-authored-by: Jordi van Liempt <35920075+jliempt@users.noreply.github.com>
2022-01-24 13:55:28 +01:00

91 lines
2.2 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 defaultsOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) {
var r string
switch name {
case "TestAddCustomDefaults_default1":
r = "default1"
case "TestAddCustomDefaults_default2":
r = "default3"
default:
r = ""
}
return ioutil.NopCloser(strings.NewReader(r)), nil
}
func TestDefaultsCommand(t *testing.T) {
cmd := DefaultsCommand()
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{"defaultsFile"}
assert.Equal(t, exp, gotReq, "required flags incorrect")
})
t.Run("Optional flags", func(t *testing.T) {
exp := []string{"output", "outputFile"}
assert.Equal(t, exp, gotOpt, "optional flags incorrect")
})
t.Run("Run", func(t *testing.T) {
t.Run("Success case", func(t *testing.T) {
defaultsOptions.openFile = defaultsOpenFileMock
defaultsOptions.defaultsFiles = []string{"test", "test"}
cmd.Run(cmd, []string{})
})
})
}
func TestGenerateDefaults(t *testing.T) {
testParams := []struct {
name string
defaultsFiles []string
expected string
}{
{
name: "Single defaults file",
defaultsFiles: []string{"test"},
expected: `{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test"}`,
},
{
name: "Multiple defaults files",
defaultsFiles: []string{"test1", "test2"},
expected: `[{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test1"},` +
`{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test2"}]`,
},
}
utils := newGetDefaultsUtilsUtils()
defaultsOptions.openFile = defaultsOpenFileMock
for _, test := range testParams {
t.Run(test.name, func(t *testing.T) {
defaultsOptions.defaultsFiles = test.defaultsFiles
result, _ := generateDefaults(utils)
assert.Equal(t, test.expected, string(result))
})
}
}