Charmil Validator
Charmil 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#
LengthRuleLength Rule can control the lengths of strings in different attributes of cobra.Command structure.
#
MustExistRuleMust Exist Rule ensures that the selected attributes from cobra.Command
strcuture must be present in the command.
#
UseMatchesRuleUse Matches Rule compares the Use attribute of cobra.Command
with the user provided regexp.
#
ExampleMatchesRuleExample Matches Rule ensures that the command is properly documented with the proper examples in it.
#
PunctuationRulePunctuation Rule checks for the punctuation errors in the command according to industry standards.
#
How to useIt is recommended to use the validator while writing unit tests for cobra commands.
- Create a configuration of type
rules.ValidatorConfig
. You can provide your own ValidatorConfig or use the default one by leaving it empty
import "github.com/aerogear/charmil/validator"
var ruleCfg rules.ValidatorConfig
or overriding default config
import "github.com/aerogear/charmil/validator"
ruleCfg := rules.ValidatorConfig{ ValidatorRules: rules.ValidatorRules{ Length: rules.Length{Limits: map[string]rules.Limit{"Use": {Min: 1}}}, MustExist: rules.MustExist{Fields: map[string]bool{"Args": true}}, UseMatches: rules.UseMatches{Regexp: `^[^-_+]+$`}, },}
- Generate the validation errors by using
ExecuteRules
function over the ruleCfg
validationErr := rules.ExecuteRules(cmd, &ruleCfg)
ExecuteRules
function will return a slice of ValidationError
object, which can be efficiently used for testing purposes.
if len(validationErr) != 0 { t.Errorf("validationErr was not empty, got length: %d; want %d", len(validationErr), 0)}for _, errs := range validationErr { if errs.Err != nil { t.Errorf("%s: cmd %s: %s", errs.Rule, errs.Cmd.CommandPath(), errs.Name) }}
#
Disable a RuleAll the rules provided by charmil are enabled by default. If you want to turn off particular rule or rules, there is an Disable
option in RuleOptions
in each rule.
ruleCfg := rules.ValidatorConfig{ ValidatorRules: rules.ValidatorRules{ Punctuation: Punctuation{ RuleOptions: validator.RuleOptions{ Disable: true, }, }, UseMatches: UseMatches{ RuleOptions: validator.RuleOptions{ Disable: true, }, },
},}
#
Ignore CommandsSometimes during development, you want to pass the tests for certain commands, but at the same time use Validator for tests. Validation can be skipped/ignored for the commands, mentioned in the validator configuration. To ignore the commands you need to specify the path of the command in validator configuration.
Assume your CLI is having a command structure like this:
mycli actions create update delete read
mycli actions create
#
Skip single command Use SkipCommands
option in ValidatorOptions
to skip validation for a command
ValidatorOptions: rules.ValidatorOptions{ SkipCommands: map[string]bool{"mycli actions create": true},},
#
Skip the command including all childrenUse SkipCommands
option in ValidatorOptions
using asterisk sign to skip validation for all subcommands
ValidatorOptions: rules.ValidatorOptions{ SkipCommands: map[string]bool{"mycli actions*": true},},
#
Skip the command for specific ruleUse SkipCommands
option in RuleOptions
to skip validation for specific rule
Length: rules.Length{ RuleOptions: validator.RuleOptions{ SkipCommands: map[string]bool{"mycli actions create": true}, }, Limits: map[string]rules.Limit{ "Use": {Min: 1}, },},