Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 1198810

Browse files
authored
chore: skip authenticated tests if not in CI (#283)
1 parent 497d474 commit 1198810

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

internal/cmd/cli_test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ import (
2121
"cdr.dev/coder-cli/pkg/clog"
2222
)
2323

24+
var (
25+
shouldSkipAuthedTests bool = false
26+
)
27+
28+
func isCI() bool {
29+
_, ok := os.LookupEnv("CI")
30+
return ok
31+
}
32+
33+
func skipIfNoAuth(t *testing.T) {
34+
if shouldSkipAuthedTests {
35+
t.Skip("no authentication provided and not in CI, skipping")
36+
}
37+
}
38+
2439
func init() {
2540
tmpDir, err := ioutil.TempDir("", "coder-cli-config-dir")
2641
if err != nil {
@@ -35,7 +50,11 @@ func init() {
3550
password := os.Getenv("CODER_PASSWORD")
3651
rawURL := os.Getenv("CODER_URL")
3752
if email == "" || password == "" || rawURL == "" {
38-
panic("CODER_EMAIL, CODER_PASSWORD, and CODER_URL are required environment variables")
53+
if isCI() {
54+
panic("when run in CI, CODER_EMAIL, CODER_PASSWORD, and CODER_URL are required environment variables")
55+
}
56+
shouldSkipAuthedTests = true
57+
return
3958
}
4059
u, err := url.Parse(rawURL)
4160
if err != nil {

internal/cmd/envs_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
func Test_envs_ls(t *testing.T) {
10+
skipIfNoAuth(t)
1011
res := execute(t, nil, "envs", "ls")
1112
res.success(t)
1213

internal/cmd/providers_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
func Test_providers_ls(t *testing.T) {
8+
skipIfNoAuth(t)
89
res := execute(t, nil, "providers", "ls")
910
res.success(t)
1011
}

pkg/clog/clog_test.go

+31-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package clog
22

33
import (
4+
"bytes"
45
"fmt"
56
"io/ioutil"
6-
"os"
77
"testing"
88

99
"cdr.dev/slog/sloggers/slogtest/assert"
@@ -16,16 +16,13 @@ func TestError(t *testing.T) {
1616
mockErr = xerrors.Errorf("wrap 1: %w", mockErr)
1717
mockErr = fmt.Errorf("wrap 2: %w", mockErr)
1818

19-
reader, writer, err := os.Pipe()
20-
assert.Success(t, "create pipe", err)
21-
22-
//! clearly not thread safe
23-
SetOutput(writer)
19+
var buf bytes.Buffer
20+
//! clearly not concurrent safe
21+
SetOutput(&buf)
2422

2523
Log(mockErr)
26-
writer.Close()
2724

28-
output, err := ioutil.ReadAll(reader)
25+
output, err := ioutil.ReadAll(&buf)
2926
assert.Success(t, "read all stderr output", err)
3027

3128
assert.Equal(t, "output is as expected", "error: fake error\n\n", string(output))
@@ -35,36 +32,48 @@ func TestError(t *testing.T) {
3532
mockErr := xerrors.Errorf("base error")
3633
mockErr = fmt.Errorf("wrap 1: %w", mockErr)
3734

38-
reader, writer, err := os.Pipe()
39-
assert.Success(t, "create pipe", err)
40-
41-
//! clearly not thread safe
42-
SetOutput(writer)
35+
var buf bytes.Buffer
36+
//! clearly not concurrent safe
37+
SetOutput(&buf)
4338

4439
Log(mockErr)
45-
writer.Close()
4640

47-
output, err := ioutil.ReadAll(reader)
41+
output, err := ioutil.ReadAll(&buf)
4842
assert.Success(t, "read all stderr output", err)
4943

5044
assert.Equal(t, "output is as expected", "fatal: wrap 1: base error\n\n", string(output))
5145
})
5246

47+
t.Run("message", func(t *testing.T) {
48+
for _, f := range []struct {
49+
f func(string, ...string)
50+
level string
51+
}{{LogInfo, "info"}, {LogSuccess, "success"}, {LogWarn, "warning"}} {
52+
var buf bytes.Buffer
53+
//! clearly not concurrent safe
54+
SetOutput(&buf)
55+
56+
f.f("testing", Hintf("maybe do %q", "this"), BlankLine, Causef("what happened was %q", "this"))
57+
58+
output, err := ioutil.ReadAll(&buf)
59+
assert.Success(t, "read all stderr output", err)
60+
61+
assert.Equal(t, "output is as expected", f.level+": testing\n | hint: maybe do \"this\"\n | \n | cause: what happened was \"this\"\n", string(output))
62+
}
63+
})
64+
5365
t.Run("multi-line", func(t *testing.T) {
5466
var mockErr error = Error("fake header", "next line", BlankLine, Tipf("content of fake tip"))
5567
mockErr = xerrors.Errorf("wrap 1: %w", mockErr)
5668
mockErr = fmt.Errorf("wrap 1: %w", mockErr)
5769

58-
reader, writer, err := os.Pipe()
59-
assert.Success(t, "create pipe", err)
60-
61-
//! clearly not thread safe
62-
SetOutput(writer)
70+
var buf bytes.Buffer
71+
//! clearly not concurrent safe
72+
SetOutput(&buf)
6373

6474
Log(mockErr)
65-
writer.Close()
6675

67-
output, err := ioutil.ReadAll(reader)
76+
output, err := ioutil.ReadAll(&buf)
6877
assert.Success(t, "read all stderr output", err)
6978

7079
assert.Equal(t,

0 commit comments

Comments
 (0)