Skip to content

Commit

Permalink
enhance: add additional explain options to hubtest (#3162)
Browse files Browse the repository at this point in the history
* enhance: add additional explain options to hubtest

* Revert "enhance: add additional explain options to hubtest"

This reverts commit b24632f.

* enhance: add additional explain options to hubtest

---------

Co-authored-by: marco <[email protected]>
  • Loading branch information
LaurenceJJones and mmetc authored Sep 3, 2024
1 parent bc6be99 commit fb0117e
Showing 1 changed file with 51 additions and 26 deletions.
77 changes: 51 additions & 26 deletions cmd/crowdsec-cli/clihubtest/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,71 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/dumps"
)


func (cli *cliHubTest) explain(testName string, details bool, skipOk bool) error {
test, err := HubTest.LoadTestItem(testName)
if err != nil {
return fmt.Errorf("can't load test: %+v", err)
}

err = test.ParserAssert.LoadTest(test.ParserResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}

if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil {
return fmt.Errorf("unable to load parser result after run: %w", err)
}
}

err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}

if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil {
return fmt.Errorf("unable to load scenario result after run: %w", err)
}
}

opts := dumps.DumpOpts{
Details: details,
SkipOk: skipOk,
}

dumps.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts)

return nil
}


func (cli *cliHubTest) NewExplainCmd() *cobra.Command {
var (
details bool
skipOk bool
)

cmd := &cobra.Command{
Use: "explain",
Short: "explain [test_name]",
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, args []string) error {
for _, testName := range args {
test, err := HubTest.LoadTestItem(testName)
if err != nil {
return fmt.Errorf("can't load test: %+v", err)
if err := cli.explain(testName, details, skipOk); err != nil {
return err
}
err = test.ParserAssert.LoadTest(test.ParserResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}

if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil {
return fmt.Errorf("unable to load parser result after run: %w", err)
}
}

err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}

if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil {
return fmt.Errorf("unable to load scenario result after run: %w", err)
}
}
opts := dumps.DumpOpts{}
dumps.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts)
}

return nil
},
}

flags := cmd.Flags()
flags.BoolVarP(&details, "verbose", "v", false, "Display individual changes")
flags.BoolVar(&skipOk, "failures", false, "Only show failed lines")

return cmd
}

0 comments on commit fb0117e

Please sign in to comment.