Skip to content

Commit

Permalink
Add tests for help command
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 18, 2023
1 parent cb34478 commit fc34676
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/conflictless/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func argsWithoutTestFlags() []string {

for _, arg := range os.Args {
if strings.HasPrefix(arg, "-test.") {
break
continue
}

args = append(args, arg)
Expand Down
64 changes: 64 additions & 0 deletions internal/pkg/conflictless/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conflictless_test

import (
"errors"
"net/url"
"os"
"os/exec"
"testing"
Expand Down Expand Up @@ -51,3 +52,66 @@ func TestCLIWithoutArguments(t *testing.T) {

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

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

if os.Getenv("TEST_CLI_HELP") != "" {
conflictless.CLI()

return
}

for _, testCase := range []struct {
description string
args []string
isError bool
}{
{"help", []string{"help"}, false},
{"help check", []string{"help", "check"}, false},
{"help generate", []string{"help", "generate"}, false},
{"help unknown", []string{"help", "unknown"}, true},
} {
// Reinitialise testCase for parallel testing.
testCase := testCase

t.Run(testCase.description, func(t *testing.T) {
t.Parallel()

stdoutFile := createTempFile(t, os.TempDir(), "test-cli-"+url.QueryEscape(testCase.description)+"-stdout")
defer os.Remove(stdoutFile.Name())

stderrFile := createTempFile(t, os.TempDir(), "test-cli-"+url.QueryEscape(testCase.description)+"-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], append([]string{"-test.run=^TestCLIHelp$"}, testCase.args...)...)
cmd.Env = append(os.Environ(), "TEST_CLI_HELP=1")
cmd.Stdout = stdoutFile
cmd.Stderr = stderrFile

err := cmd.Run()

if testCase.isError {
assert.Error(t, err)
assert.IsType(t, new(exec.ExitError), err)
} else {
assert.NoError(t, err)
}

stderrData, err := os.ReadFile(stderrFile.Name())
assert.NoError(t, err)

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

if testCase.isError {
assert.Empty(t, string(stdoutData))
assert.Contains(t, string(stderrData), "Error:")
} else {
assert.Contains(t, string(stdoutData), "Usage: conflictless")
assert.Empty(t, string(stderrData))
}
})
}
}
1 change: 0 additions & 1 deletion internal/pkg/conflictless/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func help() {
usage()
default:
PrintErrorAndExit(fmt.Sprintf("unknown help topic '%s'", topic), func() {})
os.Exit(exitCodeMisuseError)
}

os.Exit(exitCodeSuccess)
Expand Down

0 comments on commit fc34676

Please sign in to comment.