Skip to content

Commit

Permalink
Add test for invalid command
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 19, 2023
1 parent fc34676 commit 2ce5078
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/pkg/conflictless/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,48 @@ func TestCLIWithoutArguments(t *testing.T) {
assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode)
}

func TestCliWithInvalidCommand(t *testing.T) {
t.Parallel()

if os.Getenv("TEST_CLI_WITH_INVALID_COMMAND") == "1" {
conflictless.CLI()

return
}

stdoutFile := createTempFile(t, os.TempDir(), "test-cli-with-invalid-command-stdout")
defer os.Remove(stdoutFile.Name())

stderrFile := createTempFile(t, os.TempDir(), "test-cli-with-invalid-command-stderr")
defer os.Remove(stderrFile.Name())

//nolint:gosec // this is a test package so G204 doesn't really matter here.
cmd := exec.Command(os.Args[0], "-test.run=^TestCliWithInvalidCommand$", "unknown")

cmd.Env = append(os.Environ(), "TEST_CLI_WITH_INVALID_COMMAND=1")
cmd.Stdout = stdoutFile
cmd.Stderr = stderrFile
err := cmd.Run()

assert.IsType(t, new(exec.ExitError), err)

exitErr := new(*exec.ExitError)
errors.As(err, exitErr)

stdoutData, err := os.ReadFile(stdoutFile.Name())
assert.NoError(t, err)
assert.Empty(t, stdoutData)

stderrData, err := os.ReadFile(stderrFile.Name())
assert.NoError(t, err)
assert.Contains(t, string(stderrData), "Error: invalid command: 'unknown'")

expectedCode := 2
exitCode := (*exitErr).ExitCode()

assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode)
}

func TestCLIHelp(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 2ce5078

Please sign in to comment.