Skip to content

Commit

Permalink
Simplified ParsePathSpec to not use a regexp (more readable now)
Browse files Browse the repository at this point in the history
see #72658
  • Loading branch information
martinrode committed Jun 7, 2024
1 parent 9413bff commit b8e281b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
39 changes: 12 additions & 27 deletions pkg/lib/util/path_util.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package util

import (
"regexp"
"strconv"
"strings"
)

// pathSpecRegex validates and (via its capture groups) breaks up a
// path spec string in a single step (see ParsePathSpec).
var pathSpecRegex *regexp.Regexp = regexp.MustCompile(`^([0-9]*)@(.+)$`)

// PathSpec is a path specifier for including tests within manifests.
type PathSpec struct {
// ParallelRuns matches the number of parallel runs specified
Expand All @@ -21,30 +17,19 @@ type PathSpec struct {

// ParsePathSpec tries to parse the given string into a PathSpec.
//
// It returns a boolean result that indicates if parsing was successful
// (i.e. if s is a valid path specifier).
func ParsePathSpec(s string) (PathSpec, bool) {
matches := pathSpecRegex.FindStringSubmatch(s)
if matches == nil {
return PathSpec{}, false
}

spec := PathSpec{
ParallelRuns: 1,
Path: matches[2], // can't be empty, or else it wouldn't match the regex
// It returns a boolean result that indicates if parsing was successful (i.e. if
// s is a valid path specifier). The string takes the format "[n]@file.json".
func ParsePathSpec(s string) (spec PathSpec, ok bool) {
var parallelRuns string
parallelRuns, spec.Path, ok = strings.Cut(s, "@")
if parallelRuns != "" {
spec.ParallelRuns, _ = strconv.Atoi(parallelRuns)
} else {
spec.ParallelRuns = 1
}

// Determine number of parallel runs, if supplied
if matches[1] != "" {
prs, err := strconv.Atoi(matches[1])
if err != nil {
// matches[1] is all digits, so there must be something seriously wrong
panic("error Atoi-ing all-decimal regex match")
}

spec.ParallelRuns = prs
if !ok || spec.Path == "" || spec.ParallelRuns <= 0 {
return PathSpec{}, false
}

return spec, true
}

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/check_collected_responses.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{
"name": "bounce-json: bounce collected responses from N={{datastore "n_parallel"}} parallel runs: {{ datastore "responses" }}",
"name": "bounce-json: bounce collected responses from N={{datastore `n_parallel` }} parallel runs: {{ datastore `responses` }}",
"request": {
"server_url": "http://localhost{{ datastore "local_port" }}",
"server_url": "http://localhost{{ datastore `local_port` }}",
"endpoint": "bounce-json",
"method": "POST",
"body": {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/parallel.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"name": "bounce-json: bounce n=1",
"request": {
"server_url": "http://localhost{{ datastore "local_port" }}",
"server_url": "http://localhost{{ datastore `local_port` }}",
"endpoint": "bounce-json",
"method": "POST",
"body": {
Expand Down

0 comments on commit b8e281b

Please sign in to comment.