Skip to content

Commit

Permalink
Add tests for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 18, 2023
1 parent fd93f77 commit cb34478
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
23 changes: 20 additions & 3 deletions internal/pkg/conflictless/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"strings"
)

const (
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
53 changes: 53 additions & 0 deletions internal/pkg/conflictless/cli_test.go
Original file line number Diff line number Diff line change
@@ -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 <command> [flags]")

assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode)
}
7 changes: 5 additions & 2 deletions internal/pkg/conflictless/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cb34478

Please sign in to comment.