From fecdd17990a2279ffa67c000ac34a0fa0eabeb08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Jane=C5=BE?= Date: Mon, 13 Jul 2020 12:13:12 +0200 Subject: [PATCH] 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. --- .changelog/3103.internal.md | 5 +++++ go/oasis-test-runner/cmd/root.go | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 .changelog/3103.internal.md diff --git a/.changelog/3103.internal.md b/.changelog/3103.internal.md new file mode 100644 index 00000000000..7dc41d41aa6 --- /dev/null +++ b/.changelog/3103.internal.md @@ -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. diff --git a/go/oasis-test-runner/cmd/root.go b/go/oasis-test-runner/cmd/root.go index 39a527b17a5..aadd4dd8c23 100644 --- a/go/oasis-test-runner/cmd/root.go +++ b/go/oasis-test-runner/cmd/root.go @@ -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) } @@ -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"), ) } }