Skip to content

Commit

Permalink
format/dots: print one package per line
Browse files Browse the repository at this point in the history
Replace the simple implementation with something a little fancier.
  • Loading branch information
dnephin committed Dec 11, 2019
1 parent 06322b4 commit aaff263
Show file tree
Hide file tree
Showing 19 changed files with 984 additions and 67 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ workflows:
git config --global core.autocrlf false
git config --global core.symlinks true
- go/lint
- go/lint:
golangci-lint-version: 1.21.0

- build
- run

Expand Down
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ linters-settings:
min-len: 2
min-occurrences: 4
lll:
line-length: 100
line-length: 120
gocritic:
enabled-tags:
- diagnostic
Expand Down Expand Up @@ -37,3 +37,6 @@ linters:
- gosec
- scopelint
- maligned
- godox
- wsl
- funlen
1 change: 0 additions & 1 deletion flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func TestNoSummaryValue_SetAndString(t *testing.T) {
value := newNoSummaryValue()
assert.NilError(t, value.Set("output"))
assert.Equal(t, value.String(), "output")

})
t.Run("some", func(t *testing.T) {
value := newNoSummaryValue()
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ require (
github.com/google/go-cmp v0.2.0
github.com/jonboulle/clockwork v0.1.0
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect
github.com/mattn/go-isatty v0.0.3
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.4.3 // indirect
github.com/pkg/errors v0.8.0
github.com/sirupsen/logrus v1.0.5
github.com/spf13/pflag v1.0.1
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20180426230345-b49d69b5da94 // indirect
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc // indirect
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
gotest.tools v2.1.0+incompatible
)

go 1.11
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20180426230345-b49d69b5da94 h1:m5xBqfQdnzv6XuV/pJizrLOwUoGzyn1J249cA0cKL4o=
golang.org/x/crypto v0.0.0-20180426230345-b49d69b5da94/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc h1:ZMCWScCvS2fUVFw8LOpxyUUW5qiviqr4Dg5NdjLeiLU=
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/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=
gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=
Expand Down
13 changes: 5 additions & 8 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

type eventHandler struct {
formatter testjson.EventFormatter
out io.Writer
err io.Writer
jsonFile io.WriteCloser
}
Expand All @@ -30,12 +29,11 @@ func (h *eventHandler) Event(event testjson.TestEvent, execution *testjson.Execu
}
}

line, err := h.formatter(event, execution)
err := h.formatter.Format(event, execution)
if err != nil {
return errors.Wrap(err, "failed to format event")
}
_, err = h.out.Write([]byte(line))
return errors.Wrap(err, "failed to write event")
return nil
}

func (h *eventHandler) Close() error {
Expand All @@ -49,15 +47,14 @@ func (h *eventHandler) Close() error {

var _ testjson.EventHandler = &eventHandler{}

func newEventHandler(opts *options, wout io.Writer, werr io.Writer) (*eventHandler, error) {
formatter := testjson.NewEventFormatter(opts.format)
func newEventHandler(opts *options, stdout io.Writer, stderr io.Writer) (*eventHandler, error) {
formatter := testjson.NewEventFormatter(stdout, opts.format)
if formatter == nil {
return nil, errors.Errorf("unknown format %s", opts.format)
}
handler := &eventHandler{
formatter: formatter,
out: wout,
err: werr,
err: stderr,
}
var err error
if opts.jsonFile != "" {
Expand Down
22 changes: 22 additions & 0 deletions internal/dotwriter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License
===========

Copyright (c) 2015, Greg Osuri

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions internal/dotwriter/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This package contains a striped down and modified version of
https://github.com/gosuri/uilive. The original package did not work with
terminal colors, and had some bits that were unnecessary for gotestsum.
42 changes: 42 additions & 0 deletions internal/dotwriter/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*Package dotwriter implements a buffered Writer for updating progress on the
terminal.
*/
package dotwriter

import (
"bytes"
"io"
)

// ESC is the ASCII code for escape character
const ESC = 27

// Writer buffers writes until Flush is called. Flush clears previously written
// lines before writing new lines from the buffer.
type Writer struct {
out io.Writer
buf bytes.Buffer
lineCount int
}

// New returns a new Writer
func New(out io.Writer) *Writer {
return &Writer{out: out}
}

// Flush the buffer, writing all buffered lines to out
func (w *Writer) Flush() error {
if w.buf.Len() == 0 {
return nil
}
w.clearLines()
w.lineCount = bytes.Count(w.buf.Bytes(), []byte{'\n'})
_, err := w.out.Write(w.buf.Bytes())
w.buf.Reset()
return err
}

// Write saves buf to a buffer
func (w *Writer) Write(buf []byte) (int, error) {
return w.buf.Write(buf)
}
15 changes: 15 additions & 0 deletions internal/dotwriter/writer_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build !windows

package dotwriter

import (
"fmt"
"strings"
)

// clear the line and move the cursor up
var clear = fmt.Sprintf("%c[%dA%c[2K", ESC, 1, ESC)

func (w *Writer) clearLines() {
_, _ = fmt.Fprint(w.out, strings.Repeat(clear, w.lineCount))
}
81 changes: 81 additions & 0 deletions internal/dotwriter/writer_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// +build windows

package dotwriter

import (
"fmt"
"io"
"strings"
"syscall"
"unsafe"

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

var kernel32 = syscall.NewLazyDLL("kernel32.dll")

var (
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
)

// clear the line and move the cursor up
var clear = fmt.Sprintf("%c[%dA%c[2K\r", ESC, 0, ESC)

type short int16
type dword uint32
type word uint16

type coord struct {
x short
y short
}

type smallRect struct {
left short
top short
right short
bottom short
}

type consoleScreenBufferInfo struct {
size coord
cursorPosition coord
attributes word
window smallRect
maximumWindowSize coord
}

type fdWriter interface {
io.Writer
Fd() uintptr
}

func (w *Writer) clearLines() {
f, ok := w.out.(fdWriter)
if ok && !isatty.IsTerminal(f.Fd()) {
ok = false
}
if !ok {
_, _ = fmt.Fprint(w.out, strings.Repeat(clear, w.lineCount))
return
}
fd := f.Fd()
var csbi consoleScreenBufferInfo
_, _, _ = procGetConsoleScreenBufferInfo.Call(fd, uintptr(unsafe.Pointer(&csbi)))

for i := 0; i < w.lineCount; i++ {
// move the cursor up
csbi.cursorPosition.y--
_, _, _ = procSetConsoleCursorPosition.Call(fd, uintptr(*(*int32)(unsafe.Pointer(&csbi.cursorPosition))))
// clear the line
cursor := coord{
x: csbi.window.left,
y: csbi.window.top + csbi.cursorPosition.y,
}
var count, w dword
count = dword(csbi.size.x)
_, _, _ = procFillConsoleOutputCharacter.Call(fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&w)))
}
}
3 changes: 3 additions & 0 deletions testjson/doc.go
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/*Package testjson scans test2json output and builds up a summary of the events.
Events are passed to a formatter for output.
*/
package testjson // import "gotest.tools/gotestsum/testjson"
Loading

0 comments on commit aaff263

Please sign in to comment.