From cb34478c4f713e10a80d1710e1b30dc71f82b2d0 Mon Sep 17 00:00:00 2001 From: ypjama <3822534+ypjama@users.noreply.github.com> Date: Thu, 19 Oct 2023 00:45:04 +0300 Subject: [PATCH] Add tests for cli --- internal/pkg/conflictless/cli.go | 23 ++++++++++-- internal/pkg/conflictless/cli_test.go | 53 +++++++++++++++++++++++++++ internal/pkg/conflictless/help.go | 7 +++- 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 internal/pkg/conflictless/cli_test.go diff --git a/internal/pkg/conflictless/cli.go b/internal/pkg/conflictless/cli.go index 4ab7a9f..57742da 100644 --- a/internal/pkg/conflictless/cli.go +++ b/internal/pkg/conflictless/cli.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "strings" ) const ( @@ -50,11 +51,27 @@ func CLI() { } } +func argsWithoutTestFlags() []string { + args := []string{} + + for _, arg := range os.Args { + if strings.HasPrefix(arg, "-test.") { + break + } + + args = append(args, arg) + } + + return args +} + func parseCLIFlags(cfg *Config) { flag.Usage = usage - if len(os.Args) > argIdxCommand { - cfg.Flags.Command = os.Args[argIdxCommand] + args := argsWithoutTestFlags() + + if len(args) > argIdxCommand { + cfg.Flags.Command = args[argIdxCommand] } var cmd *flag.FlagSet @@ -78,7 +95,7 @@ func parseCLIFlags(cfg *Config) { } if cmd != nil { - err := cmd.Parse(os.Args[2:]) + err := cmd.Parse(args[2:]) if err != nil { panic(err) } diff --git a/internal/pkg/conflictless/cli_test.go b/internal/pkg/conflictless/cli_test.go new file mode 100644 index 0000000..eb4f503 --- /dev/null +++ b/internal/pkg/conflictless/cli_test.go @@ -0,0 +1,53 @@ +package conflictless_test + +import ( + "errors" + "os" + "os/exec" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/ypjama/conflictless-keepachangelog/internal/pkg/conflictless" +) + +func TestCLIWithoutArguments(t *testing.T) { + t.Parallel() + + if os.Getenv("TEST_CLI_WITHOUT_ARGUMENTS") == "1" { + conflictless.CLI() + + return + } + + stdoutFile := createTempFile(t, os.TempDir(), "test-cli-without-args-stdout") + defer os.Remove(stdoutFile.Name()) + + stderrFile := createTempFile(t, os.TempDir(), "test-cli-without-args-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=^TestCLIWithoutArguments$") + + cmd.Env = append(os.Environ(), "TEST_CLI_WITHOUT_ARGUMENTS=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) + + expectedCode := 2 + exitCode := (*exitErr).ExitCode() + + 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), "Usage: conflictless [flags]") + + assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode) +} diff --git a/internal/pkg/conflictless/help.go b/internal/pkg/conflictless/help.go index 477a0d6..ad734b1 100644 --- a/internal/pkg/conflictless/help.go +++ b/internal/pkg/conflictless/help.go @@ -7,8 +7,11 @@ import ( func help() { var topic string - if len(os.Args) > argIdxHelpTopic { - topic = os.Args[argIdxHelpTopic] + + args := argsWithoutTestFlags() + + if len(args) > argIdxHelpTopic { + topic = args[argIdxHelpTopic] } switch topic {