Skip to content

Commit

Permalink
limit the default test documentation ouput to failures
Browse files Browse the repository at this point in the history
This should help reduce the clutter
and make easy to pin-point all the failures in bigger testing suites
  • Loading branch information
adamluzsi committed Jul 10, 2024
1 parent 856ca6f commit 8d2a375
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
30 changes: 26 additions & 4 deletions internal/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
}
Expand Down
40 changes: 39 additions & 1 deletion internal/doc/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 8d2a375

Please sign in to comment.