Skip to content

Commit e9734b9

Browse files
authored
Update pass/fail UI (#118)
update pass/fail UI
1 parent bef09c9 commit e9734b9

File tree

5 files changed

+60
-203
lines changed

5 files changed

+60
-203
lines changed

checks/checks.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ func sendCLICommandResults(ch chan tea.Msg, cmd api.CLIStepCLICommand, result ap
188188
}
189189

190190
for j := range cmd.Tests {
191-
ch <- messages.ResolveTestMsg{Index: j}
191+
ch <- messages.ResolveTestMsg{
192+
StepIndex: index,
193+
TestIndex: j,
194+
}
192195
}
193196

194197
ch <- messages.ResolveStepMsg{
@@ -205,7 +208,10 @@ func sendHTTPRequestResults(ch chan tea.Msg, req api.CLIStepHTTPRequest, result
205208
}
206209

207210
for j := range req.Tests {
208-
ch <- messages.ResolveTestMsg{Index: j}
211+
ch <- messages.ResolveTestMsg{
212+
StepIndex: index,
213+
TestIndex: j,
214+
}
209215
}
210216

211217
ch <- messages.ResolveStepMsg{
@@ -216,6 +222,44 @@ func sendHTTPRequestResults(ch chan tea.Msg, req api.CLIStepHTTPRequest, result
216222
}
217223
}
218224

225+
func ApplySubmissionResults(cliData api.CLIData, failure *api.VerificationResultStructuredErrCLI, ch chan tea.Msg) {
226+
for i, step := range cliData.Steps {
227+
pass := true
228+
if failure != nil {
229+
pass = i < failure.FailedStepIndex
230+
}
231+
232+
ch <- messages.ResolveStepMsg{
233+
Index: i,
234+
Passed: &pass,
235+
}
236+
237+
if step.CLICommand != nil {
238+
for j := range step.CLICommand.Tests {
239+
ch <- messages.ResolveTestMsg{
240+
StepIndex: i,
241+
TestIndex: j,
242+
Passed: &pass,
243+
}
244+
}
245+
}
246+
if step.HTTPRequest != nil {
247+
for j := range step.HTTPRequest.Tests {
248+
ch <- messages.ResolveTestMsg{
249+
StepIndex: i,
250+
TestIndex: j,
251+
Passed: &pass,
252+
}
253+
}
254+
}
255+
256+
if !pass {
257+
break
258+
}
259+
260+
}
261+
}
262+
219263
func prettyPrintCLICommand(test api.CLICommandTest, variables map[string]string) string {
220264
if test.ExitCode != nil {
221265
return fmt.Sprintf("Expect exit code %d", *test.ExitCode)

cmd/submit.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
7575
if err != nil {
7676
return err
7777
}
78+
checks.ApplySubmissionResults(data, failure, ch)
79+
7880
finalise(failure)
7981
} else {
8082
finalise(nil)

messages/messages.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type StartTestMsg struct {
1414
}
1515

1616
type ResolveTestMsg struct {
17-
Index int
18-
Passed *bool
17+
StepIndex int
18+
TestIndex int
19+
Passed *bool
1920
}
2021

2122
type DoneStepMsg struct {

render/render.go

Lines changed: 8 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
144144
case messages.ResolveStepMsg:
145145
m.steps[msg.Index].passed = msg.Passed
146146
m.steps[msg.Index].finished = true
147-
m.steps[msg.Index].result = msg.Result
147+
if msg.Result != nil {
148+
m.steps[msg.Index].result = msg.Result
149+
}
148150
return m, nil
149151

150152
case messages.StartTestMsg:
@@ -155,8 +157,8 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
155157
return m, nil
156158

157159
case messages.ResolveTestMsg:
158-
m.steps[len(m.steps)-1].tests[msg.Index].passed = msg.Passed
159-
m.steps[len(m.steps)-1].tests[msg.Index].finished = true
160+
m.steps[msg.StepIndex].tests[msg.TestIndex].passed = msg.Passed
161+
m.steps[msg.StepIndex].tests[msg.TestIndex].finished = true
160162
return m, nil
161163

162164
default:
@@ -202,44 +204,16 @@ func (m rootModel) View() string {
202204
}
203205
}
204206
if m.failure != nil {
205-
str += red.Render("\n\nError: "+m.failure.ErrorMessage) + "\n\n"
207+
str += "\n\n" + red.Render("Tests failed! ❌")
208+
str += red.Render(fmt.Sprintf("\n\nFailed Step: %v", m.failure.FailedStepIndex+1))
209+
str += red.Render("\nError: "+m.failure.ErrorMessage) + "\n\n"
206210
} else if m.success {
207211
str += "\n\n" + green.Render("All tests passed! 🎉") + "\n\n"
208212
str += green.Render("Return to your browser to continue with the next lesson.") + "\n\n"
209213
}
210214
return str
211215
}
212216

213-
func prettyPrintCLICommand(test api.CLICommandTest, variables map[string]string) string {
214-
if test.ExitCode != nil {
215-
return fmt.Sprintf("Expect exit code %d", *test.ExitCode)
216-
}
217-
if test.StdoutLinesGt != nil {
218-
return fmt.Sprintf("Expect > %d lines on stdout", *test.StdoutLinesGt)
219-
}
220-
if test.StdoutContainsAll != nil {
221-
str := "Expect stdout to contain all of:"
222-
for _, contains := range test.StdoutContainsAll {
223-
interpolatedContains := checks.InterpolateVariables(contains, variables)
224-
str += fmt.Sprintf("\n - '%s'", interpolatedContains)
225-
}
226-
return str
227-
}
228-
if test.StdoutContainsNone != nil {
229-
str := "Expect stdout to contain none of:"
230-
for _, containsNone := range test.StdoutContainsNone {
231-
interpolatedContainsNone := checks.InterpolateVariables(containsNone, variables)
232-
str += fmt.Sprintf("\n - '%s'", interpolatedContainsNone)
233-
}
234-
return str
235-
}
236-
return ""
237-
}
238-
239-
func pointerToBool(a bool) *bool {
240-
return &a
241-
}
242-
243217
func printHTTPRequestResult(result api.HTTPRequestResult) string {
244218
if result.Err != "" {
245219
return fmt.Sprintf(" Err: %v\n\n", result.Err)
@@ -358,167 +332,3 @@ func StartRenderer(data api.CLIData, isSubmit bool, ch chan tea.Msg) func(*api.V
358332
wg.Wait()
359333
}
360334
}
361-
362-
func renderCLICommand(
363-
cmd api.CLIStepCLICommand,
364-
result api.CLICommandResult,
365-
failure *api.VerificationResultStructuredErrCLI,
366-
isSubmit bool,
367-
ch chan tea.Msg,
368-
index int,
369-
) {
370-
for _, test := range cmd.Tests {
371-
ch <- messages.StartTestMsg{Text: prettyPrintCLICommand(test, result.Variables)}
372-
}
373-
374-
earlierCmdFailed := false
375-
if failure != nil {
376-
earlierCmdFailed = failure.FailedStepIndex < index
377-
}
378-
for j := range cmd.Tests {
379-
earlierTestFailed := false
380-
if failure != nil {
381-
if earlierCmdFailed {
382-
earlierTestFailed = true
383-
} else if failure.FailedStepIndex == index {
384-
earlierTestFailed = failure.FailedTestIndex < j
385-
}
386-
}
387-
if !isSubmit {
388-
ch <- messages.ResolveTestMsg{Index: j}
389-
} else if earlierTestFailed {
390-
ch <- messages.ResolveTestMsg{Index: j}
391-
} else {
392-
passed := failure == nil || failure.FailedStepIndex != index || failure.FailedTestIndex != j
393-
ch <- messages.ResolveTestMsg{
394-
Index: j,
395-
Passed: pointerToBool(passed),
396-
}
397-
}
398-
}
399-
400-
if !isSubmit {
401-
ch <- messages.ResolveStepMsg{
402-
Index: index,
403-
Result: &api.CLIStepResult{
404-
CLICommandResult: &result,
405-
},
406-
}
407-
} else if earlierCmdFailed {
408-
ch <- messages.ResolveStepMsg{Index: index}
409-
} else {
410-
passed := failure == nil || failure.FailedStepIndex != index
411-
if passed {
412-
ch <- messages.ResolveStepMsg{
413-
Index: index,
414-
Passed: pointerToBool(passed),
415-
}
416-
} else {
417-
ch <- messages.ResolveStepMsg{
418-
Index: index,
419-
Passed: pointerToBool(passed),
420-
Result: &api.CLIStepResult{
421-
CLICommandResult: &result,
422-
},
423-
}
424-
}
425-
}
426-
}
427-
428-
func renderHTTPRequest(
429-
req api.CLIStepHTTPRequest,
430-
result api.HTTPRequestResult,
431-
failure *api.VerificationResultStructuredErrCLI,
432-
isSubmit bool,
433-
baseURLDefault string,
434-
ch chan tea.Msg,
435-
index int,
436-
) {
437-
for _, test := range req.Tests {
438-
ch <- messages.StartTestMsg{Text: prettyPrintHTTPTest(test, result.Variables)}
439-
}
440-
441-
for j := range req.Tests {
442-
if !isSubmit {
443-
ch <- messages.ResolveTestMsg{Index: j}
444-
} else if failure != nil && (failure.FailedStepIndex < index || (failure.FailedStepIndex == index && failure.FailedTestIndex < j)) {
445-
ch <- messages.ResolveTestMsg{Index: j}
446-
} else {
447-
ch <- messages.ResolveTestMsg{Index: j, Passed: pointerToBool(failure == nil || !(failure.FailedStepIndex == index && failure.FailedTestIndex == j))}
448-
}
449-
}
450-
451-
if !isSubmit {
452-
ch <- messages.ResolveStepMsg{
453-
Index: index,
454-
Result: &api.CLIStepResult{
455-
HTTPRequestResult: &result,
456-
},
457-
}
458-
} else if failure != nil && failure.FailedStepIndex < index {
459-
ch <- messages.ResolveStepMsg{Index: index}
460-
} else {
461-
passed := failure == nil || failure.FailedStepIndex != index
462-
if passed {
463-
ch <- messages.ResolveStepMsg{
464-
Index: index,
465-
Passed: pointerToBool(passed),
466-
}
467-
} else {
468-
ch <- messages.ResolveStepMsg{
469-
Index: index,
470-
Passed: pointerToBool(passed),
471-
Result: &api.CLIStepResult{
472-
HTTPRequestResult: &result,
473-
},
474-
}
475-
}
476-
}
477-
}
478-
479-
func prettyPrintHTTPTest(test api.HTTPRequestTest, variables map[string]string) string {
480-
if test.StatusCode != nil {
481-
return fmt.Sprintf("Expecting status code: %d", *test.StatusCode)
482-
}
483-
if test.BodyContains != nil {
484-
interpolated := checks.InterpolateVariables(*test.BodyContains, variables)
485-
return fmt.Sprintf("Expecting body to contain: %s", interpolated)
486-
}
487-
if test.BodyContainsNone != nil {
488-
interpolated := checks.InterpolateVariables(*test.BodyContainsNone, variables)
489-
return fmt.Sprintf("Expecting JSON body to not contain: %s", interpolated)
490-
}
491-
if test.HeadersContain != nil {
492-
interpolatedKey := checks.InterpolateVariables(test.HeadersContain.Key, variables)
493-
interpolatedValue := checks.InterpolateVariables(test.HeadersContain.Value, variables)
494-
return fmt.Sprintf("Expecting headers to contain: '%s: %v'", interpolatedKey, interpolatedValue)
495-
}
496-
if test.TrailersContain != nil {
497-
interpolatedKey := checks.InterpolateVariables(test.TrailersContain.Key, variables)
498-
interpolatedValue := checks.InterpolateVariables(test.TrailersContain.Value, variables)
499-
return fmt.Sprintf("Expecting trailers to contain: '%s: %v'", interpolatedKey, interpolatedValue)
500-
}
501-
if test.JSONValue != nil {
502-
var val any
503-
var op any
504-
if test.JSONValue.IntValue != nil {
505-
val = *test.JSONValue.IntValue
506-
} else if test.JSONValue.StringValue != nil {
507-
val = *test.JSONValue.StringValue
508-
} else if test.JSONValue.BoolValue != nil {
509-
val = *test.JSONValue.BoolValue
510-
}
511-
if test.JSONValue.Operator == api.OpEquals {
512-
op = "to be equal to"
513-
} else if test.JSONValue.Operator == api.OpGreaterThan {
514-
op = "to be greater than"
515-
} else if test.JSONValue.Operator == api.OpContains {
516-
op = "contains"
517-
} else if test.JSONValue.Operator == api.OpNotContains {
518-
op = "to not contain"
519-
}
520-
expecting := fmt.Sprintf("Expecting JSON at %v %s %v", test.JSONValue.Path, op, val)
521-
return checks.InterpolateVariables(expecting, variables)
522-
}
523-
return ""
524-
}

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.20.5
1+
v1.20.6

0 commit comments

Comments
 (0)