Skip to content

Commit f061ffd

Browse files
expose baseURL to cli commands
1 parent 20fc7e5 commit f061ffd

2 files changed

Lines changed: 94 additions & 8 deletions

File tree

checks/runner.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import (
99
"github.com/bootdotdev/bootdev/messages"
1010
tea "github.com/charmbracelet/bubbletea"
1111
"github.com/spf13/cobra"
12-
"github.com/spf13/viper"
1312
)
1413

1514
func CLIChecks(cliData api.CLIData, overrideBaseURL string, ch chan tea.Msg) (results []api.CLIStepResult) {
1615
client := &http.Client{}
17-
variables := make(map[string]string)
1816
results = make([]api.CLIStepResult, len(cliData.Steps))
1917

2018
if cliData.BaseURLDefault == api.BaseURLOverrideRequired && overrideBaseURL == "" {
@@ -25,6 +23,11 @@ func CLIChecks(cliData api.CLIData, overrideBaseURL string, ch chan tea.Msg) (re
2523
if overrideBaseURL == "" {
2624
baseURL = cliData.BaseURLDefault
2725
}
26+
baseURL = strings.TrimSuffix(baseURL, "/")
27+
variables := make(map[string]string)
28+
if baseURL != "" {
29+
variables["baseURL"] = baseURL
30+
}
2831

2932
for i, step := range cliData.Steps {
3033
// This is the magic of the initial message sent before executing the test
@@ -35,12 +38,7 @@ func CLIChecks(cliData api.CLIData, overrideBaseURL string, ch chan tea.Msg) (re
3538
NoPenaltyOnFail: step.NoPenaltyOnFail,
3639
}
3740
} else if step.HTTPRequest != nil {
38-
finalBaseURL := baseURL
39-
overrideURL := viper.GetString("override_base_url")
40-
if overrideURL != "" {
41-
finalBaseURL = overrideURL
42-
}
43-
fullURL := strings.Replace(step.HTTPRequest.Request.FullURL, api.BaseURLPlaceholder, finalBaseURL, 1)
41+
fullURL := strings.Replace(step.HTTPRequest.Request.FullURL, api.BaseURLPlaceholder, baseURL, 1)
4442
interpolatedURL := InterpolateVariables(fullURL, variables)
4543

4644
ch <- messages.StartStepMsg{

checks/runner_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,101 @@
11
package checks
22

33
import (
4+
"net/http"
5+
"net/http/httptest"
46
"testing"
57

68
api "github.com/bootdotdev/bootdev/client"
79
"github.com/bootdotdev/bootdev/messages"
810
tea "github.com/charmbracelet/bubbletea"
11+
"github.com/spf13/viper"
912
)
1013

14+
func TestCLIChecksInterpolatesResolvedBaseURLInCommands(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
defaultBaseURL string
18+
overrideBaseURL string
19+
want string
20+
}{
21+
{
22+
name: "manifest default",
23+
defaultBaseURL: "http://localhost:3000",
24+
want: "http://localhost:3000",
25+
},
26+
{
27+
name: "manifest default with trailing slash",
28+
defaultBaseURL: "http://localhost:3000/",
29+
want: "http://localhost:3000",
30+
},
31+
{
32+
name: "configured override",
33+
defaultBaseURL: "http://localhost:3000",
34+
overrideBaseURL: "http://localhost:4000/",
35+
want: "http://localhost:4000",
36+
},
37+
}
38+
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
cliData := api.CLIData{
42+
BaseURLDefault: tt.defaultBaseURL,
43+
Steps: []api.CLIStep{{
44+
CLICommand: &api.CLIStepCLICommand{
45+
Command: `echo '${baseURL}'`,
46+
},
47+
}},
48+
}
49+
ch := make(chan tea.Msg, 10)
50+
51+
results := CLIChecks(cliData, tt.overrideBaseURL, ch)
52+
53+
if got := results[0].CLICommandResult.Stdout; got != tt.want {
54+
t.Fatalf("command stdout = %q, want %q", got, tt.want)
55+
}
56+
})
57+
}
58+
}
59+
60+
func TestCLIChecksUsesOverrideParameterForHTTPRequestPreview(t *testing.T) {
61+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
62+
w.WriteHeader(http.StatusNoContent)
63+
}))
64+
defer server.Close()
65+
66+
previousOverride := viper.GetString("override_base_url")
67+
viper.Set("override_base_url", "http://localhost:1")
68+
t.Cleanup(func() {
69+
viper.Set("override_base_url", previousOverride)
70+
})
71+
72+
cliData := api.CLIData{
73+
BaseURLDefault: "http://localhost:3000",
74+
Steps: []api.CLIStep{{
75+
HTTPRequest: &api.CLIStepHTTPRequest{
76+
Request: api.HTTPRequest{
77+
Method: http.MethodGet,
78+
FullURL: api.BaseURLPlaceholder + "/health",
79+
},
80+
},
81+
}},
82+
}
83+
messageChannel := make(chan tea.Msg, 10)
84+
85+
results := CLIChecks(cliData, server.URL+"/", messageChannel)
86+
87+
startMessage, ok := (<-messageChannel).(messages.StartStepMsg)
88+
if !ok {
89+
t.Fatal("expected start step message")
90+
}
91+
if want := server.URL + "/health"; startMessage.URL != want {
92+
t.Fatalf("preview URL = %q, want %q", startMessage.URL, want)
93+
}
94+
if got := results[0].HTTPRequestResult.StatusCode; got != http.StatusNoContent {
95+
t.Fatalf("response status = %d, want %d", got, http.StatusNoContent)
96+
}
97+
}
98+
1199
func TestApplySubmissionResultsMarksAllStepsAndTestsPassedWhenNoFailure(t *testing.T) {
12100
cliData := api.CLIData{Steps: []api.CLIStep{
13101
{CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{{}, {}}}},

0 commit comments

Comments
 (0)