Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1695) Change tests to use assert and require #1853

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
644 changes: 206 additions & 438 deletions command_test.go

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package cli

import (
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCompletionDisable(t *testing.T) {
cmd := &Command{}

err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName})
if err == nil {
t.Error("Expected error for no help topic for completion")
}
assert.Error(t, err, "Expected error for no help topic for completion")
}

func TestCompletionEnable(t *testing.T) {
Expand All @@ -23,9 +21,7 @@ func TestCompletionEnable(t *testing.T) {
}

err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName})
if err == nil || !strings.Contains(err.Error(), "no shell provided") {
t.Errorf("expected no shell provided error instead got [%v]", err)
}
assert.ErrorContains(t, err, "no shell provided")
}

func TestCompletionEnableDiffCommandName(t *testing.T) {
Expand All @@ -35,9 +31,7 @@ func TestCompletionEnableDiffCommandName(t *testing.T) {
}

err := cmd.Run(buildTestContext(t), []string{"foo", "junky"})
if err == nil || !strings.Contains(err.Error(), "no shell provided") {
t.Errorf("expected no shell provided error instead got [%v]", err)
}
assert.ErrorContains(t, err, "no shell provided")
}

func TestCompletionShell(t *testing.T) {
Expand Down Expand Up @@ -94,7 +88,5 @@ func TestCompletionInvalidShell(t *testing.T) {
}

err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName, "junky-sheell"})
if err == nil {
t.Error("Expected error for invalid shell")
}
assert.ErrorContains(t, err, "unknown shell junky-sheell")
}
36 changes: 19 additions & 17 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHandleExitCoder_nil(t *testing.T) {
Expand All @@ -22,8 +24,8 @@ func TestHandleExitCoder_nil(t *testing.T) {

HandleExitCoder(nil)

expect(t, exitCode, 0)
expect(t, called, false)
assert.Equal(t, 0, exitCode)
assert.False(t, called)
}

func TestHandleExitCoder_ExitCoder(t *testing.T) {
Expand All @@ -41,8 +43,8 @@ func TestHandleExitCoder_ExitCoder(t *testing.T) {

HandleExitCoder(Exit("galactic perimeter breach", 9))

expect(t, exitCode, 9)
expect(t, called, true)
assert.Equal(t, 9, exitCode)
assert.True(t, called)
}

func TestHandleExitCoder_ErrorExitCoder(t *testing.T) {
Expand All @@ -60,8 +62,8 @@ func TestHandleExitCoder_ErrorExitCoder(t *testing.T) {

HandleExitCoder(Exit(errors.New("galactic perimeter breach"), 9))

expect(t, exitCode, 9)
expect(t, called, true)
assert.Equal(t, 9, exitCode)
assert.True(t, called)
}

func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) {
Expand All @@ -82,8 +84,8 @@ func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) {
err := newMultiError(errors.New("wowsa"), errors.New("egad"), exitErr, exitErr2)
HandleExitCoder(err)

expect(t, exitCode, 11)
expect(t, called, true)
assert.Equal(t, 11, exitCode)
assert.True(t, called)
}

func TestHandleExitCoder_MultiErrorWithoutExitCoder(t *testing.T) {
Expand All @@ -102,8 +104,8 @@ func TestHandleExitCoder_MultiErrorWithoutExitCoder(t *testing.T) {
err := newMultiError(errors.New("wowsa"), errors.New("egad"))
HandleExitCoder(err)

expect(t, exitCode, 1)
expect(t, called, true)
assert.Equal(t, 1, exitCode)
assert.True(t, called)
}

// make a stub to not import pkg/errors
Expand Down Expand Up @@ -137,8 +139,8 @@ func TestHandleExitCoder_ErrorWithFormat(t *testing.T) {
err := Exit(NewErrorWithFormat("I am formatted"), 1)
HandleExitCoder(err)

expect(t, called, true)
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: I am formatted\n")
assert.True(t, called)
assert.Equal(t, ErrWriter.(*bytes.Buffer).String(), "This the format: I am formatted\n")
}

func TestHandleExitCoder_MultiErrorWithFormat(t *testing.T) {
Expand All @@ -156,8 +158,8 @@ func TestHandleExitCoder_MultiErrorWithFormat(t *testing.T) {
err := newMultiError(NewErrorWithFormat("err1"), NewErrorWithFormat("err2"))
HandleExitCoder(err)

expect(t, called, true)
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: err1\nThis the format: err2\n")
assert.True(t, called)
assert.Equal(t, ErrWriter.(*bytes.Buffer).String(), "This the format: err1\nThis the format: err2\n")
}

func TestMultiErrorErrorsCopy(t *testing.T) {
Expand All @@ -167,17 +169,17 @@ func TestMultiErrorErrorsCopy(t *testing.T) {
errors.New("baz"),
}
me := newMultiError(errList...)
expect(t, errList, me.Errors())
assert.Equal(t, errList, me.Errors())
}

func TestErrRequiredFlags_Error(t *testing.T) {
missingFlags := []string{"flag1", "flag2"}
err := &errRequiredFlags{missingFlags: missingFlags}
expectedMsg := "Required flags \"flag1, flag2\" not set"
expect(t, expectedMsg, err.Error())
assert.Equal(t, expectedMsg, err.Error())

missingFlags = []string{"flag1"}
err = &errRequiredFlags{missingFlags: missingFlags}
expectedMsg = "Required flag \"flag1\" not set"
expect(t, expectedMsg, err.Error())
assert.Equal(t, expectedMsg, err.Error())
}
28 changes: 5 additions & 23 deletions flag_bool_with_inverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,31 +333,13 @@ func TestBoolWithInverseNames(t *testing.T) {
}
names := flag.Names()

if len(names) != 2 {
t.Errorf("expected 2 names, got %d", len(names))
return
}

if names[0] != "env" {
t.Errorf("expected first name to be `env`, got `%s`", names[0])
return
}

if names[1] != "no-env" {
t.Errorf("expected first name to be `no-env`, got `%s`", names[1])
return
}
require.Len(t, names, 2)
require.Equal(t, "env", names[0], "expected first name to be `env`")
require.Equal(t, "no-env", names[1], "expected first name to be `no-env`")

flagString := flag.String()
if strings.Contains(flagString, "--env") == false {
t.Errorf("expected `%s` to contain `--env`", flagString)
return
}

if strings.Contains(flagString, "--no-env") == false {
t.Errorf("expected `%s` to contain `--no-env`", flagString)
return
}
require.Contains(t, flagString, "--env")
require.Contains(t, flagString, "--no-env")
}

func TestBoolWithInverseDestination(t *testing.T) {
Expand Down
14 changes: 5 additions & 9 deletions flag_mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFlagMutuallyExclusiveFlags(t *testing.T) {
Expand Down Expand Up @@ -30,14 +32,10 @@ func TestFlagMutuallyExclusiveFlags(t *testing.T) {
}

err := cmd.Run(buildTestContext(t), []string{"foo"})
if err != nil {
t.Error(err)
}
assert.NoError(t, err)

err = cmd.Run(buildTestContext(t), []string{"foo", "--i", "10"})
if err != nil {
t.Error(err)
}
assert.NoError(t, err)

err = cmd.Run(buildTestContext(t), []string{"foo", "--i", "11", "--ai", "12"})
if err == nil {
Expand All @@ -60,9 +58,7 @@ func TestFlagMutuallyExclusiveFlags(t *testing.T) {
}

err = cmd.Run(buildTestContext(t), []string{"foo", "--i", "10"})
if err != nil {
t.Error(err)
}
assert.NoError(t, err)

err = cmd.Run(buildTestContext(t), []string{"foo", "--i", "11", "--ai", "12"})
if err == nil {
Expand Down
Loading
Loading