Skip to content

Commit

Permalink
path_util: Changing ParsePathSpec to return error instead of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jun 7, 2024
1 parent ee47406 commit 982a026
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
11 changes: 7 additions & 4 deletions api_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,16 @@ func (ats *Suite) parseAndRunTest(
loader.ServerURL = serverURL
loader.OAuthClient = ats.Config.OAuthClient

// Determine number of parallel runs
// Parse PathSpec (if any) and determine number of parallel runs
parallelRuns := 1
if vStr, ok := v.(string); ok {
pathSpec, ok := util.ParsePathSpec(vStr)
if ok {
parallelRuns = pathSpec.ParallelRuns
pathSpec, err := util.ParsePathSpec(vStr)
if err != nil {
logrus.Error(fmt.Errorf("test string is not a valid path spec: %w", err))
return false
}

parallelRuns = pathSpec.ParallelRuns
}

// Configure parallel runs, if specified
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var c = &http.Client{

// OpenFileOrUrl opens either a local file or gives the resp.Body from a remote file
func OpenFileOrUrl(path, rootDir string) (string, io.ReadCloser, error) {
pathSpec, ok := ParsePathSpec(path)
if ok {
pathSpec, err := ParsePathSpec(path)
if err == nil {
path = pathSpec.Path
}

Expand Down
24 changes: 16 additions & 8 deletions pkg/lib/util/path_util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"fmt"
"strconv"
"strings"
)
Expand All @@ -17,25 +18,32 @@ 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). The string takes the format "[n]@file.json".
func ParsePathSpec(s string) (spec PathSpec, ok bool) {
// The string takes the format "[n]@file.json". Invalid path specs
// result in an error.
func ParsePathSpec(s string) (spec PathSpec, err error) {
var ok bool
var parallelRuns string

parallelRuns, spec.Path, ok = strings.Cut(s, "@")
if parallelRuns != "" {
spec.ParallelRuns, _ = strconv.Atoi(parallelRuns)
spec.ParallelRuns, err = strconv.Atoi(parallelRuns)
if err != nil {
return PathSpec{}, fmt.Errorf("error parsing ParallelRuns of path spec %q: %w", s, err)
}
} else {
spec.ParallelRuns = 1
}

if !ok || spec.Path == "" || spec.ParallelRuns <= 0 {
return PathSpec{}, false
return PathSpec{}, fmt.Errorf("invalid path spec %q", s)
}
return spec, true

return spec, err
}

// IsPathSpec is a wrapper around ParsePathSpec that discards the parsed PathSpec.
// It's useful for chaining within boolean expressions.
func IsPathSpec(s string) bool {
_, ok := ParsePathSpec(s)
return ok
_, err := ParsePathSpec(s)
return err == nil
}
8 changes: 4 additions & 4 deletions pkg/lib/util/path_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestParsePathSpec(t *testing.T) {
testCase := testCases[i]

t.Run(testCase.s, func(t *testing.T) {
actual, ok := ParsePathSpec(testCase.s)
require.True(t, ok)
actual, err := ParsePathSpec(testCase.s)
require.NoError(t, err)
require.Equal(t, testCase.expected, actual)
})
}
Expand All @@ -58,8 +58,8 @@ func TestParsePathSpec(t *testing.T) {
s := testCase

t.Run(s, func(t *testing.T) {
actual, ok := ParsePathSpec(s)
require.False(t, ok)
actual, err := ParsePathSpec(s)
require.Error(t, err)
require.Zero(t, actual)
})
}
Expand Down

0 comments on commit 982a026

Please sign in to comment.