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

Format go test output with Go rather than bash and jq #3853

Merged
merged 1 commit into from
Mar 22, 2020
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
44 changes: 1 addition & 43 deletions hack/gotest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,6 @@ set -e
# - It recaps the failures at the end
# - It lists the 20 slowest tests

BOLD='\033[1m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
RESET='\033[0m'

LOG=$(mktemp -t tests.json.XXXXXX)
trap "rm -f $LOG" EXIT

echo "go test $@"

# Keep execution simple for users who do not have jq installed
if ! $(command -v jq > /dev/null); then
go test $* | sed ''/FAIL/s//`printf "${RED}FAIL${RESET}"`/''
exit ${PIPESTATUS[0]}
fi

if [[ $@ == *"-v"* ]]; then
JQ_FILTER='select(has("Output") and (.Action=="output")) | .Output'
else
JQ_FILTER='select(has("Output") and (.Action=="output") and (has("Test")|not) and (.Output!="PASS\n") and (.Output!="FAIL\n") and (.Output|startswith("coverage:")|not) and (.Output|contains("[no test files]")|not)) | .Output'
fi

go test -json $* | tee $LOG | jq --unbuffered -j "${JQ_FILTER}" | sed ''/FAIL/s//`printf "${RED}FAIL${RESET}"`/''
RESULT=${PIPESTATUS[0]}

if [ $RESULT != 0 ]; then
MODULE="$(go list -m)"
echo -e "\n${RED}=== Failed Tests ===${RESET}"

FAILED_TESTS=$(cat $LOG | jq -r 'select(.Action=="fail" and has("Test")) | "\(.Package) \(.Test)"')
while IFS= read -r line; do
ID=( $line )
PACKAGE_NAME=${ID[0]}
TRIMMED_PACKAGE_NAME=${PACKAGE_NAME#"$MODULE"}
TEST_NAME=${ID[1]}
echo -e "${BOLD}$TRIMMED_PACKAGE_NAME/$TEST_NAME${RESET}"
JQ_FILTER="select(.Action==\"output\" and has(\"Test\") and .Package==\"$PACKAGE_NAME\" and .Test==\"$TEST_NAME\" and has(\"Output\") and (.Output|startswith(\"=== RUN\")|not)) | \"\(.Output|rtrimstr(\"\\n\"))\""
cat $LOG | jq -r "${JQ_FILTER}"
done <<< "$FAILED_TESTS"
fi

echo -e "\n${YELLOW}=== Slow Tests ===${RESET}"
cat $LOG | jq -rs 'map(select(.Elapsed > 0 and has("Test"))) | sort_by(.Elapsed) | reverse | map("\(.Elapsed)\t\(.Test)")[]' | head -n20

exit $RESULT
go run hack/tests/main.go $@
177 changes: 177 additions & 0 deletions hack/tests/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"bufio"
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"sort"
"strings"
"sync"
)

type LogLine struct {
Action string
Test string
Package string
Output string
Elapsed float32
}

func main() {
if err := goTest(os.Args[1:]); err != nil {
os.Exit(1)
}
}

func goTest(testArgs []string) error {
args := append([]string{"test", "-json"}, testArgs...)
verbose := isVerbose(testArgs)

cmd := exec.CommandContext(context.Background(), "go", args...)
out, err := cmd.StdoutPipe()
if err != nil {
return err
}

failedTests := map[string]bool{}
var failedLogs []LogLine
var allLogs []LogLine

var wc sync.WaitGroup
wc.Add(1)

go func() {
defer wc.Done()

// Print logs while tests are running
scanner := bufio.NewScanner(out)
for i := 0; scanner.Scan(); i++ {
var l LogLine
if err := json.Unmarshal(scanner.Bytes(), &l); err != nil {
log.Panicf("unable to parse line %d: %v", i, err)
}

allLogs = append(allLogs, l)

if l.Action == "output" {
if verbose || (l.Test == "" && l.Output != "PASS\n" && l.Output != "FAIL\n" && !strings.HasPrefix(l.Output, "coverage:") && !strings.Contains(l.Output, "[no test files]")) {
fmt.Print(failInRed(l.Output))
}
}

// Is this an error?
if (l.Action == "fail" || strings.Contains(l.Output, "FAIL")) && l.Test != "" {
if failedTests[l.Package+"/"+l.Test] {
continue
}
failedTests[l.Package+"/"+l.Test] = true

failedLogs = append(failedLogs, l)
}
}

// Print detailed information about failures.
if len(failedLogs) > 0 {
fmt.Println(red("\n=== Failed Tests ==="))

for _, l := range failedLogs {
fmt.Println(bold(trimPackage(l.Package) + "/" + l.Test))

for _, l := range logsForTest(l.Test, l.Package, allLogs) {
if l.Action == "output" && l.Output != "" && !strings.HasPrefix(l.Output, "=== RUN") {
fmt.Print(failInRed(l.Output))
}
}
}
}

// Print top slowest tests.
fmt.Println(yellow("\n=== Slow Tests ==="))
for _, l := range topSlowest(20, allLogs) {
fmt.Printf("%.2fs\t%s\n", l.Elapsed, l.Test)
}
}()

err = cmd.Run()
wc.Wait()
return err
}

func failInRed(msg string) string {
return strings.ReplaceAll(msg, "FAIL", red("FAIL"))
}

func red(msg string) string {
return "\033[0;31m" + msg + "\033[0m"
}

func yellow(msg string) string {
return "\033[0;33m" + msg + "\033[0m"
}

func bold(msg string) string {
return "\033[1m" + msg + "\033[0m"
}

func trimPackage(pkg string) string {
return strings.TrimPrefix(pkg, "github.com/GoogleContainerTools/skaffold")
}

func isVerbose(args []string) bool {
for _, arg := range args {
if arg == "-v" {
return true
}
}

return false
}

func logsForTest(test, pkg string, all []LogLine) []LogLine {
var forTest []LogLine

for _, l := range all {
if l.Package == pkg && l.Test == test {
forTest = append(forTest, l)
}
}

return forTest
}

func topSlowest(max int, all []LogLine) []LogLine {
var top []LogLine

for _, l := range all {
if l.Test != "" && l.Elapsed > 0 {
top = append(top, l)
}
}

sort.Slice(top, func(i, j int) bool { return top[i].Elapsed > top[j].Elapsed })

if len(top) <= max {
return top
}
return top[0:max]
}