Skip to content

Commit

Permalink
feat: common update for tests
Browse files Browse the repository at this point in the history
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
Nayls committed Sep 16, 2021
1 parent 2842b07 commit 568dcb7
Show file tree
Hide file tree
Showing 59 changed files with 19,558 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
uses: actions/checkout@v2

- name: Test
run: go test ./...
run: make test
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
bin/
pinger
pinger

!cmd/**
!configs/**
!docs/**
!internal/**
!vendor/**
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,34 @@ help: #!# Display this help screen
@awk 'BEGIN {FS = ":.*#!#"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-].+:.*?#!#/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^#!#@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

# TASKS ============================================================================================================
all: pre-run build-linux build-windows build-darwin #!# run job pre-run, build-linux, build-windows, build-darwin
all: pre-commit generate-docs build #!# Run jobs: pre-commit, generate-docs, build
all-develop: pre-commit generate-docs build-linux-amd64 #!# Run jobs: pre-commit, generate-docs, build-linux-amd64

pre-run: ## job for deploy git
pre-commit: dependency test generate-docs #!# Run jobs: dependency, test, genereate-docs

dependency: #!# Download all dependency and generate vendor
@echo "> Go fmt"
@go fmt ./...
@echo "> Go mod tidy"
@go mod tidy
@echo "> Go mod download"
@go mod download
@echo "> Go mod vendor"
@go mod vendor

test: #!# Run test
@echo "> Run tests"
@go test ./...

generate-docs: #!# Generate docs
@echo "> Generate cli docs"
@go run main.go generate cli


build: build-linux build-windows build-darwin #!# Run jobs: build-linux, build-windows, build-darwin

# LINUX BUILD
build-linux: build-linux-386 build-linux-amd64 build-linux-arm build-linux-arm64 #!# build linux 386, amd64, arm, arm64
build-linux: build-linux-386 build-linux-amd64 build-linux-arm build-linux-arm64 #!# Build linux 386, amd64, arm, arm64

build-linux-386: ## build binary file linux-386
@echo "> Build linux 386"
Expand Down Expand Up @@ -76,8 +90,9 @@ build-linux-arm64: ## build binary file linux-arm64
-trimpath \
-o bin/linux/pinger-linux-arm64 ./main.go


# WINDOWS BUILD
build-windows: build-windows-386 build-windows-amd64 build-windows-arm #!# build windows 386, amd64, arm
build-windows: build-windows-386 build-windows-amd64 build-windows-arm #!# Build windows 386, amd64, arm

build-windows-386: ## build binary file windows-386
@echo "> Build windows 386"
Expand Down Expand Up @@ -112,8 +127,9 @@ build-windows-arm: ## build binary file windows-arm
-trimpath \
-o bin/windows/pinger-windows-arm ./main.go


# MACOS BUILD
build-darwin: build-darwin-amd64 build-darwin-arm64 #!# build darwin amd64, arm64
build-darwin: build-darwin-amd64 build-darwin-arm64 #!# Build darwin amd64, arm64

build-darwin-amd64: ## build binary file darwin-amd64
@echo "> Build darwin amd64"
Expand Down
2 changes: 1 addition & 1 deletion cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Zsh:
# You will need to start a new shell for this setup to take effect.
fish:
Fish:
$ pinger completion fish | source
Expand Down
132 changes: 132 additions & 0 deletions cmd/completion/completion_test.go
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)
}
28 changes: 13 additions & 15 deletions cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,28 @@ var generateCmd = &cobra.Command{
Use: "generate [command]",
Short: "Command generate",
Long: `Command for generate docs and etc`,
Run: func(cmd *cobra.Command, args []string) {},
// Run: func(cmd *cobra.Command, args []string) {},
}

var generateCliDocCmd = &cobra.Command{
Use: "cli",
Short: "Generate cli documentation",
Long: `Generate cli documentations `,
Run: doSomething,
}

func GetGenerateCmd() *cobra.Command {
return generateCmd
}
Long: `Generate cli documentations`,
Run: func(cmd *cobra.Command, args []string) {
if _, err := os.Stat("./docs/cli"); os.IsNotExist(err) {
if err := os.MkdirAll("./docs/cli", 0755); err != nil {
log.Fatal(err)
}
}

func doSomething(cmd *cobra.Command, args []string) {
if _, err := os.Stat("./docs/cli"); os.IsNotExist(err) {
if err := os.MkdirAll("./docs/cli", 0755); err != nil {
if err := doc.GenMarkdownTree(cli.GetRootCmd(), "./docs/cli"); err != nil {
log.Fatal(err)
}
}
},
}

if err := doc.GenMarkdownTree(cli.GetRootCmd(), "./docs/cli"); err != nil {
log.Fatal(err)
}
func GetGenerateCmd() *cobra.Command {
return generateCmd
}

func init() {
Expand Down
94 changes: 94 additions & 0 deletions cmd/generate/generate_test.go
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:`)
}
Loading

0 comments on commit 568dcb7

Please sign in to comment.