Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Add unit tests for clog#167

Merged
cmoog merged 4 commits intomasterfromtests
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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
2 changes: 1 addition & 1 deletion.github/workflows/test.yaml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,7 +38,7 @@ jobs:
- name: test
uses: ./ci/image
with:
args:go test -v -cover -covermode=count ./internal/... ./cmd/...
args:./ci/steps/unit_test.sh
gendocs:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletionsci/image/Dockerfile
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ ENV CI=true

RUN go get golang.org/x/tools/cmd/goimports
RUN go get github.com/mattn/goveralls
RUN apt update && apt install grep
9 changes: 9 additions & 0 deletionsci/steps/unit_test.sh
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

echo "--- go test..."

go test $(go list ./... | grep -v pkg/tcli | grep -v ci/integration)
File renamed without changes.
76 changes: 76 additions & 0 deletionspkg/clog/clog_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
package clog

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

"cdr.dev/slog/sloggers/slogtest/assert"
"golang.org/x/xerrors"
)

func TestError(t *testing.T) {
t.Run("oneline", func(t *testing.T) {
var mockErr error = Error("fake error")
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
os.Stderr = writer

Log(mockErr)
writer.Close()

output, err := ioutil.ReadAll(reader)
assert.Success(t, "read all stderr output", err)

assert.Equal(t, "output is as expected", "error: fake error\n\n", string(output))
})

t.Run("plain-error", func(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
os.Stderr = writer

Log(mockErr)
writer.Close()

output, err := ioutil.ReadAll(reader)
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("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
os.Stderr = writer

Log(mockErr)
writer.Close()

output, err := ioutil.ReadAll(reader)
assert.Success(t, "read all stderr output", err)

assert.Equal(t,
"output is as expected",
"error: fake header\n | next line\n | \n | tip: content of fake tip\n\n",
string(output),
)
})
}

[8]ページ先頭

©2009-2025 Movatter.jp