Skip to content

Commit 6fc866a

Browse files
distinguish variable not found from empty
1 parent c787fd9 commit 6fc866a

2 files changed

Lines changed: 59 additions & 13 deletions

File tree

render/variables.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type variableEntry struct {
1414
name string
1515
value string
16+
found bool
1617
description string
1718
}
1819

@@ -27,41 +28,46 @@ func renderVariableSection(title string, entries []variableEntry) string {
2728
var str strings.Builder
2829
fmt.Fprintf(&str, " %s: \n", title)
2930
for _, entry := range entries {
30-
fmt.Fprintf(&str, " - %s: %s (%s)\n", entry.name, formatVariableValue(entry.value), entry.description)
31+
fmt.Fprintf(&str, " - %s: %s (%s)\n", entry.name, formatVariableValue(entry.value, entry.found), entry.description)
3132
}
3233
return str.String()
3334
}
3435

35-
func formatVariableValue(value string) string {
36-
if value == "" {
36+
func formatVariableValue(value string, found bool) string {
37+
if !found {
3738
return "[not found]"
3839
}
40+
if value == "" {
41+
return "[empty]"
42+
}
3943
return value
4044
}
4145

4246
func savedVariablesForHTTPResult(result api.HTTPRequestResult) []variableEntry {
4347
var entries []variableEntry
4448
for _, responseVariable := range result.Request.ResponseVariables {
45-
value := result.Variables[responseVariable.Name]
46-
if value == "" {
49+
value, found := result.Variables[responseVariable.Name]
50+
if !found {
4751
continue
4852
}
4953

5054
description := responseVariableDescription(responseVariable)
5155
entries = append(entries, variableEntry{
5256
name: responseVariable.Name,
5357
value: value,
58+
found: true,
5459
description: description,
5560
})
5661
}
5762
for _, responseHeaderVariable := range result.Request.ResponseHeaderVariables {
58-
value := result.Variables[responseHeaderVariable.Name]
59-
if value == "" {
63+
value, found := result.Variables[responseHeaderVariable.Name]
64+
if !found {
6065
continue
6166
}
6267
entries = append(entries, variableEntry{
6368
name: responseHeaderVariable.Name,
6469
value: value,
70+
found: true,
6571
description: responseHeaderVariableDescription(responseHeaderVariable),
6672
})
6773
}
@@ -71,7 +77,7 @@ func savedVariablesForHTTPResult(result api.HTTPRequestResult) []variableEntry {
7177
func missingSaveVariablesForHTTPResult(result api.HTTPRequestResult) []variableEntry {
7278
var entries []variableEntry
7379
for _, responseVariable := range result.Request.ResponseVariables {
74-
if result.Variables[responseVariable.Name] != "" {
80+
if _, found := result.Variables[responseVariable.Name]; found {
7581
continue
7682
}
7783

@@ -82,7 +88,7 @@ func missingSaveVariablesForHTTPResult(result api.HTTPRequestResult) []variableE
8288
})
8389
}
8490
for _, responseHeaderVariable := range result.Request.ResponseHeaderVariables {
85-
if result.Variables[responseHeaderVariable.Name] != "" {
91+
if _, found := result.Variables[responseHeaderVariable.Name]; found {
8692
continue
8793
}
8894
entries = append(entries, variableEntry{
@@ -108,7 +114,7 @@ func availableVariablesForHTTPResult(result api.HTTPRequestResult) (entries []va
108114
return
109115
}
110116
expectsVariables = true
111-
value := result.Variables[name]
117+
value, found := result.Variables[name]
112118
key := name + "\x00" + description
113119
if seen[key] {
114120
return
@@ -117,6 +123,7 @@ func availableVariablesForHTTPResult(result api.HTTPRequestResult) (entries []va
117123
entries = append(entries, variableEntry{
118124
name: name,
119125
value: value,
126+
found: found,
120127
description: description,
121128
})
122129
}
@@ -167,7 +174,7 @@ func availableVariablesForCLIResult(result api.CLICommandResult) (entries []vari
167174

168175
add := func(name, description string) {
169176
expectsVariables = true
170-
value := result.Variables[name]
177+
value, found := result.Variables[name]
171178
key := name + "\x00" + description
172179
if seen[key] {
173180
return
@@ -176,6 +183,7 @@ func availableVariablesForCLIResult(result api.CLICommandResult) (entries []vari
176183
entries = append(entries, variableEntry{
177184
name: name,
178185
value: value,
186+
found: found,
179187
description: description,
180188
})
181189
}

render/variables_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,48 @@ func TestAvailableVariablesPrintsNotFoundWhenExpectedButUnavailable(t *testing.T
8383
}
8484
}
8585

86+
func TestHTTPVariableSectionsDistinguishEmptyFromMissing(t *testing.T) {
87+
result := api.HTTPRequestResult{
88+
Variables: map[string]string{"emptyCode": ""},
89+
Request: api.CLIStepHTTPRequest{
90+
ResponseVariables: []api.HTTPRequestResponseVariable{
91+
{Name: "emptyCode", Path: ".empty_code"},
92+
{Name: "missingCode", Path: ".missing_code"},
93+
},
94+
Request: api.HTTPRequest{
95+
Headers: map[string]string{"X-Code": "${emptyCode}"},
96+
},
97+
},
98+
}
99+
100+
got := printHTTPRequestResult(result)
101+
for _, expected := range []string{
102+
"emptyCode: [empty] (JSON Body .empty_code)",
103+
"missingCode: [not found] (JSON Body .missing_code)",
104+
"emptyCode: [empty] (Request Header \"X-Code\")",
105+
} {
106+
if !strings.Contains(got, expected) {
107+
t.Errorf("output missing %q\n%s", expected, got)
108+
}
109+
}
110+
for _, unexpected := range []string{
111+
"emptyCode: [not found]",
112+
"missingCode: [empty]",
113+
} {
114+
if strings.Contains(got, unexpected) {
115+
t.Errorf("output unexpectedly contains %q\n%s", unexpected, got)
116+
}
117+
}
118+
}
119+
86120
func TestCLIAvailableVariables(t *testing.T) {
87121
result := api.CLICommandResult{
88122
Variables: map[string]string{
89-
"url": "http://localhost:42069",
123+
"empty": "",
124+
"url": "http://localhost:42069",
90125
},
91126
Command: api.CLIStepCLICommand{
92-
Command: "curl ${url}",
127+
Command: "curl ${url} ${empty}",
93128
Tests: []api.CLICommandTest{
94129
{StdoutContainsAll: []string{"${expected}"}},
95130
},
@@ -105,6 +140,9 @@ func TestCLIAvailableVariables(t *testing.T) {
105140
if !strings.Contains(got, "url: http://localhost:42069 (Command)") {
106141
t.Fatalf("expected url entry in:\n%s", got)
107142
}
143+
if !strings.Contains(got, "empty: [empty] (Command)") {
144+
t.Fatalf("expected empty entry in:\n%s", got)
145+
}
108146
if !strings.Contains(got, "expected: [not found] (Stdout Contains Test)") {
109147
t.Fatalf("expected missing expected entry in:\n%s", got)
110148
}

0 commit comments

Comments
 (0)