diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 81c7a10d..fd942730 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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: diff --git a/ci/image/Dockerfile b/ci/image/Dockerfile index 8eb77371..f814866d 100644 --- a/ci/image/Dockerfile +++ b/ci/image/Dockerfile @@ -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 diff --git a/ci/steps/unit_test.sh b/ci/steps/unit_test.sh new file mode 100755 index 00000000..9f7c9e13 --- /dev/null +++ b/ci/steps/unit_test.sh @@ -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) diff --git a/pkg/clog/error.go b/pkg/clog/clog.go similarity index 100% rename from pkg/clog/error.go rename to pkg/clog/clog.go diff --git a/pkg/clog/clog_test.go b/pkg/clog/clog_test.go new file mode 100644 index 00000000..8d1c88b4 --- /dev/null +++ b/pkg/clog/clog_test.go @@ -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), + ) + }) +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy