From 8d2a375ebc05d64b261ca70b13edd893435b9d8a Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Wed, 10 Jul 2024 21:41:35 +0200 Subject: [PATCH] limit the default test documentation ouput to failures This should help reduce the clutter and make easy to pin-point all the failures in bigger testing suites --- internal/doc/doc.go | 30 ++++++++++++++++++++++++++---- internal/doc/doc_test.go | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/internal/doc/doc.go b/internal/doc/doc.go index 767106d..06cfa92 100644 --- a/internal/doc/doc.go +++ b/internal/doc/doc.go @@ -81,6 +81,12 @@ func (n *node) cd(path []string) *node { } func (gen DocumentFormat) hasFailed(n *node) bool { + if n == nil { + return false + } + if n.TestingCase.TestFailed { + return true + } for _, child := range n.Nodes { if child.TestingCase.TestFailed { return true @@ -92,10 +98,21 @@ func (gen DocumentFormat) hasFailed(n *node) bool { return false } +func (gen DocumentFormat) hasFailedInSubnodes(n *node) bool { + if n == nil { + return false + } + for _, child := range n.Nodes { + if gen.hasFailed(child) { + return true + } + } + return false +} + func (gen DocumentFormat) generateDocumentString(n *node, indent string) string { var sb strings.Builder for key, child := range n.Nodes { - sb.WriteString(indent) var ( line = key colour = green @@ -111,9 +128,14 @@ func (gen DocumentFormat) generateDocumentString(n *node, indent string) string if len(child.Nodes) == 0 { line = colourise(colour, line) } - sb.WriteString(line) - sb.WriteString("\n") - sb.WriteString(gen.generateDocumentString(child, indent+" ")) + if internal.Verbose() || gen.hasFailed(child) { + sb.WriteString(indent) + sb.WriteString(line) + sb.WriteString("\n") + } + if internal.Verbose() || gen.hasFailedInSubnodes(child) { + sb.WriteString(gen.generateDocumentString(child, indent+" ")) + } } return sb.String() } diff --git a/internal/doc/doc_test.go b/internal/doc/doc_test.go index d6bd740..9e463fc 100644 --- a/internal/doc/doc_test.go +++ b/internal/doc/doc_test.go @@ -63,6 +63,9 @@ func TestTestDocumentGenerator(t *testing.T) { t.Run("many - colourless", func(t *testing.T) { testcase.SetEnv(t, "TERM", "dumb") + internal.StubVerbose(t, func() bool { + return true + }) docw := doc.DocumentFormat{} @@ -99,6 +102,9 @@ func TestTestDocumentGenerator(t *testing.T) { t.Run("many - colourised", func(t *testing.T) { testcase.SetEnv(t, "TERM", "xterm-256color") + internal.StubVerbose(t, func() bool { + return true + }) docw := doc.DocumentFormat{} @@ -131,7 +137,7 @@ func TestTestDocumentGenerator(t *testing.T) { }) }) - t.Run("skipped tests are greyed out", func(t *testing.T) { + t.Run("skipped tests are yellow", func(t *testing.T) { testcase.SetEnv(t, "TERM", "xterm-256color") internal.StubVerbose(t, func() bool { return true @@ -155,6 +161,38 @@ func TestTestDocumentGenerator(t *testing.T) { exp := "TestTestDocumentGenerator\n smoke\n \x1b[93mtestA [SKIP]\x1b[0m\n" assert.Contain(t, d, exp) }) + + t.Run("non verbose - documentation limited to errors", func(t *testing.T) { + testcase.SetEnv(t, "TERM", "dumb") + internal.StubVerbose(t, func() bool { + return false + }) + + docw := doc.DocumentFormat{} + + d, err := docw.MakeDocument(context.Background(), []doc.TestingCase{ + { + ContextPath: []string{ + "TestTestDocumentGenerator", + "smoke", + "testA", + }, + TestFailed: false, + }, + { + ContextPath: []string{ + "TestTestDocumentGenerator", + "smoke", + "testB", + }, + TestFailed: true, + }, + }) + assert.NoError(t, err) + + exp := "TestTestDocumentGenerator\n smoke\n testB [FAIL]\n" + assert.Contain(t, d, exp) + }) } func Test_spike(t *testing.T) {