diff --git a/internal/cmd/cli_test.go b/internal/cmd/cli_test.go index d1c639dc..7823d7e3 100644 --- a/internal/cmd/cli_test.go +++ b/internal/cmd/cli_test.go @@ -21,6 +21,21 @@ import ( "cdr.dev/coder-cli/pkg/clog" ) +var ( + shouldSkipAuthedTests bool = false +) + +func isCI() bool { + _, ok := os.LookupEnv("CI") + return ok +} + +func skipIfNoAuth(t *testing.T) { + if shouldSkipAuthedTests { + t.Skip("no authentication provided and not in CI, skipping") + } +} + func init() { tmpDir, err := ioutil.TempDir("", "coder-cli-config-dir") if err != nil { @@ -35,7 +50,11 @@ func init() { password := os.Getenv("CODER_PASSWORD") rawURL := os.Getenv("CODER_URL") if email == "" || password == "" || rawURL == "" { - panic("CODER_EMAIL, CODER_PASSWORD, and CODER_URL are required environment variables") + if isCI() { + panic("when run in CI, CODER_EMAIL, CODER_PASSWORD, and CODER_URL are required environment variables") + } + shouldSkipAuthedTests = true + return } u, err := url.Parse(rawURL) if err != nil { diff --git a/internal/cmd/envs_test.go b/internal/cmd/envs_test.go index c7cc6451..ec672b20 100644 --- a/internal/cmd/envs_test.go +++ b/internal/cmd/envs_test.go @@ -7,6 +7,7 @@ import ( ) func Test_envs_ls(t *testing.T) { + skipIfNoAuth(t) res := execute(t, nil, "envs", "ls") res.success(t) diff --git a/internal/cmd/providers_test.go b/internal/cmd/providers_test.go index 14900759..685e129b 100644 --- a/internal/cmd/providers_test.go +++ b/internal/cmd/providers_test.go @@ -5,6 +5,7 @@ import ( ) func Test_providers_ls(t *testing.T) { + skipIfNoAuth(t) res := execute(t, nil, "providers", "ls") res.success(t) } diff --git a/pkg/clog/clog_test.go b/pkg/clog/clog_test.go index 4c75a3e5..51eab07e 100644 --- a/pkg/clog/clog_test.go +++ b/pkg/clog/clog_test.go @@ -1,9 +1,9 @@ package clog import ( + "bytes" "fmt" "io/ioutil" - "os" "testing" "cdr.dev/slog/sloggers/slogtest/assert" @@ -16,16 +16,13 @@ func TestError(t *testing.T) { mockErr = xerrors.Errorf("wrap 1: %w", mockErr) mockErr = fmt.Errorf("wrap 2: %w", mockErr) - reader, writer, err := os.Pipe() - assert.Success(t, "create pipe", err) - - //! clearly not thread safe - SetOutput(writer) + var buf bytes.Buffer + //! clearly not concurrent safe + SetOutput(&buf) Log(mockErr) - writer.Close() - output, err := ioutil.ReadAll(reader) + output, err := ioutil.ReadAll(&buf) assert.Success(t, "read all stderr output", err) assert.Equal(t, "output is as expected", "error: fake error\n\n", string(output)) @@ -35,36 +32,48 @@ func TestError(t *testing.T) { mockErr := xerrors.Errorf("base error") mockErr = fmt.Errorf("wrap 1: %w", mockErr) - reader, writer, err := os.Pipe() - assert.Success(t, "create pipe", err) - - //! clearly not thread safe - SetOutput(writer) + var buf bytes.Buffer + //! clearly not concurrent safe + SetOutput(&buf) Log(mockErr) - writer.Close() - output, err := ioutil.ReadAll(reader) + output, err := ioutil.ReadAll(&buf) assert.Success(t, "read all stderr output", err) assert.Equal(t, "output is as expected", "fatal: wrap 1: base error\n\n", string(output)) }) + t.Run("message", func(t *testing.T) { + for _, f := range []struct { + f func(string, ...string) + level string + }{{LogInfo, "info"}, {LogSuccess, "success"}, {LogWarn, "warning"}} { + var buf bytes.Buffer + //! clearly not concurrent safe + SetOutput(&buf) + + f.f("testing", Hintf("maybe do %q", "this"), BlankLine, Causef("what happened was %q", "this")) + + output, err := ioutil.ReadAll(&buf) + assert.Success(t, "read all stderr output", err) + + 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)) + } + }) + t.Run("multi-line", func(t *testing.T) { var mockErr error = Error("fake header", "next line", BlankLine, Tipf("content of fake tip")) mockErr = xerrors.Errorf("wrap 1: %w", mockErr) mockErr = fmt.Errorf("wrap 1: %w", mockErr) - reader, writer, err := os.Pipe() - assert.Success(t, "create pipe", err) - - //! clearly not thread safe - SetOutput(writer) + var buf bytes.Buffer + //! clearly not concurrent safe + SetOutput(&buf) Log(mockErr) - writer.Close() - output, err := ioutil.ReadAll(reader) + output, err := ioutil.ReadAll(&buf) assert.Success(t, "read all stderr output", err) assert.Equal(t,