Skip to content

Commit

Permalink
Fix issue where passing arguments to generate command in unit test wa…
Browse files Browse the repository at this point in the history
…s not functioning

Issue was caused by command library expecting a command separated slice instead of space saparated string

Co-authored-by: Marco Franssen <marco.franssen@philips.com>
Signed-off-by: Brend Smits <brend.smits@philips.com>
  • Loading branch information
Brend-Smits and marcofranssen committed Oct 12, 2021
1 parent b79a22d commit f7a7cd5
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions cmd/slsa-provenance/cli/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,48 @@ func TestErrors(t *testing.T) {
testCases := []struct {
name string
errorMessage string
validArgument string
validArgument []string
}{
{
name: "test artifact path argument error",
errorMessage: "no value found for required flag: -artifact_path",
validArgument: "-artifact_path artifact/path",
name: "test artifact path argument error",
errorMessage: "no value found for required flag: -artifact_path",
// TODO: Refactor from validArguments to "Arguments" - Allows testing for bad/happy flow
validArgument: []string{"-artifact_path", "artifact/path"},
},
{
name: "test output path argument error",
errorMessage: "no value found for required flag: -output_path",
validArgument: "-output_path output/path",
validArgument: []string{"-output_path", "output/path"},
},
{
name: "test github context argument error",
errorMessage: "no value found for required flag: -github_context",
validArgument: "-github_context gh-context",
validArgument: []string{"-github_context", "gh-context"},
},
{
name: "test runner context argument error",
errorMessage: "no value found for required flag: -runner_context",
validArgument: "-runner_context runner-context",
validArgument: []string{"-runner_context", "runner-context"},
},
// TODO: Add test case that provides no arguments (use make slice function)
}

supplementedArguments := []string{}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
cli := cli.Generate()
err := cli.Exec(context.Background(), supplementedArguments)
supplementedArguments = append(supplementedArguments, tt.validArgument)
cli := cli.Generate()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := cli.Parse(tc.validArgument)
if err != nil {
t.Error("Failed parsing arguments")
}
err = cli.Run(context.Background())

// TODO: Happy flow: If err message is empty string, go test happy flow

if err == nil {
t.Error("Expected an error but did not generate one")
} else {
if err.Error() != tt.errorMessage {
t.Errorf("Expected error to match: %s", tt.errorMessage)
if err.Error() != tc.errorMessage {
t.Errorf("Expected error to match: %s, got: %v", tc.errorMessage, err)
}
}
})
Expand Down

0 comments on commit f7a7cd5

Please sign in to comment.