Skip to content

Commit

Permalink
Merge pull request #3103 from oasisprotocol/tjanez/oasis-test-runner-…
Browse files Browse the repository at this point in the history
…limit-regex

go/oasis-test-runner/cmd: Limit scenario name regex matching
  • Loading branch information
tjanez authored Jul 14, 2020
2 parents db0c32b + fecdd17 commit 0466b12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changelog/3103.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go/oasis-test-runner/cmd: Limit scenario name regex matching

Prevent oasis-test-runner to match too many scenarios for a given
scenario name regex by ensuring the given scenario name regex matches
the whole scenario name.
18 changes: 10 additions & 8 deletions go/oasis-test-runner/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,17 @@ func runRoot(cmd *cobra.Command, args []string) error {

// Enumerate requested scenarios.
toRun := common.GetDefaultScenarios() // Run all default scenarios if not set.
if vec := viper.GetStringSlice(common.CfgTest); len(vec) > 0 {
if scNameRegexes := viper.GetStringSlice(common.CfgTest); len(scNameRegexes) > 0 {
matched := make(map[scenario.Scenario]bool)
for _, v := range vec {
name := strings.ToLower(v)
for _, scNameRegex := range scNameRegexes {
// Make sure the given scenario name regex matches the whole scenario name, not just
// a substring.
regex := fmt.Sprintf("^%s$", scNameRegex)

var anyMatched bool
for scName, scenario := range common.GetScenarios() {
var match bool
match, err = regexp.MatchString(name, scName)
match, err = regexp.MatchString(regex, scName)
if err != nil {
return fmt.Errorf("root: bad scenario name regexp: %w", err)
}
Expand All @@ -289,11 +291,11 @@ func runRoot(cmd *cobra.Command, args []string) error {
}
}
if !anyMatched {
logger.Error("unknown scenario",
"scenario", name,
logger.Error("no scenario matches regex",
"scenario_regex", scNameRegex,
)
return fmt.Errorf("root: unknown scenario: %s\nAvailable scenarios:\n%s",
name, strings.Join(common.GetScenarioNames(), "\n"),
return fmt.Errorf("root: no scenario matches regex: %s\nAvailable scenarios:\n%s",
scNameRegex, strings.Join(common.GetScenarioNames(), "\n"),
)
}
}
Expand Down

0 comments on commit 0466b12

Please sign in to comment.