Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: merge default and user provided rulesconfig
- Loading branch information
Showing
10 changed files
with
162 additions
and
67 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
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,9 +1,37 @@ | ||
## Validator | ||
Validator can be used for testing or controlling many aspects of cobra commands. It provides many rules out of the box for validating the commands. | ||
Validator can be used for testing and controlling many aspects of cobra commands. It provides many rules out of the box for validating the commands. | ||
|
||
## Rules provided by validator | ||
- [LengthRule](validator_length_rule.md) | ||
- [MustExistRule](validator_must_exist_rule.md) | ||
- UseMatches | ||
> We are working on the validator library to provide more rules | ||
## How to use | ||
It is recommended to use the validator while writing unit tests for cobra commands. | ||
|
||
1. Create a validator of type `rules.RuleConfig`. You can provide your own RulesConfig or use the default one by leaving it empty | ||
```go | ||
var vali rules.RuleConfig | ||
``` | ||
or overriding default config | ||
```go | ||
var vali rules.RuleConfig = rules.RuleConfig{ | ||
Verbose: true, | ||
MustExist: rules.MustExist{ | ||
Fields: []string{"Args"}, | ||
}, | ||
} | ||
``` | ||
2. Generate the validation errors by using `ExecuteRules` function over the config | ||
```go | ||
validationErr := vali.ExecuteRules(cmd) | ||
``` | ||
`ExecuteRules` function will return a slice of `ValidationError` object, which can be efficiently used for testing purposes. | ||
```go | ||
for _, errs := range validationErr { | ||
if errs.Err != nil { | ||
t.Errorf("%s: cmd %s: %s", errs.Rule, errs.Cmd.CommandPath(), errs.Name) | ||
} | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
package example | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommand() *cobra.Command { | ||
|
||
cmd := &cobra.Command{ | ||
Use: "new", | ||
Short: "This is the short", | ||
Long: "This is long", | ||
Example: "My Example", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
}, | ||
} | ||
|
||
cmd1 := &cobra.Command{ | ||
Use: "subcmd", | ||
Short: "", | ||
Example: "examples", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
cmd2 := &cobra.Command{ | ||
Use: "subcmd2", | ||
Short: "", | ||
Example: "examples", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
cmd1.AddCommand(cmd2) | ||
cmd.AddCommand(cmd1) | ||
|
||
return cmd | ||
} |
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,39 @@ | ||
package example | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/aerogear/charmil/validator/rules" | ||
) | ||
|
||
func Test_ExecuteCommand(t *testing.T) { | ||
cmd := NewCommand() | ||
|
||
// Testing cobra commands with default recommended config | ||
// default config can also be overrided by | ||
/* | ||
var vali rules.RuleConfig = rules.RuleConfig{ | ||
Verbose: true, | ||
MustExist: rules.MustExist{ | ||
Fields: []string{"Args"}, | ||
}, | ||
} | ||
*/ | ||
var vali rules.RuleConfig | ||
validationErr := vali.ExecuteRules(cmd) | ||
for _, errs := range validationErr { | ||
if errs.Err != nil { | ||
t.Errorf("%s: cmd %s: %s", errs.Rule, errs.Cmd.CommandPath(), errs.Name) | ||
} | ||
} | ||
|
||
b := bytes.NewBufferString("") | ||
cmd.SetOut(b) | ||
cmd.Execute() | ||
_, err := ioutil.ReadAll(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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
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