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

chore: skip authenticated tests if not in CI #283

Merged
merged 1 commit into from
Mar 11, 2021
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
21 changes: 20 additions & 1 deletion internal/cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/envs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func Test_envs_ls(t *testing.T) {
skipIfNoAuth(t)
res := execute(t, nil, "envs", "ls")
res.success(t)

Expand Down
1 change: 1 addition & 0 deletions internal/cmd/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

func Test_providers_ls(t *testing.T) {
skipIfNoAuth(t)
res := execute(t, nil, "providers", "ls")
res.success(t)
}
53 changes: 31 additions & 22 deletions pkg/clog/clog_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package clog

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

"cdr.dev/slog/sloggers/slogtest/assert"
Expand All @@ -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))
Expand All @@ -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,
Expand Down