-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: * common func for test in package internal/test * main file with exec * test for completion, generate, pinger cmd Change: * cobra cmd move to pkg internal/cli * all cmd work with internal/cli * add new make command * change in github actions command run test to make test
- Loading branch information
Showing
59 changed files
with
19,558 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,4 @@ jobs: | |
uses: actions/checkout@v2 | ||
|
||
- name: Test | ||
run: go test ./... | ||
run: make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
bin/ | ||
pinger | ||
pinger | ||
|
||
!cmd/** | ||
!configs/** | ||
!docs/** | ||
!internal/** | ||
!vendor/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package completion_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Nayls/pinger/cmd/completion" | ||
"github.com/Nayls/pinger/internal/cli" | ||
"github.com/Nayls/pinger/internal/test" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func buildTestCmd() *cobra.Command { | ||
cmd := cli.GetRootCmd() | ||
|
||
// Add completion command | ||
cmd.AddCommand(completion.GetCompletionCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func Test_SingleCommandWithoutSubcommand(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.Contains(t, out, `Error: accepts 1 arg(s), received 0`) | ||
assert.Contains(t, out, `completion [bash|zsh|fish|powershell]`) | ||
} | ||
|
||
func Test_SingleCommandWithLongFlagHelp(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "--help") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotContains(t, out, `Error:`) | ||
|
||
assert.Contains(t, out, `Usage:`) | ||
assert.NotContains(t, out, `Available Commands:`) | ||
assert.Contains(t, out, `Flags:`) | ||
|
||
assert.Contains(t, out, `To load completions:`) | ||
assert.Contains(t, out, `Bash:`) | ||
assert.Contains(t, out, `Zsh:`) | ||
assert.Contains(t, out, `Fish:`) | ||
assert.Contains(t, out, `PowerShell:`) | ||
} | ||
|
||
func Test_SingleCommandWithShortFlagHelp(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "-h") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotContains(t, out, `Error:`) | ||
|
||
assert.Contains(t, out, `Usage:`) | ||
assert.NotContains(t, out, `Available Commands:`) | ||
assert.Contains(t, out, `Flags:`) | ||
|
||
assert.Contains(t, out, `To load completions:`) | ||
assert.Contains(t, out, `Bash:`) | ||
assert.Contains(t, out, `Zsh:`) | ||
assert.Contains(t, out, `Fish:`) | ||
assert.Contains(t, out, `PowerShell:`) | ||
} | ||
|
||
func Test_NegativeCallSingleCommandWithSubcommand(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotContains(t, out, `Error:`) | ||
|
||
assert.Contains(t, out, `Usage:`) | ||
assert.NotContains(t, out, `Available Commands:`) | ||
assert.Contains(t, out, `Flags:`) | ||
|
||
assert.Contains(t, out, `To load completions:`) | ||
assert.Contains(t, out, `Bash:`) | ||
assert.Contains(t, out, `Zsh:`) | ||
assert.Contains(t, out, `Fish:`) | ||
assert.Contains(t, out, `PowerShell:`) | ||
} | ||
|
||
func Test_NegativeCallSingleCommandWithoutSubcommandWithFlag(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "--fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotEmpty(t, out) | ||
} | ||
|
||
func Test_CallCommandWithSubcommandBash(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotEmpty(t, out) | ||
} | ||
|
||
func Test_CallCommandWithSubcommandZsh(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotEmpty(t, out) | ||
} | ||
|
||
func Test_CallCommandWithSubcommandFish(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotEmpty(t, out) | ||
} | ||
|
||
func Test_CallCommandWithSubcommandPowershell(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "completion", "fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotEmpty(t, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package generate_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/Nayls/pinger/cmd/generate" | ||
"github.com/Nayls/pinger/internal/cli" | ||
"github.com/Nayls/pinger/internal/test" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func buildTestCmd() *cobra.Command { | ||
cmd := cli.GetRootCmd() | ||
|
||
// Add generate command | ||
cmd.AddCommand(generate.GetGenerateCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func Test_CallSingleCommandWithoutSubcommand(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "generate") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotContains(t, out, `Error:`) | ||
|
||
assert.Contains(t, out, `Command for generate docs and etc`) | ||
|
||
assert.Contains(t, out, `Usage:`) | ||
assert.Contains(t, out, `Available Commands:`) | ||
assert.Contains(t, out, `Flags:`) | ||
} | ||
|
||
func Test_NegativeCallSingleCommandWithSubcommand(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "generate", "fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotContains(t, out, `Error:`) | ||
|
||
assert.Contains(t, out, `Command for generate docs and etc`) | ||
|
||
assert.Contains(t, out, `Usage:`) | ||
assert.Contains(t, out, `Available Commands:`) | ||
assert.Contains(t, out, `Flags:`) | ||
} | ||
|
||
func Test_NegativeCallSingleCommandWithoutSubcommandWithFlag(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "generate", "--fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.Contains(t, out, `Error: unknown flag: --fail`) | ||
|
||
assert.Contains(t, out, `Usage:`) | ||
assert.Contains(t, out, `Available Commands:`) | ||
assert.Contains(t, out, `Flags:`) | ||
|
||
} | ||
|
||
func Test_CallCommandWithSubcommandCli(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "generate", "cli") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.NotContains(t, out, `Error:`) | ||
assert.Empty(t, out) | ||
assert.DirExists(t, "./docs/cli") | ||
assert.FileExists(t, "./docs/cli/pinger.md") | ||
assert.FileExists(t, "./docs/cli/pinger_generate.md") | ||
assert.FileExists(t, "./docs/cli/pinger_generate_cli.md") | ||
|
||
os.RemoveAll("./docs") | ||
} | ||
|
||
func Test_NegativeCallCommandWithSubcommandCliWithFlag(t *testing.T) { | ||
out, err := test.ExecuteCommand(buildTestCmd(), "generate", "cli", "--fail") | ||
if err != nil { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.Contains(t, out, `Error: unknown flag: --fail`) | ||
|
||
assert.Contains(t, out, `Usage:`) | ||
assert.NotContains(t, out, `Available Commands:`) | ||
assert.Contains(t, out, `Flags:`) | ||
} |
Oops, something went wrong.