Skip to content

Commit

Permalink
test(qa): add a duplicated command test (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBrosse authored Apr 3, 2020
1 parent a906040 commit fed004b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
20 changes: 14 additions & 6 deletions cmd/scw-qa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/internal/namespaces"
"github.com/scaleway/scaleway-cli/internal/qa"
"github.com/scaleway/scaleway-cli/internal/tabwriter"
"github.com/scaleway/scaleway-cli/internal/terminal"
)

func main() {
commands := namespaces.GetCommands()
errors := qa.LintCommands(commands)

errorCounts := map[string]int{}
fmt.Println(terminal.Style("Errors:", color.Bold))
for _, err := range errors {
errorCounts[fmt.Sprintf("%T", err)]++
fmt.Printf("%v\n", err)
}

fmt.Printf("Errors:\n")
errorCounts := map[string]int{}
for _, err := range errors {
fmt.Printf("%v\n", err)
errorCounts[fmt.Sprintf("%T", err)]++
}
fmt.Printf("\nSummary:\n")

fmt.Println(terminal.Style("\nSummary:", color.Bold))
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
for key, count := range errorCounts {
fmt.Printf("%s: %d\n", key, count)
_, _ = fmt.Fprintf(w, "%s\t%d\n", strings.TrimPrefix(key, "*qa."), count)
}
_ = w.Flush()
}
30 changes: 29 additions & 1 deletion internal/qa/qa.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func LintCommands(commands *core.Commands) []interface{} {
errors = append(errors, testPositionalArgMustBeRequiredError(commands)...)
errors = append(errors, testExampleCanHaveOnlyOneTypeOfExampleError(commands)...)
errors = append(errors, testDifferentLocalizationForNamespaceError(commands)...)
errors = append(errors, testDuplicatedCommandError(commands)...)
return errors
}

Expand Down Expand Up @@ -140,7 +141,7 @@ type DifferentLocalizationForNamespaceError struct {
}

func (err DifferentLocalizationForNamespaceError) Error() string {
return fmt.Sprintf("different localization for '%v', '%v': %v, %v",
return fmt.Sprintf("different localization for commands '%v', '%v': %v, %v",
err.Command1.GetCommandLine(), err.Command2.GetCommandLine(), err.ArgNames1, err.ArgNames2)
}

Expand Down Expand Up @@ -208,3 +209,30 @@ func testDifferentLocalizationForNamespaceError(commands *core.Commands) []inter
}
return errors
}

type DuplicatedCommandError struct {
Command *core.Command
}

func (err DuplicatedCommandError) Error() string {
return fmt.Sprintf("duplicated command '%s'", err.Command.GetCommandLine())
}

// testDuplicatedCommandError testes that there is no duplicate command.
func testDuplicatedCommandError(commands *core.Commands) []interface{} {
errors := []interface{}(nil)
uniqueness := make(map[string]bool)

for _, command := range commands.GetAll() {
key := command.GetCommandLine()

if uniqueness[key] {
errors = append(errors, &DuplicatedCommandError{Command: command})
continue
}

uniqueness[key] = true
}

return errors
}

0 comments on commit fed004b

Please sign in to comment.