Skip to content

Commit 0bcd9df

Browse files
simplify, tighten some tests
1 parent 597acd3 commit 0bcd9df

4 files changed

Lines changed: 20 additions & 73 deletions

File tree

checks/jq_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package checks
22

33
import (
44
"reflect"
5-
"strings"
65
"testing"
76

87
api "github.com/bootdotdev/bootdev/client"
@@ -15,7 +14,7 @@ func TestRunStdoutJqQuery(t *testing.T) {
1514
test api.StdoutJqTest
1615
variables map[string]string
1716
want api.CLICommandJqOutput
18-
wantError string
17+
wantError bool
1918
}{
2019
{
2120
name: "queries json with interpolated query",
@@ -52,7 +51,7 @@ func TestRunStdoutJqQuery(t *testing.T) {
5251
want: api.CLICommandJqOutput{
5352
Query: `.name`,
5453
},
55-
wantError: "invalid character",
54+
wantError: true,
5655
},
5756
{
5857
name: "returns jq error",
@@ -63,20 +62,20 @@ func TestRunStdoutJqQuery(t *testing.T) {
6362
},
6463
want: api.CLICommandJqOutput{
6564
Query: `.name[`,
66-
Error: "unexpected EOF",
6765
},
66+
wantError: true,
6867
},
6968
}
7069

7170
for _, tt := range tests {
7271
t.Run(tt.name, func(t *testing.T) {
7372
got := runStdoutJqQuery(tt.stdout, tt.test, tt.variables)
74-
if tt.wantError != "" {
73+
if tt.wantError {
7574
if got.Query != tt.want.Query {
7675
t.Fatalf("Query = %q, want %q", got.Query, tt.want.Query)
7776
}
78-
if !strings.Contains(got.Error, tt.wantError) {
79-
t.Fatalf("expected error containing %q, got %q", tt.wantError, got.Error)
77+
if got.Error == "" {
78+
t.Fatal("expected an error")
8079
}
8180
return
8281
}

checks/local_test.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,19 @@ func TestLocalSubmissionEventReportsFirstFailure(t *testing.T) {
7373
}
7474
}
7575

76-
func TestEvaluateCLICommandReportsStdoutVariableParseError(t *testing.T) {
77-
cliData := api.CLIData{Steps: []api.CLIStep{
78-
{CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{
79-
{ExitCode: intPtr(0)},
80-
}}},
81-
}}
82-
results := []api.CLIStepResult{
83-
{CLICommandResult: &api.CLICommandResult{
84-
ExitCode: 0,
85-
Err: "invalid stdout variable configuration",
86-
}},
87-
}
76+
func TestEvaluateCLICommandReportsExecutionError(t *testing.T) {
77+
const message = "invalid stdout variable configuration"
78+
failure := evaluateCLICommandTests(
79+
0,
80+
api.CLIStepCLICommand{},
81+
api.CLICommandResult{Err: message},
82+
)
8883

89-
event := LocalSubmissionEvent(cliData, results)
90-
if event.ResultSlug != api.VerificationResultSlugFailure {
91-
t.Fatalf("ResultSlug = %q, want failure", event.ResultSlug)
92-
}
93-
if event.StructuredErrCLI == nil {
84+
if failure == nil {
9485
t.Fatal("expected structured failure")
9586
}
96-
if event.StructuredErrCLI.ErrorMessage != "invalid stdout variable configuration" {
97-
t.Fatalf("ErrorMessage = %q, want stdout variable error", event.StructuredErrCLI.ErrorMessage)
87+
if failure.ErrorMessage != message {
88+
t.Fatalf("ErrorMessage = %q, want %q", failure.ErrorMessage, message)
9889
}
9990
}
10091

checks/runner_test.go

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package checks
33
import (
44
"net/http"
55
"net/http/httptest"
6+
"reflect"
67
"testing"
78

89
api "github.com/bootdotdev/bootdev/client"
@@ -155,64 +156,26 @@ func TestApplySubmissionResultsStopsAfterFailedHTTPTest(t *testing.T) {
155156

156157
func applySubmissionResultsMessages(cliData api.CLIData, failure *api.StructuredErrCLI) []tea.Msg {
157158
ch := make(chan tea.Msg)
158-
done := make(chan struct{})
159159
go func() {
160160
defer close(ch)
161-
defer close(done)
162161
ApplySubmissionResults(cliData, failure, ch)
163162
}()
164163

165164
var msgs []tea.Msg
166165
for msg := range ch {
167166
msgs = append(msgs, msg)
168167
}
169-
<-done
170168
return msgs
171169
}
172170

173171
func assertMessages(t *testing.T, got []tea.Msg, want []tea.Msg) {
174172
t.Helper()
175173

176-
if len(got) != len(want) {
177-
t.Fatalf("got %d messages, want %d\ngot: %#v\nwant: %#v", len(got), len(want), got, want)
178-
}
179-
for i := range want {
180-
assertMessage(t, i, got[i], want[i])
181-
}
182-
}
183-
184-
func assertMessage(t *testing.T, index int, got tea.Msg, want tea.Msg) {
185-
t.Helper()
186-
187-
switch want := want.(type) {
188-
case messages.ResolveStepMsg:
189-
got, ok := got.(messages.ResolveStepMsg)
190-
if !ok {
191-
t.Fatalf("message %d = %T, want %T", index, got, want)
192-
}
193-
if got.Index != want.Index || !sameBoolPtr(got.Passed, want.Passed) {
194-
t.Fatalf("message %d = %#v, want %#v", index, got, want)
195-
}
196-
case messages.ResolveTestMsg:
197-
got, ok := got.(messages.ResolveTestMsg)
198-
if !ok {
199-
t.Fatalf("message %d = %T, want %T", index, got, want)
200-
}
201-
if got.StepIndex != want.StepIndex || got.TestIndex != want.TestIndex || !sameBoolPtr(got.Passed, want.Passed) {
202-
t.Fatalf("message %d = %#v, want %#v", index, got, want)
203-
}
204-
default:
205-
t.Fatalf("unsupported wanted message type %T", want)
174+
if !reflect.DeepEqual(got, want) {
175+
t.Fatalf("messages = %#v, want %#v", got, want)
206176
}
207177
}
208178

209179
func boolPtr(v bool) *bool {
210180
return &v
211181
}
212-
213-
func sameBoolPtr(a *bool, b *bool) bool {
214-
if a == nil || b == nil {
215-
return a == b
216-
}
217-
return *a == *b
218-
}

render/variables_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ func TestHTTPVariableSections(t *testing.T) {
3535
},
3636
}
3737

38-
got := renderVariableSection("Variables Saved", savedVariablesForHTTPResult(result))
39-
got += renderVariableSection("Variables Missing", missingSaveVariablesForHTTPResult(result))
40-
available, expectsVariables := availableVariablesForHTTPResult(result)
41-
if !expectsVariables {
42-
t.Fatalf("expected HTTP request to use variables")
43-
}
44-
got += renderVariableSection("Variables Available", available)
38+
got := printHTTPRequestResult(result)
4539

4640
wantContains := []string{
4741
"Variables Saved:",

0 commit comments

Comments
 (0)