Skip to content

Commit

Permalink
subcommands_test: Group command tests with t.Run.
Browse files Browse the repository at this point in the history
Simple clean up to help display/group command tests together and cut
down on some of the repetition.
  • Loading branch information
wlynch authored and tekton-robot committed Jan 4, 2022
1 parent caa4a7f commit 02d4f7e
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions cmd/entrypoint/subcommands/subcommands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ func TestProcessSuccessfulSubcommands(t *testing.T) {
t.Fatalf("error writing source file: %v", err)
}

returnValue := Process([]string{CopyCommand, src, dst})
if _, ok := returnValue.(SubcommandSuccessful); !ok {
t.Errorf("unexpected return value from cp command: %v", returnValue)
}

returnValue = Process([]string{DecodeScriptCommand, src})
if _, ok := returnValue.(SubcommandSuccessful); !ok {
t.Errorf("unexpected return value from decode-script command: %v", returnValue)
for _, tc := range []struct {
command string
args []string
}{
{
command: CopyCommand,
args: []string{src, dst},
},
{
command: DecodeScriptCommand,
args: []string{src},
},
} {
t.Run(tc.command, func(t *testing.T) {
returnValue := Process(append([]string{tc.command}, tc.args...))
if _, ok := returnValue.(SubcommandSuccessful); !ok {
t.Errorf("unexpected return value from command: %v", returnValue)
}
})
}

t.Run(StepInitCommand, func(t *testing.T) {
Expand Down Expand Up @@ -67,19 +78,24 @@ func TestProcessIgnoresNonSubcommands(t *testing.T) {
t.Errorf("unexpected error processing 0 args: %v", err)
}

if err := Process([]string{CopyCommand}); err != nil {
t.Errorf("unexpected error processing decode-script command with 0 additional args: %v", err)
}
t.Run(CopyCommand, func(t *testing.T) {
if err := Process([]string{CopyCommand}); err != nil {
t.Errorf("unexpected error processing command with 0 additional args: %v", err)
}

if err := Process([]string{CopyCommand, "foo.txt", "bar.txt", "baz.txt"}); err != nil {
t.Errorf("unexpected error processing cp command with invalid number of args: %v", err)
}
if err := Process([]string{CopyCommand, "foo.txt", "bar.txt", "baz.txt"}); err != nil {
t.Errorf("unexpected error processing command with invalid number of args: %v", err)
}
})

if err := Process([]string{DecodeScriptCommand}); err != nil {
t.Errorf("unexpected error processing decode-script command with 0 additional args: %v", err)
}
t.Run(DecodeScriptCommand, func(t *testing.T) {

if err := Process([]string{DecodeScriptCommand, "foo.txt", "bar.txt"}); err != nil {
t.Errorf("unexpected error processing decode-script command with invalid number of args: %v", err)
}
if err := Process([]string{DecodeScriptCommand}); err != nil {
t.Errorf("unexpected error processing command with 0 additional args: %v", err)
}

if err := Process([]string{DecodeScriptCommand, "foo.txt", "bar.txt"}); err != nil {
t.Errorf("unexpected error processing command with invalid number of args: %v", err)
}
})
}

0 comments on commit 02d4f7e

Please sign in to comment.