Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions checks/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"maps"
"os"
"os/exec"
"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -122,31 +121,6 @@ func runCLICommandWithOutputLimit(
return result
}

func parseStdoutVariables(stdout string, vardefs []api.CLICommandStdoutVariable, variables map[string]string) error {
for _, vardef := range vardefs {
if vardef.Name == "" {
return fmt.Errorf("invalid stdout variable configuration")
}
if vardef.Regex == "" {
return fmt.Errorf("invalid stdout variable configuration")
}
re, err := regexp.Compile(vardef.Regex)
if err != nil {
return fmt.Errorf("invalid stdout variable configuration")
}
if re.NumSubexp() != 1 {
return fmt.Errorf("invalid stdout variable configuration")
}

matches := re.FindStringSubmatch(stdout)
if len(matches) == 2 {
variables[vardef.Name] = matches[1]
}
}

return nil
}

func prettyPrintCLICommand(test api.CLICommandTest, variables map[string]string) string {
var descriptions []string
if test.ExitCode != nil {
Expand Down Expand Up @@ -178,7 +152,7 @@ func prettyPrintCLICommand(test api.CLICommandTest, variables map[string]string)
}

if test.StdoutJq != nil {
descriptions = append(descriptions, prettyPrintStdoutJqTest(*test.StdoutJq, variables))
descriptions = append(descriptions, prettyPrintStdoutJqTest(*test.StdoutJq))
}

return strings.Join(descriptions, "\n")
Expand Down
28 changes: 4 additions & 24 deletions checks/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,6 @@ func TestRunCLICommandCapsOutput(t *testing.T) {
}
}

func TestRunCLICommandCapturesStdoutVariables(t *testing.T) {
variables := map[string]string{}
result := runCLICommand(api.CLIStepCLICommand{
Command: `go env GOOS`,
StdoutVariables: []api.CLICommandStdoutVariable{{
Name: "goos",
Regex: `([a-z0-9]+)`,
}},
}, variables, defaultShell())

if result.Err != "" {
t.Fatalf("unexpected command error: %s", result.Err)
}
if result.Variables["goos"] != runtime.GOOS {
t.Fatalf("captured goos = %q, want %q", result.Variables["goos"], runtime.GOOS)
}
if variables["goos"] != runtime.GOOS {
t.Fatalf("shared goos = %q, want %q", variables["goos"], runtime.GOOS)
}
}

func TestRunCLICommandKeepsStderrSeparateFromStdoutChecks(t *testing.T) {
command := `printf 'stdout-value\n'; printf 'stderr-value\n' >&2`
if runtime.GOOS == "windows" {
Expand All @@ -89,9 +68,6 @@ func TestRunCLICommandKeepsStderrSeparateFromStdoutChecks(t *testing.T) {
if result.Stderr != "stderr-value" {
t.Fatalf("stderr = %q, want stderr-value", result.Stderr)
}
if strings.Contains(result.Stdout, "stderr-value") {
t.Fatalf("stdout unexpectedly contains stderr: %q", result.Stdout)
}
if _, ok := variables["stderr_value"]; ok {
t.Fatalf("stderr unexpectedly populated a stdout variable")
}
Expand All @@ -114,6 +90,10 @@ func TestRunCLICommandInterpolatesCapturedStdoutVariables(t *testing.T) {
t.Fatalf("unexpected first command error: %s", first.Err)
}

if first.Variables["goenv"] != "GOOS" {
t.Fatalf("captured variable = %q, want GOOS", first.Variables["goenv"])
}

second := runCLICommand(api.CLIStepCLICommand{
Command: `go env ${goenv}`,
}, variables, defaultShell())
Expand Down
114 changes: 0 additions & 114 deletions checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"maps"
"net/http"
"net/url"
"regexp"
"slices"
"strings"
"unicode/utf8"
Expand All @@ -21,8 +20,6 @@ const (
maxBinaryBodyBytes = 16 * 1024
)

var interpolationPattern = regexp.MustCompile(`\$\{([^}]+)\}`)

func runHTTPRequest(
client *http.Client,
baseURL string,
Expand Down Expand Up @@ -124,27 +121,6 @@ func runHTTPRequest(
return result
}

func interpolateJSONStrings(value any, variables map[string]string) any {
switch value := value.(type) {
case string:
return InterpolateVariables(value, variables)
case []any:
interpolated := make([]any, len(value))
for i, item := range value {
interpolated[i] = interpolateJSONStrings(item, variables)
}
return interpolated
case map[string]any:
interpolated := make(map[string]any, len(value))
for key, item := range value {
interpolated[key] = interpolateJSONStrings(item, variables)
}
return interpolated
default:
return value
}
}

func prettyPrintHTTPTest(test api.HTTPRequestTest, variables map[string]string) string {
var descriptions []string
if test.StatusCode != nil {
Expand Down Expand Up @@ -217,74 +193,6 @@ func truncateAndStringifyBody(body []byte) string {
return string(body)
}

func parseVariables(body []byte, vardefs []api.HTTPRequestResponseVariable, variables map[string]string) error {
bodyString := string(body)

for _, vardef := range vardefs {
switch {
case vardef.Path != "" && vardef.BodyRegex != "":
return fmt.Errorf("invalid response variable configuration")

case vardef.BodyRegex != "":
re, err := regexp.Compile(vardef.BodyRegex)
if err != nil {
return fmt.Errorf("invalid response body variable configuration")
}
if re.NumSubexp() != 1 {
return fmt.Errorf("invalid response body variable configuration")
}
matches := re.FindStringSubmatch(bodyString)
if len(matches) == 2 {
variables[vardef.Name] = matches[1]
}

case vardef.Path != "":
vals, err := valsFromJqPath(vardef.Path, bodyString)
if err != nil {
return err
}
if len(vals) == 1 && vals[0] != nil {
variables[vardef.Name] = fmt.Sprintf("%v", vals[0])
}

default:
return fmt.Errorf("invalid response variable configuration")
}
}

return nil
}

func parseHeaderVariables(headers map[string]string, vardefs []api.HTTPRequestResponseHeaderVariable, variables map[string]string) error {
for _, vardef := range vardefs {
headerValue, ok := findHeaderValue(headers, vardef.Header)
if !ok {
continue
}

value := headerValue
if vardef.Regex != "" {
re, err := regexp.Compile(vardef.Regex)
if err != nil {
return fmt.Errorf("invalid response header variable configuration")
}
if re.NumSubexp() != 1 {
return fmt.Errorf("invalid response header variable configuration")
}

matches := re.FindStringSubmatch(headerValue)
if len(matches) != 2 {
continue
}
value = matches[1]
}

variables[vardef.Name] = value
}

return nil
}

func findHeaderValue(headers map[string]string, key string) (string, bool) {
for actualKey, value := range headers {
if strings.EqualFold(actualKey, key) {
Expand All @@ -294,28 +202,6 @@ func findHeaderValue(headers map[string]string, key string) (string, bool) {
return "", false
}

func InterpolateVariables(template string, vars map[string]string) string {
return interpolationPattern.ReplaceAllStringFunc(template, func(m string) string {
// Extract the key from the match, which is in the form ${key}
key := strings.TrimSuffix(strings.TrimPrefix(m, "${"), "}")
if val, ok := vars[key]; ok {
return val
}
return m
})
}

func InterpolationNames(template string) []string {
matches := interpolationPattern.FindAllStringSubmatch(template, -1)
names := make([]string, 0, len(matches))
for _, match := range matches {
if len(match) > 1 {
names = append(names, match[1])
}
}
return names
}

func likelyBinary(b []byte) bool {
if len(b) == 0 {
return false
Expand Down
Loading
Loading