Skip to content

Commit 0e51634

Browse files
authored
no penalty on fail (#152)
1 parent 99bfebc commit 0e51634

File tree

7 files changed

+53
-37
lines changed

7 files changed

+53
-37
lines changed

checks/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func sendHTTPRequestResults(ch chan tea.Msg, req api.CLIStepHTTPRequest, result
111111
}
112112
}
113113

114-
func ApplySubmissionResults(cliData api.CLIData, failure *api.VerificationResultStructuredErrCLI, ch chan tea.Msg) {
114+
func ApplySubmissionResults(cliData api.CLIData, failure *api.StructuredErrCLI, ch chan tea.Msg) {
115115
for i, step := range cliData.Steps {
116116
stepPass := true
117117
isFailedStep := false

client/lessons.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ type Lesson struct {
1414
}
1515

1616
type LessonDataCLI struct {
17-
// Readme string
17+
// Readme string
1818
CLIData CLIData
1919
}
2020

2121
const BaseURLOverrideRequired = "override"
2222

2323
type CLIData struct {
24-
// ContainsCompleteDir bool
24+
// ContainsCompleteDir bool
25+
// NoPenaltyOnFail bool
2526
BaseURLDefault string
2627
Steps []CLIStep
2728
AllowedOperatingSystems []string
@@ -189,41 +190,48 @@ type lessonSubmissionCLI struct {
189190
CLIResults []CLIStepResult
190191
}
191192

192-
type verificationResult struct {
193-
ResultSlug string
194-
// user friendly message to put in the toast
195-
ResultMessage string
196-
// only present if the lesson is an CLI type
197-
StructuredErrCLI *VerificationResultStructuredErrCLI
193+
type LessonSubmissionEvent struct {
194+
ResultSlug VerificationResultSlug
195+
StructuredErrCLI *StructuredErrCLI
198196
}
199197

200-
type VerificationResultStructuredErrCLI struct {
198+
type StructuredErrCLI struct {
201199
ErrorMessage string `json:"Error"`
202200
FailedStepIndex int `json:"FailedStepIndex"`
203201
FailedTestIndex int `json:"FailedTestIndex"`
204202
}
205203

206-
func SubmitCLILesson(uuid string, results []CLIStepResult) (*VerificationResultStructuredErrCLI, error) {
204+
type VerificationResultSlug string
205+
206+
const (
207+
// "noop" is for "noPenaltyOnFail" on the CLI type
208+
VerificationResultSlugNoop VerificationResultSlug = "noop"
209+
VerificationResultSlugSystemError VerificationResultSlug = "system-error"
210+
VerificationResultSlugSuccess VerificationResultSlug = "success"
211+
VerificationResultSlugFailure VerificationResultSlug = "failure"
212+
)
213+
214+
func SubmitCLILesson(uuid string, results []CLIStepResult) (LessonSubmissionEvent, error) {
207215
bytes, err := json.Marshal(lessonSubmissionCLI{CLIResults: results})
208216
if err != nil {
209-
return nil, err
217+
return LessonSubmissionEvent{}, err
210218
}
211219
endpoint := fmt.Sprintf("/v1/lessons/%v/", uuid)
212220
resp, code, err := fetchWithAuthAndPayload("POST", endpoint, bytes)
213221
if err != nil {
214-
return nil, err
222+
return LessonSubmissionEvent{}, err
215223
}
216224
if code == 402 {
217-
return nil, fmt.Errorf("to run and submit the tests for this lesson, you must have an active Boot.dev membership\nhttps://boot.dev/pricing")
225+
return LessonSubmissionEvent{}, fmt.Errorf("to run and submit the tests for this lesson, you must have an active Boot.dev membership\nhttps://boot.dev/pricing")
218226
}
219227
if code != 200 {
220-
return nil, fmt.Errorf("failed to submit CLI lesson (code %v): %s", code, string(resp))
228+
return LessonSubmissionEvent{}, fmt.Errorf("failed to submit CLI lesson (code %v): %s", code, string(resp))
221229
}
222230

223-
result := verificationResult{}
231+
result := LessonSubmissionEvent{}
224232
err = json.Unmarshal(resp, &result)
225233
if err != nil {
226-
return nil, err
234+
return LessonSubmissionEvent{}, err
227235
}
228-
return result.StructuredErrCLI, nil
236+
return result, nil
229237
}

cmd/submit.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,17 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
6868
// StartRenderer and returns immediately, finalise function blocks the execution until the renderer is closed.
6969
finalise := render.StartRenderer(data, isSubmit, ch)
7070

71-
results := checks.CLIChecks(data, overrideBaseURL, ch)
71+
cliResults := checks.CLIChecks(data, overrideBaseURL, ch)
7272

7373
if isSubmit {
74-
failure, err := api.SubmitCLILesson(lessonUUID, results)
74+
submissionEvent, err := api.SubmitCLILesson(lessonUUID, cliResults)
7575
if err != nil {
7676
return err
7777
}
78-
checks.ApplySubmissionResults(data, failure, ch)
79-
80-
finalise(failure)
78+
checks.ApplySubmissionResults(data, submissionEvent.StructuredErrCLI, ch)
79+
finalise(submissionEvent)
8180
} else {
82-
finalise(nil)
81+
finalise(api.LessonSubmissionEvent{})
8382
}
8483
return nil
8584
}

messages/messages.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type ResolveTestMsg struct {
2121
}
2222

2323
type DoneStepMsg struct {
24-
Failure *api.VerificationResultStructuredErrCLI
24+
Result api.VerificationResultSlug
25+
Failure *api.StructuredErrCLI
2526
}
2627

2728
type ResolveStepMsg struct {

render/models.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type stepModel struct {
2424
type rootModel struct {
2525
steps []stepModel
2626
spinner spinner.Model
27-
failure *api.VerificationResultStructuredErrCLI
27+
result api.VerificationResultSlug
28+
failure *api.StructuredErrCLI
2829
isSubmit bool
29-
success bool
3030
finalized bool
3131
clear bool
3232
}

render/render.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ func (m rootModel) Init() tea.Cmd {
3030
func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
3131
switch msg := msg.(type) {
3232
case messages.DoneStepMsg:
33+
m.result = msg.Result
3334
m.failure = msg.Failure
34-
if m.failure == nil && m.isSubmit {
35-
m.success = true
36-
}
3735
m.clear = true
3836
return m, tea.Quit
3937

@@ -93,7 +91,7 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
9391
}
9492
}
9593

96-
func StartRenderer(data api.CLIData, isSubmit bool, ch chan tea.Msg) func(*api.VerificationResultStructuredErrCLI) {
94+
func StartRenderer(data api.CLIData, isSubmit bool, ch chan tea.Msg) func(api.LessonSubmissionEvent) {
9795
var wg sync.WaitGroup
9896
p := tea.NewProgram(initModel(isSubmit), tea.WithoutSignalHandler())
9997

@@ -117,8 +115,11 @@ func StartRenderer(data api.CLIData, isSubmit bool, ch chan tea.Msg) func(*api.V
117115
}
118116
}()
119117

120-
return func(failure *api.VerificationResultStructuredErrCLI) {
121-
ch <- messages.DoneStepMsg{Failure: failure}
118+
return func(submissionEvent api.LessonSubmissionEvent) {
119+
ch <- messages.DoneStepMsg{
120+
Result: submissionEvent.ResultSlug,
121+
Failure: submissionEvent.StructuredErrCLI,
122+
}
122123
wg.Wait()
123124
}
124125
}

render/view.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,20 @@ func (m rootModel) View() string {
161161
str.WriteString(printHTTPRequestResult(*step.result.HTTPRequestResult))
162162
}
163163
}
164-
if m.failure != nil {
164+
165+
if m.result == api.VerificationResultSlugSuccess && m.isSubmit {
166+
str.WriteString("\n\n" + green.Render("All tests passed! 🎉") + "\n\n")
167+
str.WriteString(green.Render("Return to your browser to continue with the next lesson.") + "\n\n")
168+
} else if m.result == api.VerificationResultSlugNoop {
169+
str.WriteString("\n\nTests failed! ❌")
170+
str.WriteString(fmt.Sprintf("\n\nFailed Step: %v", m.failure.FailedStepIndex+1))
171+
str.WriteString("\nError: " + m.failure.ErrorMessage + "\n")
172+
str.WriteString("\nYou haven't passed, but you also haven't been penalized.\n\n")
173+
} else if m.result == api.VerificationResultSlugFailure {
165174
str.WriteString("\n\n" + red.Render("Tests failed! ❌"))
166175
str.WriteString(red.Render(fmt.Sprintf("\n\nFailed Step: %v", m.failure.FailedStepIndex+1)))
167176
str.WriteString(red.Render("\nError: "+m.failure.ErrorMessage) + "\n\n")
168-
} else if m.success {
169-
str.WriteString("\n\n" + green.Render("All tests passed! 🎉") + "\n\n")
170-
str.WriteString(green.Render("Return to your browser to continue with the next lesson.") + "\n\n")
171177
}
178+
172179
return str.String()
173180
}

0 commit comments

Comments
 (0)