From 86a8ca4b04a5497e9bf2dde222f9454c21535e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bourgois?= Date: Wed, 11 Mar 2020 11:00:07 +0100 Subject: [PATCH] chore: add more QA tests (#763) --- internal/qa/qa.go | 69 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/internal/qa/qa.go b/internal/qa/qa.go index e90f5b8e80..1851f1f626 100644 --- a/internal/qa/qa.go +++ b/internal/qa/qa.go @@ -1,6 +1,7 @@ package qa import ( + "strconv" "strings" "github.com/scaleway/scaleway-cli/internal/core" @@ -10,6 +11,8 @@ func LintCommands(commands *core.Commands) []interface{} { errors := []interface{}(nil) errors = append(errors, testShortEndWithDotError(commands)...) errors = append(errors, testShortIsNotPresentError(commands)...) + errors = append(errors, testArgMustUseDashError(commands)...) + errors = append(errors, testExampleCanHaveOnlyOneTypeOfExampleError(commands)...) return errors } @@ -24,12 +27,11 @@ func (err ShortMustNotEndWithDotError) Error() string { func testShortEndWithDotError(commands *core.Commands) []interface{} { errors := []interface{}(nil) for _, command := range commands.GetAll() { - if !strings.HasSuffix(command.Short, ".") { - continue + if strings.HasSuffix(command.Short, ".") { + errors = append(errors, &ShortMustNotEndWithDotError{ + Command: command, + }) } - errors = append(errors, &ShortMustNotEndWithDotError{ - Command: command, - }) } return errors } @@ -45,12 +47,59 @@ func (err ShortMustBePresentError) Error() string { func testShortIsNotPresentError(commands *core.Commands) []interface{} { errors := []interface{}(nil) for _, command := range commands.GetAll() { - if command.Short != "" { - continue + if command.Short == "" { + errors = append(errors, &ShortMustBePresentError{ + Command: command, + }) + } + } + return errors +} + +type ArgMustUseDashError struct { + Command *core.Command + Argspec *core.ArgSpec +} + +func (err ArgMustUseDashError) Error() string { + return "arg must use dash for command '" + err.Command.GetCommandLine() + "', arg '" + err.Argspec.Name + "'" +} + +func testArgMustUseDashError(commands *core.Commands) []interface{} { + errors := []interface{}(nil) + for _, command := range commands.GetAll() { + for _, argspec := range command.ArgSpecs { + if strings.Contains(argspec.Name, "_") { + errors = append(errors, &ArgMustUseDashError{ + Command: command, + Argspec: argspec, + }) + } + } + } + return errors +} + +type ExampleCanHaveOnlyOneTypeOfExampleError struct { + Command *core.Command + ExampleIndex int +} + +func (err ExampleCanHaveOnlyOneTypeOfExampleError) Error() string { + return "arg must use dash for command '" + err.Command.GetCommandLine() + "', example #" + strconv.Itoa(err.ExampleIndex) +} + +func testExampleCanHaveOnlyOneTypeOfExampleError(commands *core.Commands) []interface{} { + errors := []interface{}(nil) + for _, command := range commands.GetAll() { + for i, example := range command.Examples { + if example.Request != "" && example.Raw != "" { + errors = append(errors, &ExampleCanHaveOnlyOneTypeOfExampleError{ + Command: command, + ExampleIndex: i, + }) + } } - errors = append(errors, &ShortMustBePresentError{ - Command: command, - }) } return errors }