Skip to content

Commit

Permalink
use NewStringData
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Nov 2, 2024
1 parent 97b4244 commit b1e83ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
24 changes: 11 additions & 13 deletions internal/app/table_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func (c *consoleWriter) summaryTable(
skip: "Skip",
}
tbl.Headers(header.toRow()...)

var rows []summaryRow
data := table.NewStringData()

// Capture as separate slices because notests are optional when passed tests are available.
// The only exception is if passed=0 and notests=1, then we display them regardless. This
Expand All @@ -82,7 +81,7 @@ func (c *consoleWriter) summaryTable(
packageName: packageName,
cover: "--", pass: "--", fail: "--", skip: "--",
}
rows = append(rows, row)
data.Append(row.toRow())
continue
}
if pkg.HasFailedBuildOrSetup {
Expand All @@ -92,7 +91,7 @@ func (c *consoleWriter) summaryTable(
packageName: packageName + "\n[" + pkg.Summary.Output + "]",
cover: "--", pass: "--", fail: "--", skip: "--",
}
rows = append(rows, row)
data.Append(row.toRow())
continue
}
if pkg.NoTestFiles {
Expand Down Expand Up @@ -189,25 +188,24 @@ func (c *consoleWriter) summaryTable(
passed = append(passed, row)
}

if len(rows) == 0 && len(passed) == 0 && len(notests) == 0 {
if data.Rows() == 0 && len(passed) == 0 && len(notests) == 0 {
return
}
rows = append(rows, passed...)
for _, r := range passed {
data.Append(r.toRow())
}

// Only display the "no tests to run" cases if users want to see them when passed
// tests are available.
// An exception is made if there are no passed tests and only a single no test files
// package. This is almost always because the user forgot to match one or more packages.
if showNoTests || (len(passed) == 0 && len(notests) == 1) {
rows = append(rows, notests...)
}
// The table gets written to a strings builder so we can further modify the output
// with lipgloss.
for _, r := range rows {
tbl.Rows(r.toRow())
for _, r := range notests {
data.Append(r.toRow())
}
}

fmt.Fprintln(c.w, tbl.Render())
fmt.Fprintln(c.w, tbl.Data(data).Render())
}

type summaryRow struct {
Expand Down
25 changes: 9 additions & 16 deletions internal/app/table_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func (c *consoleWriter) testsTable(packages []*parse.Package, option TestTableOp
packageName: "Package",
}
tbl.Headers(header.toRow()...)

var rows []testRow
data := table.NewStringData()

names := make([]string, 0, len(packages))
for _, pkg := range packages {
Expand Down Expand Up @@ -106,19 +105,16 @@ func (c *consoleWriter) testsTable(packages []*parse.Package, option TestTableOp
testName: testName,
packageName: packageName,
}
rows = append(rows, row)
data.Append(row.toRow())
}
if i != (len(packages) - 1) {
// Add a blank row between packages.
rows = append(rows, testRow{})
data.Append(testRow{}.toRow())
}
}

for _, r := range rows {
tbl.Rows(r.toRow())
}
if len(rows) > 0 {
fmt.Fprintln(c.w, tbl.Render())
if data.Rows() > 0 {
fmt.Fprintln(c.w, tbl.Data(data).Render())
}
}

Expand All @@ -142,7 +138,7 @@ func (c *consoleWriter) testsTableMarkdown(packages []*parse.Package, option Tes
"Test",
}
tbl.Headers(header...)
var rows [][]string
data := table.NewStringData()

// Discard packages where we cannot generate a sensible test summary.
if pkg.NoTestFiles || pkg.NoTests || pkg.HasPanic {
Expand All @@ -168,16 +164,13 @@ func (c *consoleWriter) testsTableMarkdown(packages []*parse.Package, option Tes
case parse.ActionFail:
status = c.red(status)
}
rows = append(rows, []string{
data.Append([]string{
status,
strconv.FormatFloat(t.Elapsed(), 'f', 2, 64),
testName,
})
}
for _, r := range rows {
tbl.Rows(r)
}
if len(rows) > 0 {
if data.Rows() > 0 {
fmt.Fprintf(c.w, "## 📦 Package **`%s`**\n", pkg.Summary.Package)
fmt.Fprintln(c.w)
fmt.Fprintf(c.w,
Expand All @@ -192,7 +185,7 @@ func (c *consoleWriter) testsTableMarkdown(packages []*parse.Package, option Tes
fmt.Fprintln(c.w)
fmt.Fprintln(c.w, "<summary>Click for test summary</summary>")
fmt.Fprintln(c.w)
fmt.Fprintln(c.w, tbl.Render())
fmt.Fprintln(c.w, tbl.Data(data).Render())
fmt.Fprintln(c.w, "</details>")
fmt.Fprintln(c.w)
}
Expand Down

0 comments on commit b1e83ed

Please sign in to comment.