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
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,25 @@ For lessons with HTTP tests, you can configure the CLI with a base URL that over

### CLI colors

The CLI text output is rendered with extra colors: green (e.g., success messages), red (e.g., error messages), and gray (e.g., secondary text).
CLI colors are configured by their role.

- To customize these colors, run:

```sh
bootdev config colors --red VALUE --green VALUE --gray VALUE --magenta VALUE --yellow VALUE
bootdev config colors \
--header VALUE \
--subheader VALUE \
--information VALUE \
--warning VALUE \
--pass VALUE \
--fail VALUE \
--secondary VALUE \
--accent VALUE
```

_You can use an [ANSI color code](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) or a hex string as the `VALUE`._
_You can use an [ANSI palette color index](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) (`0`–`255`, such as `6` for cyan) or a hex string (such as `#7AA2F7`) as the `VALUE`. Palette indexes `0`–`15` follow your terminal's theme._

- Alternatively, edit the `color` section in your active configuration file (such as `~/.bootdev.yaml`). Omitted keys use the default colors.

- To get the current colors, run:

Expand Down
13 changes: 9 additions & 4 deletions checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func runHTTPRequest(
captured := make(map[string]string)
defer func() {
result.Err = publishCaptures(captureNames(api.CLIStep{HTTPRequest: &requestStep}), variables, captured, result.Err)
if result.Err != "" && result.ErrorCategory == "" {
result.ErrorCategory = api.HTTPErrorResponse
}
result.Variables = maps.Clone(variables)
result.Request = requestStep
}()
Expand All @@ -49,7 +52,7 @@ func runHTTPRequest(
bodyJSON := interpolateJSONStrings(requestStep.Request.BodyJSON, variables)
dat, err := json.Marshal(bodyJSON)
if err != nil {
return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to marshal request body: %s", err)}
return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to marshal request body: %s", err), ErrorCategory: api.HTTPErrorRequest}
}
requestBody = bytes.NewReader(dat)
contentType = "application/json"
Expand All @@ -66,7 +69,7 @@ func runHTTPRequest(

req, err := http.NewRequest(requestStep.Request.Method, completeURL, requestBody)
if err != nil {
return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to create request: %s", err)}
return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to create request: %s", err), ErrorCategory: api.HTTPErrorRequest}
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
Expand All @@ -92,14 +95,14 @@ func runHTTPRequest(
resp, err := requestClient.Do(req)
if err != nil {
errString := fmt.Sprintf("Failed to fetch: %s", err.Error())
result = api.HTTPRequestResult{Err: errString}
result = api.HTTPRequestResult{Err: errString, ErrorCategory: api.HTTPErrorExchange}
return result
}
defer resp.Body.Close()

body, err := io.ReadAll(io.LimitReader(resp.Body, maxHTTPResponseBodyBytes+1))
if err != nil {
result = api.HTTPRequestResult{Err: "Failed to read response body"}
result = api.HTTPRequestResult{Err: fmt.Sprintf("Failed to read response body: %s", err), ErrorCategory: api.HTTPErrorResponse}
return result
}

Expand All @@ -122,10 +125,12 @@ func runHTTPRequest(
}
if err := parseVariables([]byte(bodyString), requestStep.ResponseVariables, captured); err != nil {
result.Err = fmt.Sprintf("Failed to parse response variable: %s", err)
result.ErrorCategory = api.HTTPErrorResponse
return result
}
if err := parseHeaderVariables(headers, requestStep.ResponseHeaderVariables, captured); err != nil {
result.Err = fmt.Sprintf("Failed to parse response header variable: %s", err)
result.ErrorCategory = api.HTTPErrorResponse
}

return result
Expand Down
33 changes: 33 additions & 0 deletions checks/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,39 @@ func TestRunHTTPRequestReportsInvalidRequest(t *testing.T) {
if !strings.Contains(result.Err, "Failed to create request") {
t.Fatalf("runHTTPRequest() error = %q, want request creation error", result.Err)
}
if result.ErrorCategory != api.HTTPErrorRequest {
t.Fatalf("unexpected error category: %s", result.ErrorCategory)
}
}

func TestRunHTTPRequestCategorizesResponseErrors(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/incomplete" {
w.Header().Set("Content-Length", "100")
}
_, _ = w.Write([]byte(`{}`))
}))
defer server.Close()
for _, tt := range []struct {
name string
path string
variables []api.HTTPRequestResponseVariable
want api.HTTPErrorCategory
}{
{name: "incomplete response", path: "/incomplete", want: api.HTTPErrorResponse},
{name: "invalid extraction", path: "/", variables: []api.HTTPRequestResponseVariable{{Name: "value", Path: ".["}}, want: api.HTTPErrorResponse},
{name: "missing capture", path: "/", variables: []api.HTTPRequestResponseVariable{{Name: "value", Path: ".missing"}}, want: api.HTTPErrorResponse},
} {
t.Run(tt.name, func(t *testing.T) {
result := runHTTPRequest(server.Client(), server.URL, map[string]string{}, api.CLIStepHTTPRequest{
Request: api.HTTPRequest{Method: http.MethodGet, FullURL: server.URL + tt.path},
ResponseVariables: tt.variables,
})
if result.Err == "" || result.ErrorCategory != tt.want {
t.Fatalf("category = %q, want %q; error = %q", result.ErrorCategory, tt.want, result.Err)
}
})
}
}

func TestTruncateAndStringifyBodyCapsBinaryBody(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion checks/jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func compareInt(actual int, expected int, operator api.JqOperator) bool {
}
}

func valFromJqPath(path string, jsn string) (any, error) {
func JSONValueAtPath(path string, jsn string) (any, error) {
vals, err := valsFromJqPath(path, jsn)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion checks/jq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestRunStdoutJqQuery(t *testing.T) {
}

func TestValFromJqPathRejectsMultipleValues(t *testing.T) {
_, err := valFromJqPath(`.items[].id`, `{"items":[{"id":1},{"id":2}]}`)
_, err := JSONValueAtPath(`.items[].id`, `{"items":[{"id":1},{"id":2}]}`)
if err == nil {
t.Fatal("expected error for multiple values")
}
Expand Down
2 changes: 1 addition & 1 deletion checks/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func capturedVariableMatches(vars map[string]string, name, expectedValue string)
}

func jsonValOp(test api.HTTPRequestTestJSONValue, jsn string, variables map[string]string) error {
val, err := valFromJqPath(test.Path, jsn)
val, err := JSONValueAtPath(test.Path, jsn)
if err != nil {
return err
}
Expand Down
10 changes: 9 additions & 1 deletion checks/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ func CLIChecks(cliData api.CLIData, options RunOptions, send func(tea.Msg)) ([]a
delete(variables, name)
}
results[i].DependencyFailure = &api.DependencyFailure{Names: missing}
send(messages.StartStepMsg{Description: step.Description, NoPenaltyOnFail: step.NoPenaltyOnFail})
startMessage := messages.StartStepMsg{Description: step.Description, NoPenaltyOnFail: step.NoPenaltyOnFail}
if step.CLICommand != nil {
startMessage.CMD = step.CLICommand.Command
startMessage.TmdlQuery = step.CLICommand.StdoutFilterTmdl
} else if step.HTTPRequest != nil {
startMessage.Method = step.HTTPRequest.Request.Method
startMessage.URL = step.HTTPRequest.Request.FullURL
}
send(startMessage)
send(messages.ResolveStepMsg{Index: i, Result: &results[i]})
continue
}
Expand Down
45 changes: 45 additions & 0 deletions checks/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,51 @@ func TestCLIChecksReturnsManifestErrors(t *testing.T) {
}
}

func TestSkippedStepsPreserveCommandAndRequestDetails(t *testing.T) {
command := `printf '%s' '${token}'`
requestURL := "${baseURL}/reports/${token}"
query := "report"
cliData := api.CLIData{
BaseURLDefault: "http://localhost:1",
Steps: []api.CLIStep{
{Description: "Skipped command", CLICommand: &api.CLIStepCLICommand{Command: command, StdoutFilterTmdl: &query}},
{Description: "Skipped request", HTTPRequest: &api.CLIStepHTTPRequest{Request: api.HTTPRequest{Method: http.MethodGet, FullURL: requestURL}}},
{CLICommand: &api.CLIStepCLICommand{
Command: "echo 'token=ready'",
StdoutVariables: []api.CLICommandStdoutVariable{{Name: "token", Regex: "token=(.*)"}},
}},
},
}
var starts []messages.StartStepMsg
results, err := CLIChecks(cliData, RunOptions{}, func(msg tea.Msg) {
if start, ok := msg.(messages.StartStepMsg); ok {
starts = append(starts, start)
}
})
if err != nil {
t.Fatal(err)
}
if len(starts) != 3 || len(results) != 3 {
t.Fatalf("expected three step messages and results")
}
for _, index := range []int{0, 1} {
result := results[index]
if result.DependencyFailure == nil || len(result.DependencyFailure.Names) != 1 || result.DependencyFailure.Names[0] != "token" ||
result.CLICommandResult != nil || result.HTTPRequestResult != nil {
t.Fatalf("step %d should be skipped without execution: %+v", index+1, result)
}
}
if starts[0].CMD != command || starts[0].TmdlQuery == nil || *starts[0].TmdlQuery != query || starts[0].Description != "Skipped command" {
t.Fatalf("skipped command lost its details: %+v", starts[0])
}
if starts[1].Method != http.MethodGet || starts[1].URL != requestURL || starts[1].Description != "Skipped request" {
t.Fatalf("skipped request lost its details: %+v", starts[1])
}
if results[2].CLICommandResult == nil || results[2].CLICommandResult.Variables["token"] != "ready" {
t.Fatal("independent later step did not execute")
}
}

func TestApplySubmissionResultsMarksAllStepsAndTestsPassedWhenNoFailure(t *testing.T) {
cliData := api.CLIData{Steps: []api.CLIStep{
{CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{{}, {}}}},
Expand Down
11 changes: 10 additions & 1 deletion client/lessons.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,17 @@ type CLICommandJqOutput struct {
Error string
}

type HTTPErrorCategory string

const (
HTTPErrorRequest HTTPErrorCategory = "request"
HTTPErrorExchange HTTPErrorCategory = "http"
HTTPErrorResponse HTTPErrorCategory = "response"
)

type HTTPRequestResult struct {
Err string `json:"FetchErr,omitempty"`
ErrorCategory HTTPErrorCategory `json:"-"`
Err string `json:"FetchErr,omitempty"`
StatusCode int
ResponseHeaders map[string]string
ResponseTrailers map[string]string
Expand Down
14 changes: 9 additions & 5 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"

"github.com/bootdotdev/bootdev/render"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -18,11 +19,14 @@ var configureCmd = &cobra.Command{
}

var defaultColors = map[string]string{
"gray": "8",
"red": "1",
"green": "2",
"magenta": "5",
"yellow": "#f6bd45",
"secondary": render.DefaultSecondaryColor,
"fail": render.DefaultFailColor,
"pass": render.DefaultPassColor,
"accent": render.DefaultAccentColor,
"warning": render.DefaultWarningColor,
"header": render.DefaultHeaderColor,
"subheader": render.DefaultSubheaderColor,
"information": render.DefaultInformationColor,
}

// configureColorsCmd represents the `configure colors` command for changing
Expand Down
58 changes: 55 additions & 3 deletions cmd/localtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"

"github.com/bootdotdev/bootdev/checks"
api "github.com/bootdotdev/bootdev/client"
Expand All @@ -19,6 +21,7 @@ import (
func init() {
rootCmd.AddCommand(localTestCmd)
localTestCmd.Flags().Bool("ignore-os", false, "skip the lesson's allowed operating systems check")
localTestCmd.Flags().Bool("run", false, "run local lesson commands without grading")
localTestCmd.Flags().String("shell", "", "shell for lesson commands: sh or pwsh (default depends on host OS)")
localTestCmd.Flags().BoolVarP(&verboseOutput, "verbose", "v", false, "show detailed final output for every step")
}
Expand All @@ -33,6 +36,7 @@ var localTestCmd = &cobra.Command{

func localTestHandler(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cmd.SilenceErrors = false

data, err := readLocalCLIData(args[0])
if err != nil {
Expand All @@ -51,14 +55,23 @@ func localTestHandler(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
runOnly, err := cmd.Flags().GetBool("run")
if err != nil {
return err
}

overrideBaseURL := viper.GetString("override_base_url")
runContext := localRunContext(args[0])
if runOnly {
runContext.Action = "Running lesson:"
}
runContext.BaseURL = data.BaseURLDefault
if overrideBaseURL != "" {
fmt.Printf("Using overridden base_url: %v\n", overrideBaseURL)
fmt.Printf("You can reset to the default with `bootdev config base_url --reset`\n\n")
runContext.BaseURL = overrideBaseURL
runContext.BaseURLOverridden = true
}

send, finish := render.StartRenderer(true, verboseOutput, false)
send, finish := render.StartRenderer(!runOnly, verboseOutput, false, runContext)
submissionEvent := api.LessonSubmissionEvent{}
defer func() {
finish(submissionEvent)
Expand All @@ -68,16 +81,55 @@ func localTestHandler(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if runOnly {
return nil
}
submissionEvent = checks.LocalSubmissionEvent(data, cliResults)
checks.ApplySubmissionResults(data, submissionEvent.StructuredErrCLI, send)

if submissionEvent.ResultSlug != api.VerificationResultSlugSuccess {
// The renderer already reports the failure. Suppress Cobra's duplicate
// error message while returning the error to preserve a nonzero exit status.
cmd.SilenceErrors = true
return localTestFailureError(submissionEvent.StructuredErrCLI)
}

return nil
}

func localRunContext(path string) render.RunContext {
manifestPath := filepath.Clean(path)
if absolutePath, err := filepath.Abs(manifestPath); err == nil {
manifestPath = absolutePath
}
if info, err := os.Stat(manifestPath); err == nil && info.IsDir() {
manifestPath = filepath.Join(manifestPath, "cli.yaml")
}
lessonDirectory := filepath.Dir(manifestPath)
context := render.RunContext{
Action: "Submitting lesson:",
LessonTitle: filepath.Base(lessonDirectory),
}
if readme, err := os.ReadFile(filepath.Join(lessonDirectory, "readme.md")); err == nil {
for _, line := range strings.Split(string(readme), "\n") {
if strings.HasPrefix(line, "# ") {
if title := strings.TrimSpace(strings.TrimPrefix(line, "# ")); title != "" {
context.LessonTitle = title
}
break
}
}
}
chapterPrefix, _, chapterFound := strings.Cut(filepath.Base(filepath.Dir(lessonDirectory)), "-")
lessonPrefix, _, lessonFound := strings.Cut(filepath.Base(lessonDirectory), "-")
chapterNumber, chapterErr := strconv.Atoi(chapterPrefix)
lessonNumber, lessonErr := strconv.Atoi(lessonPrefix)
if chapterFound && lessonFound && chapterErr == nil && lessonErr == nil && chapterNumber > 0 && lessonNumber > 0 {
context.LessonTitle = fmt.Sprintf("%d.%d - %s", chapterNumber, lessonNumber, context.LessonTitle)
}
return context
}

func localTestFailureError(failure *api.StructuredErrCLI) error {
if failure == nil {
return errors.New("local checks failed")
Expand Down
Loading
Loading