Skip to content

Commit baff3b8

Browse files
authored
feat(qa): add json examples validation (#3186)
1 parent f0a4b9e commit baff3b8

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

internal/qa/example.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package qa
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/scaleway/scaleway-cli/v2/internal/core"
8+
)
9+
10+
type CommandInvalidJSONExampleError struct {
11+
Command *core.Command
12+
}
13+
14+
func (err CommandInvalidJSONExampleError) Error() string {
15+
return fmt.Sprintf("command has invalid json examples '%s'",
16+
err.Command.GetCommandLine("scw"),
17+
)
18+
}
19+
20+
// testArgSpecInvalidError tests that all argspecs have a corresponding in their command's argstype.
21+
func testCommandInvalidJSONExampleError(commands *core.Commands) []error {
22+
errors := []error(nil)
23+
24+
for _, command := range commands.GetAll() {
25+
for _, example := range command.Examples {
26+
if example.ArgsJSON != "" {
27+
out := map[string]any{}
28+
err := json.Unmarshal([]byte(example.ArgsJSON), &out)
29+
if err != nil {
30+
errors = append(errors, &CommandInvalidJSONExampleError{
31+
Command: command,
32+
})
33+
}
34+
}
35+
}
36+
}
37+
38+
return errors
39+
}

internal/qa/qa.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func LintCommands(commands *core.Commands) []error {
2121
errors = append(errors, testAtLeastOneExampleIsPresentError(commands)...)
2222
errors = append(errors, testArgSpecInvalidError(commands)...)
2323
errors = append(errors, testArgSpecMissingError(commands)...)
24+
errors = append(errors, testCommandInvalidJSONExampleError(commands)...)
2425

2526
errors = filterIgnore(errors)
2627

0 commit comments

Comments
 (0)