Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 6, 2024
1 parent 4af0e48 commit 8552300
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions internal/pkg/conflictless/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,60 +266,61 @@ func TestCLIHelp(t *testing.T) {
}
}

func TestCLIGenerateWithInvalidFlags(t *testing.T) {
func TestCLIWithInvalidDirFlag(t *testing.T) {
t.Parallel()

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

return
}

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

stderrFile := createTempFile(t, os.TempDir(), "test-cli-generate-with-invalid-flags-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=^TestCLIGenerateWithInvalidFlags$",
for _, command := range []string{
"check",
"generate",
"--dir",
"rhymenocerous",
"--changelog",
"HIPPOPOTAMUS.md",
"--bump",
"steve",
)

cmd.Stdout = stdoutFile
cmd.Stderr = stderrFile

cmd.Env = append(os.Environ(), "TEST_CLI_GENERATE_INVALID_FLAGS=1")
err := cmd.Run()
"preview",
} {
t.Run("invalid_dir_flag_with_command_"+command, func(t *testing.T) {
t.Parallel()

assert.Error(t, err)
stdoutFile := createTempFile(t, os.TempDir(), "test-cli-generate-with-invalid-flags-stdout")
defer os.Remove(stdoutFile.Name())

assert.IsType(t, new(exec.ExitError), err)
stderrFile := createTempFile(t, os.TempDir(), "test-cli-generate-with-invalid-flags-stderr")
defer os.Remove(stderrFile.Name())

exitErr := new(*exec.ExitError)
errors.As(err, exitErr)
//nolint:gosec // this is a test package so G204 doesn't really matter here.
cmd := exec.Command(
os.Args[0],
"-test.run=^TestCLIWithInvalidDirFlag$",
command,
"--dir",
"rhymenocerous",
)

expectedCode := 2
exitCode := (*exitErr).ExitCode()
cmd.Stdout = stdoutFile
cmd.Stderr = stderrFile

assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode)
cmd.Env = append(os.Environ(), "TEST_CLI_INVALID_DIR_FLAG=1")
err := cmd.Run()

stdoutData, err := os.ReadFile(stdoutFile.Name())
assert.NoError(t, err)
assert.Error(t, err)
assert.IsType(t, new(exec.ExitError), err)

stderrData, err := os.ReadFile(stderrFile.Name())
assert.NoError(t, err)
exitErr := new(*exec.ExitError)
errors.As(err, exitErr)
exitCode := (*exitErr).ExitCode()
expectedCode := 2
assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode)

assert.Empty(t, string(stdoutData))
assert.NotEmpty(t, string(stderrData))
stdoutData, err := os.ReadFile(stdoutFile.Name())
assert.NoError(t, err)
stderrData, err := os.ReadFile(stderrFile.Name())
assert.NoError(t, err)
assert.Empty(t, string(stdoutData))
assert.NotEmpty(t, string(stderrData))
})
}
}

func TestCLICreateWithInvalidFlags(t *testing.T) {
Expand Down

0 comments on commit 8552300

Please sign in to comment.