Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a testsource format #154

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/jonboulle/clockwork v0.1.0
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.3
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4
golang.org/x/tools v0.0.0-20201017001424-6003fad69a88
gotest.tools/v3 v3.0.2
)

Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,44 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9 h1:YTzHMGlqJu67/uEo1lBv0n3wBXhXNeUbB1XfN2vmTm0=
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4 h1:1mMox4TgefDwqluYCv677yNXwlfTkija4owZve/jr78=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201017001424-6003fad69a88 h1:ZB1XYzdDo7c/O48jzjMkvIjnC120Z9/CwgDWhePjQdQ=
golang.org/x/tools v0.0.0-20201017001424-6003fad69a88/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
87 changes: 87 additions & 0 deletions internal/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package color

import (
"bufio"
"fmt"
"io"
"os"

"github.com/mattn/go-isatty"
)

var NoColor = os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))

const escape = "\x1b"

type Attribute struct {
background bool
modifier uint8
red, green, blue uint8
code256 uint8
}

func (a Attribute) BG() Attribute {
a.background = true
return a
}

func (a Attribute) Bold() Attribute {
a.modifier = 1
return a
}

func (a Attribute) Underline() Attribute {
a.modifier = 4
return a
}

func RGB(r, g, b uint8) Attribute {
return Attribute{red: r, green: g, blue: b}
}

func Code256(code uint8) Attribute {
return Attribute{code256: code}
}

func Hex(hex uint32) Attribute {
return Attribute{
red: uint8(hex & (255 << 16) >> 16),
green: uint8(hex & (255 << 8) >> 8),
blue: uint8(hex & 255),
}
}

func Unset(w io.Writer) (int, error) {
if NoColor {
return 0, nil
}
return fmt.Fprintf(w, "%s[0m", escape)
}

func Color(a Attribute) func(w io.Writer) (int, error) {
if NoColor {
return nil
}
return func(w io.Writer) (int, error) {
buf := bufio.NewWriter(w)

fmt.Fprint(buf, escape, "[")
if a.modifier != 0 {
fmt.Fprintf(buf, "%d;", a.modifier)
}
if a.background {
buf.WriteString("48;")
} else {
buf.WriteString("38;")
}
switch {
case a.code256 > 0:
fmt.Fprintf(buf, "5;%d", a.code256)
default:
fmt.Fprintf(buf, "2;%d;%d;%d", a.red, a.green, a.blue)
}
buf.WriteString("m")
return buf.Buffered(), buf.Flush()
}
}
87 changes: 87 additions & 0 deletions internal/color/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package color

import (
"fmt"
"os"
"testing"

"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
)

func TestCode256(t *testing.T) {
t.Skip("color palette")
defer Unset(os.Stdout)
NoColor = false

grid256(" ", Attribute.BG)
grid256(" Ao", func(a Attribute) Attribute { return a })
grid256(" Ao", Attribute.Bold)
grid256(" Ao", Attribute.Underline)
t.Fail()
}

func seq(n int) []struct{} {
return make([]struct{}, n)
}

func grid256(cell string, fn func(a Attribute) Attribute) {
for i := range seq(16) {
if i%8 == 0 {
Unset(os.Stdout)
fmt.Println()
}
f := Color(fn(Code256(uint8(i))))
f(os.Stdout)
fmt.Print(cell)
}

for i := range seq(256 - 16) {
if i%24 == 0 {
Unset(os.Stdout)
fmt.Println()
}
f := Color(fn(Code256(uint8(i))))
f(os.Stdout)
fmt.Print(cell)
}
Unset(os.Stdout)
fmt.Println()
}

func TestRGB(t *testing.T) {
t.Skip("color palette")
defer Unset(os.Stdout)
NoColor = false

gridRGB(" ", Attribute.BG)
t.Fail()
}

func gridRGB(cell string, fn func(a Attribute) Attribute) {
step := 63
i := 0
for r := range seq(8) {
for g := range seq(8) {
for b := range seq(8) {
if i%16 == 0 {
Unset(os.Stdout)
fmt.Println()
}

f := Color(fn(RGB(uint8(r*step), uint8(g*step), uint8(b*step))))
f(os.Stdout)
fmt.Print(cell)
i++
}
}
}
Unset(os.Stdout)
fmt.Println()
}

func TestHex(t *testing.T) {
hex := Hex(0xC7773E)
expected := RGB(199, 119, 62)
assert.DeepEqual(t, hex, expected, cmp.AllowUnexported(Attribute{}))
}
9 changes: 6 additions & 3 deletions testjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func standardQuietFormat(event TestEvent, _ *Execution) (string, error) {
}

func testNameFormat(event TestEvent, exec *Execution) (string, error) {
if isPkgFailureOutput(event) {
return event.Output, nil
}

result := colorEvent(event)(strings.ToUpper(string(event.Action)))
formatTest := func() string {
pkgPath := RelativePackagePath(event.Package)
Expand All @@ -50,9 +54,6 @@ func testNameFormat(event TestEvent, exec *Execution) (string, error) {
}

switch {
case isPkgFailureOutput(event):
return event.Output, nil

case event.PackageEvent():
switch event.Action {
case ActionSkip:
Expand Down Expand Up @@ -219,6 +220,8 @@ func NewEventFormatter(out io.Writer, format string) EventFormatter {
return newDotFormatter(out)
case "testname", "short-verbose":
return &formatAdapter{out, testNameFormat}
case "testsource":
return newTestSourceFormatter(out)
case "pkgname", "short":
return &formatAdapter{out, pkgNameFormat}
case "pkgname-and-test-fails", "short-with-failures":
Expand Down
1 change: 1 addition & 0 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type fakeHandler struct {
}

func (s *fakeHandler) Config(t *testing.T) ScanConfig {
t.Helper()
return ScanConfig{
Stdout: bytes.NewReader(golden.Get(t, s.inputName+".out")),
Stderr: bytes.NewReader(golden.Get(t, s.inputName+".err")),
Expand Down
Empty file.
Loading