-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add cobra commands validator #767
Conversation
cc: @wtrocki |
@ankithans I think we need to adjust our rules for CLI to pass. Then we can create issue for rhoas to fix those problems. This will require alignment with documentation teams and @craicoverflow as lead, but overall we show value of having validator for commands. Let's create issue with errors for ideal rules we want to have and adjust rules now to have it pass. |
go.mod
Outdated
@@ -7,6 +7,7 @@ require ( | |||
github.com/BurntSushi/toml v0.3.1 | |||
github.com/MakeNowJust/heredoc v1.0.0 | |||
github.com/Nerzal/gocloak/v7 v7.11.0 | |||
github.com/aerogear/charmil v0.0.0-20210630101651-4ba3da6c5dbc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For merge we will need official tag/release
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets do release :)
pkg/cmd/root/root_test.go
Outdated
@@ -0,0 +1,104 @@ | |||
package root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not great for visibility.
We can call it:
- charmil/rules
- cobralint
etc.
We can also keep that in the root but then rename Test_ExecuteCommand
to be more descriptive
pkg/cmd/root/root_test.go
Outdated
var vali rules.RuleConfig | ||
validationErr := vali.ExecuteRules(cmd) | ||
for _, errs := range validationErr { | ||
if errs.Err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fail test if error happends.
pkg/cmd/root/root_test.go
Outdated
} | ||
} | ||
|
||
if len(validationErr) == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I have idea. How about we do keep test passing but have commented out assertions and follow up issue.
That will be better than changing rules. If we can make rules more adjusted to existing commands in rhoas + keep asertions to detect new commands but ignore errors that we currently have tat will be great deal.
Will there be a
Before merging ensure that the conventional commit type is different (a |
for now configurations can be done in the var vali rules.RuleConfig = rules.RuleConfig{
Verbose: true,
MustExist: rules.MustExist{
Fields: []string{"Args"},
},
} We had a talk here (aerogear/charmil#89 (comment)) and decided that having config file will work great for similar CLI based solution, but not for a testing framework. But this can be reconsidered according to the requirements!
sure 👍 |
Rebase needed. |
@wtrocki rebase done |
pkg/cmd/root/root_test.go
Outdated
"github.com/redhat-developer/app-services-cli/pkg/localize/goi18n" | ||
) | ||
|
||
func Test_ExecuteCommand(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename
pkg/cmd/root/root_test.go
Outdated
} | ||
} | ||
|
||
func initConfig(f *factory.Factory) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is this for? We have mockutil package that does mock config
pkg/cmd/root/root_test.go
Outdated
@@ -0,0 +1,47 @@ | |||
package root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validator_test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed filename
pkg/cmd/root/root_test.go
Outdated
"github.com/redhat-developer/app-services-cli/pkg/localize/goi18n" | ||
) | ||
|
||
func Test_RootRhoasCommand(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ValidateCommandsUsingCharmilValidator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test_ValidateCommandsUsingCharmilValidator
did this, to run the test
Please put all errors that are happening into new issue and add info how to enable unit tests after they are fixed: |
Description
Add charmil validator in root_test.go, for testing of cobra commands. The validator has some default rules defined, that can be overridden.
Currently Validator supports 2 rules:
logs
$ go test ./pkg/cmd/root/ commands checked: 35 checks failed: 47 --- FAIL: Test_ExecuteCommand (0.01s) root_test.go:40: LENGTH_RULE: cmd rhoas: Short length should be at least 15 root_test.go:40: LENGTH_RULE: cmd rhoas: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas cluster connect: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas cluster status: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas completion bash: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas completion bash: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas completion fish: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas completion fish: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas completion zsh: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas completion zsh: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas kafka consumergroup delete: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka consumergroup delete: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka consumergroup describe: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka consumergroup describe: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka consumergroup list: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic create: Short length should be at least 15 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic create: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic delete: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic delete: Short length should be at least 15 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic delete: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic describe: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic describe: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic list: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic update: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka consumergroup: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka consumergroup: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas kafka consumergroup: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas kafka list: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas kafka list: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka topic: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas kafka topic: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas serviceaccount delete: Example length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas cluster: Long length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas cluster: Long must be present root_test.go:40: LENGTH_RULE: cmd rhoas completion: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas completion: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas kafka: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas kafka: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas kafka: Long must be present root_test.go:40: MUST_EXIST_RULE: cmd rhoas kafka: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas logout: Long length should be at least 100 root_test.go:40: LENGTH_RULE: cmd rhoas logout: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas logout: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas serviceaccount: Example length should be at least 100 root_test.go:40: MUST_EXIST_RULE: cmd rhoas serviceaccount: Example must be present root_test.go:40: LENGTH_RULE: cmd rhoas whoami: Example length should be at least 100 FAIL FAIL github.com/redhat-developer/app-services-cli/pkg/cmd/root 0.023s FAIL
Verification Steps
go get github.com/aerogear/charmil
go test ./pkg/cmd/root
Type of change
Checklist