Skip to content

Commit 131ac9a

Browse files
committed
fix tests
1 parent c3bf70a commit 131ac9a

2 files changed

Lines changed: 8 additions & 145 deletions

File tree

cmd/src/login_test.go

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99
"net/http/httptest"
1010
"net/url"
11-
"os"
1211
"strings"
1312
"testing"
1413
"time"
@@ -26,69 +25,6 @@ func mustParseURL(t *testing.T, raw string) *url.URL {
2625
return u
2726
}
2827

29-
func loginCommand(t *testing.T) *command {
30-
t.Helper()
31-
for _, cmd := range commands {
32-
if cmd.matches("login") {
33-
return cmd
34-
}
35-
}
36-
t.Fatal("login command not found")
37-
return nil
38-
}
39-
40-
func captureProcessOutput(t *testing.T, fn func() error) (stdout string, stderr string, err error) {
41-
t.Helper()
42-
43-
stdoutR, stdoutW, err := os.Pipe()
44-
if err != nil {
45-
t.Fatal(err)
46-
}
47-
stderrR, stderrW, err := os.Pipe()
48-
if err != nil {
49-
_ = stdoutR.Close()
50-
_ = stdoutW.Close()
51-
t.Fatal(err)
52-
}
53-
54-
oldStdout := os.Stdout
55-
oldStderr := os.Stderr
56-
os.Stdout = stdoutW
57-
os.Stderr = stderrW
58-
defer func() {
59-
os.Stdout = oldStdout
60-
os.Stderr = oldStderr
61-
}()
62-
63-
err = fn()
64-
65-
_ = stdoutW.Close()
66-
_ = stderrW.Close()
67-
68-
stdoutBytes, readErr := io.ReadAll(stdoutR)
69-
if readErr != nil {
70-
t.Fatal(readErr)
71-
}
72-
stderrBytes, readErr := io.ReadAll(stderrR)
73-
if readErr != nil {
74-
t.Fatal(readErr)
75-
}
76-
77-
return strings.TrimSpace(string(stdoutBytes)), strings.TrimSpace(string(stderrBytes)), err
78-
}
79-
80-
func runLoginHandler(t *testing.T, cfgValue *config, args ...string) (stdout string, stderr string, err error) {
81-
t.Helper()
82-
83-
oldCfg := cfg
84-
cfg = cfgValue
85-
t.Cleanup(func() { cfg = oldCfg })
86-
87-
return captureProcessOutput(t, func() error {
88-
return loginCommand(t).handler(args)
89-
})
90-
}
91-
9228
func TestLogin(t *testing.T) {
9329
check := func(t *testing.T, cfg *config) (output string, err error) {
9430
t.Helper()
@@ -203,87 +139,6 @@ func TestLogin(t *testing.T) {
203139
})
204140
}
205141

206-
func TestLoginHandler(t *testing.T) {
207-
t.Run("warns when login endpoint differs from configured endpoint", func(t *testing.T) {
208-
t.Setenv("SRC_ENDPOINT", "https://example.com")
209-
210-
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
211-
fmt.Fprintln(w, `{"data":{"currentUser":{"username":"alice"}}}`)
212-
}))
213-
defer s.Close()
214-
215-
stdout, stderr, err := runLoginHandler(t, &config{
216-
endpointURL: mustParseURL(t, "https://example.com"),
217-
accessToken: "x",
218-
}, s.URL)
219-
if err != nil {
220-
t.Fatal(err)
221-
}
222-
if !strings.Contains(stderr, "Warning: Logging into "+s.URL+" instead of the configured endpoint https://example.com.") {
223-
t.Fatalf("stderr = %q, want endpoint warning", stderr)
224-
}
225-
if !strings.Contains(stderr, "export SRC_ENDPOINT="+s.URL) {
226-
t.Fatalf("stderr = %q, want shell tip", stderr)
227-
}
228-
if !strings.Contains(stdout, "✔︎ Authenticated as alice on "+s.URL) {
229-
t.Fatalf("stdout = %q, want validation output", stdout)
230-
}
231-
})
232-
233-
t.Run("warns when no SRC_ENDPOINT is configured in the environment", func(t *testing.T) {
234-
if oldValue, ok := os.LookupEnv("SRC_ENDPOINT"); ok {
235-
_ = os.Unsetenv("SRC_ENDPOINT")
236-
t.Cleanup(func() { _ = os.Setenv("SRC_ENDPOINT", oldValue) })
237-
} else {
238-
_ = os.Unsetenv("SRC_ENDPOINT")
239-
}
240-
241-
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
242-
fmt.Fprintln(w, `{"data":{"currentUser":{"username":"alice"}}}`)
243-
}))
244-
defer s.Close()
245-
246-
stdout, stderr, err := runLoginHandler(t, &config{
247-
endpointURL: mustParseURL(t, SGDotComEndpoint),
248-
accessToken: "x",
249-
}, s.URL)
250-
if err != nil {
251-
t.Fatal(err)
252-
}
253-
if !strings.Contains(stderr, "Warning: No SRC_ENDPOINT is configured in the environment. Logging in using \""+s.URL+"\".") {
254-
t.Fatalf("stderr = %q, want default-endpoint warning", stderr)
255-
}
256-
if !strings.Contains(stderr, "NOTE: By default src will use \""+SGDotComEndpoint+"\" if SRC_ENDPOINT is not set.") {
257-
t.Fatalf("stderr = %q, want default endpoint note", stderr)
258-
}
259-
if !strings.Contains(stdout, "✔︎ Authenticated as alice on "+s.URL) {
260-
t.Fatalf("stdout = %q, want validation output", stdout)
261-
}
262-
})
263-
264-
t.Run("warns when using config file", func(t *testing.T) {
265-
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
266-
fmt.Fprintln(w, `{"data":{"currentUser":{"username":"alice"}}}`)
267-
}))
268-
defer s.Close()
269-
270-
stdout, stderr, err := runLoginHandler(t, &config{
271-
endpointURL: mustParseURL(t, s.URL),
272-
accessToken: "x",
273-
configFilePath: "f",
274-
})
275-
if err != nil {
276-
t.Fatal(err)
277-
}
278-
if !strings.Contains(stderr, "Configuring src with a JSON file is deprecated") {
279-
t.Fatalf("stderr = %q, want deprecation warning", stderr)
280-
}
281-
if !strings.Contains(stdout, "✔︎ Authenticated as alice on "+s.URL) {
282-
t.Fatalf("stdout = %q, want validation output", stdout)
283-
}
284-
})
285-
}
286-
287142
type fakeOAuthClient struct {
288143
startErr error
289144
startCalled *bool

cmd/src/main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestReadConfig(t *testing.T) {
5151
Scheme: "https",
5252
Host: "sourcegraph.com",
5353
},
54+
usingDefaultEndpoint: true,
5455
additionalHeaders: map[string]string{},
5556
},
5657
},
@@ -149,6 +150,7 @@ func TestReadConfig(t *testing.T) {
149150
Scheme: "https",
150151
Host: "sourcegraph.com",
151152
},
153+
usingDefaultEndpoint: true,
152154
accessToken: "abc",
153155
additionalHeaders: map[string]string{},
154156
},
@@ -173,6 +175,7 @@ func TestReadConfig(t *testing.T) {
173175
Scheme: "https",
174176
Host: "sourcegraph.com",
175177
},
178+
usingDefaultEndpoint: true,
176179
accessToken: "",
177180
proxyPath: "",
178181
proxyURL: &url.URL{
@@ -209,6 +212,7 @@ func TestReadConfig(t *testing.T) {
209212
Scheme: "https",
210213
Host: "sourcegraph.com",
211214
},
215+
usingDefaultEndpoint: true,
212216
proxyPath: socketPath,
213217
proxyURL: nil,
214218
additionalHeaders: map[string]string{},
@@ -222,6 +226,7 @@ func TestReadConfig(t *testing.T) {
222226
Scheme: "https",
223227
Host: "sourcegraph.com",
224228
},
229+
usingDefaultEndpoint: true,
225230
proxyPath: socketPath,
226231
proxyURL: nil,
227232
additionalHeaders: map[string]string{},
@@ -235,6 +240,7 @@ func TestReadConfig(t *testing.T) {
235240
Scheme: "https",
236241
Host: "sourcegraph.com",
237242
},
243+
usingDefaultEndpoint: true,
238244
proxyPath: "",
239245
proxyURL: &url.URL{
240246
Scheme: "socks5",
@@ -251,6 +257,7 @@ func TestReadConfig(t *testing.T) {
251257
Scheme: "https",
252258
Host: "sourcegraph.com",
253259
},
260+
usingDefaultEndpoint: true,
254261
proxyPath: "",
255262
proxyURL: &url.URL{
256263
Scheme: "socks5h",
@@ -332,6 +339,7 @@ func TestReadConfig(t *testing.T) {
332339
envCI: "1",
333340
want: &config{
334341
endpointURL: &url.URL{Scheme: "https", Host: "sourcegraph.com"},
342+
usingDefaultEndpoint: true,
335343
additionalHeaders: map[string]string{},
336344
inCI: true,
337345
},

0 commit comments

Comments
 (0)