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

Fix CSV formatting for 'ginkgo outline' #1490

Merged
merged 4 commits into from
Dec 6, 2024
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
30 changes: 15 additions & 15 deletions ginkgo/outline/_testdata/alias_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,NodotFixture,81,670,false,false,false,""
Describe,normal,126,259,false,false,false,""
It,normal,166,255,true,false,false,""
By,normal,201,223,false,false,false,""
By,normal,227,249,false,false,false,""
Context,normal,262,342,false,false,false,""
It,normal,301,338,true,false,false,""
When,normal,345,422,false,false,false,""
It,normal,381,418,true,false,false,""
It,normal,425,461,true,false,false,""
Specify,normal,464,505,true,false,false,""
DescribeTable,normal,508,586,false,false,false,""
Entry,normal,557,582,true,false,false,""
DescribeTable,normal,589,667,false,false,false,""
Entry,normal,638,663,true,false,false,""
Describe,NodotFixture,81,670,false,false,false,
Describe,normal,126,259,false,false,false,
It,normal,166,255,true,false,false,
By,normal,201,223,false,false,false,
By,normal,227,249,false,false,false,
Context,normal,262,342,false,false,false,
It,normal,301,338,true,false,false,
When,normal,345,422,false,false,false,
It,normal,381,418,true,false,false,
It,normal,425,461,true,false,false,
Specify,normal,464,505,true,false,false,
DescribeTable,normal,508,586,false,false,false,
Entry,normal,557,582,true,false,false,
DescribeTable,normal,589,667,false,false,false,
Entry,normal,638,663,true,false,false,
14 changes: 14 additions & 0 deletions ginkgo/outline/_testdata/csv_proper_escaping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example_test

import (
. "github.com/onsi/ginkgo/v2"
)

var _ = Describe("Abnormal\"Fixture", func() {
When("four random words: flown, authenticating, semiweekly, and overproduction", func() {
It("has a dangling double-quote here: \"", func() {
By("step 1")
By("step 2")
})
})
})
6 changes: 6 additions & 0 deletions ginkgo/outline/_testdata/csv_proper_escaping_test.go.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,"Abnormal""Fixture",73,300,false,false,false,
When,"four random words: flown, authenticating, semiweekly, and overproduction",113,297,false,false,false,
It,"has a dangling double-quote here: """,205,293,true,false,false,
By,step 1,260,272,false,false,false,
By,step 2,276,288,false,false,false,
1 change: 1 addition & 0 deletions ginkgo/outline/_testdata/csv_proper_escaping_test.go.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"Describe","text":"Abnormal\"Fixture","start":73,"end":300,"spec":false,"focused":false,"pending":false,"labels":[],"nodes":[{"name":"When","text":"four random words: flown, authenticating, semiweekly, and overproduction","start":113,"end":297,"spec":false,"focused":false,"pending":false,"labels":[],"nodes":[{"name":"It","text":"has a dangling double-quote here: \"","start":205,"end":293,"spec":true,"focused":false,"pending":false,"labels":[],"nodes":[{"name":"By","text":"step 1","start":260,"end":272,"spec":false,"focused":false,"pending":false,"labels":null,"nodes":[]},{"name":"By","text":"step 2","start":276,"end":288,"spec":false,"focused":false,"pending":false,"labels":null,"nodes":[]}]}]}]}]
10 changes: 5 additions & 5 deletions ginkgo/outline/_testdata/dsl_core_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,DslCoreFixture,82,216,false,false,false,""
Describe,dslcore,119,213,false,false,false,""
It,dslcore,150,209,true,false,false,""
By,step 1,176,188,false,false,false,""
By,step 2,192,204,false,false,false,""
Describe,DslCoreFixture,82,216,false,false,false,
Describe,dslcore,119,213,false,false,false,
It,dslcore,150,209,true,false,false,
By,step 1,176,188,false,false,false,
By,step 2,192,204,false,false,false,
30 changes: 15 additions & 15 deletions ginkgo/outline/_testdata/focused_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,unfocused,73,529,false,false,false,""
FDescribe,focused,105,202,false,true,false,""
It,focused,137,198,true,true,false,""
By,focused,163,176,false,true,false,""
By,focused,180,193,false,true,false,""
FContext,focused,205,268,false,true,false,""
It,focused,236,264,true,true,false,""
FWhen,focused,271,331,false,true,false,""
It,focused,299,327,true,true,false,""
FIt,focused,334,362,true,true,false,""
FSpecify,focused,365,398,true,true,false,""
FDescribeTable,focused,401,462,false,true,false,""
Entry,focused,442,458,true,true,false,""
DescribeTable,focused,465,526,false,false,false,""
FEntry,focused,505,522,true,true,false,""
Describe,unfocused,73,529,false,false,false,
FDescribe,focused,105,202,false,true,false,
It,focused,137,198,true,true,false,
By,focused,163,176,false,true,false,
By,focused,180,193,false,true,false,
FContext,focused,205,268,false,true,false,
It,focused,236,264,true,true,false,
FWhen,focused,271,331,false,true,false,
It,focused,299,327,true,true,false,
FIt,focused,334,362,true,true,false,
FSpecify,focused,365,398,true,true,false,
FDescribeTable,focused,401,462,false,true,false,
Entry,focused,442,458,true,true,false,
DescribeTable,focused,465,526,false,false,false,
FEntry,focused,505,522,true,true,false,
26 changes: 13 additions & 13 deletions ginkgo/outline/_testdata/labels_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,NormalFixture,73,571,false,false,false,""
Describe,NormalFixture,73,571,false,false,false,
Describe,normal,109,228,false,false,false,"normal, serial"
It,normal,166,224,true,false,false,""
By,step 1,191,203,false,false,false,""
By,step 2,207,219,false,false,false,""
Context,normal,231,323,false,false,false,""
It,normal,166,224,true,false,false,
By,step 1,191,203,false,false,false,
By,step 2,207,219,false,false,false,
Context,normal,231,323,false,false,false,
It,normal,260,319,true,false,false,"medium, slow"
When,normal,326,383,false,false,false,""
It,normal,352,379,true,false,false,""
It,normal,386,412,true,false,false,""
Specify,normal,415,446,true,false,false,""
DescribeTable,normal,449,507,false,false,false,""
Entry,normal,488,503,true,false,false,""
DescribeTable,normal,510,568,false,false,false,""
Entry,normal,549,564,true,false,false,""
When,normal,326,383,false,false,false,
It,normal,352,379,true,false,false,
It,normal,386,412,true,false,false,
Specify,normal,415,446,true,false,false,
DescribeTable,normal,449,507,false,false,false,
Entry,normal,488,503,true,false,false,
DescribeTable,normal,510,568,false,false,false,
Entry,normal,549,564,true,false,false,
34 changes: 17 additions & 17 deletions ginkgo/outline/_testdata/mixed_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
FDescribe,unfocused,73,584,false,false,false,""
FContext,unfocused,106,205,false,false,false,""
It,unfocused,139,169,true,false,false,""
FIt,focused,172,201,true,true,false,""
Context,unfocused,208,306,false,false,false,""
FIt,focused,240,269,true,true,false,""
It,unfocused,272,302,true,false,false,""
FContext,focused,309,403,false,true,false,""
It,focused,340,368,true,true,false,""
It,focused,371,399,true,true,false,""
PContext,unfocused,406,581,false,false,true,""
FIt,unfocused,439,507,true,false,true,""
By,unfocused,468,483,false,false,true,""
By,unfocused,487,502,false,false,true,""
It,unfocused,510,577,true,false,true,""
By,unfocused,538,553,false,false,true,""
By,unfocused,557,572,false,false,true,""
FDescribe,unfocused,73,584,false,false,false,
FContext,unfocused,106,205,false,false,false,
It,unfocused,139,169,true,false,false,
FIt,focused,172,201,true,true,false,
Context,unfocused,208,306,false,false,false,
FIt,focused,240,269,true,true,false,
It,unfocused,272,302,true,false,false,
FContext,focused,309,403,false,true,false,
It,focused,340,368,true,true,false,
It,focused,371,399,true,true,false,
PContext,unfocused,406,581,false,false,true,
FIt,unfocused,439,507,true,false,true,
By,unfocused,468,483,false,false,true,
By,unfocused,487,502,false,false,true,
It,unfocused,510,577,true,false,true,
By,unfocused,538,553,false,false,true,
By,unfocused,557,572,false,false,true,
28 changes: 14 additions & 14 deletions ginkgo/outline/_testdata/nestedfocused_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
FDescribe,unfocused,73,476,false,false,false,""
FContext,unfocused,106,275,false,false,false,""
It,unfocused,139,206,true,false,false,""
By,unfocused,167,182,false,false,false,""
By,unfocused,186,201,false,false,false,""
FIt,focused,209,271,true,true,false,""
By,focused,236,249,false,true,false,""
By,focused,253,266,false,true,false,""
Context,unfocused,278,376,false,false,false,""
FIt,focused,310,339,true,true,false,""
It,unfocused,342,372,true,false,false,""
FContext,focused,379,473,false,true,false,""
It,focused,410,438,true,true,false,""
It,focused,441,469,true,true,false,""
FDescribe,unfocused,73,476,false,false,false,
FContext,unfocused,106,275,false,false,false,
It,unfocused,139,206,true,false,false,
By,unfocused,167,182,false,false,false,
By,unfocused,186,201,false,false,false,
FIt,focused,209,271,true,true,false,
By,focused,236,249,false,true,false,
By,focused,253,266,false,true,false,
Context,unfocused,278,376,false,false,false,
FIt,focused,310,339,true,true,false,
It,unfocused,342,372,true,false,false,
FContext,focused,379,473,false,true,false,
It,focused,410,438,true,true,false,
It,focused,441,469,true,true,false,
30 changes: 15 additions & 15 deletions ginkgo/outline/_testdata/nodot_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,NodotFixture,71,614,false,false,false,""
Describe,normal,113,233,false,false,false,""
It,normal,150,229,true,false,false,""
By,normal,182,201,false,false,false,""
By,normal,205,224,false,false,false,""
Context,normal,236,310,false,false,false,""
It,normal,272,306,true,false,false,""
When,normal,313,384,false,false,false,""
It,normal,346,380,true,false,false,""
It,normal,387,420,true,false,false,""
Specify,normal,423,461,true,false,false,""
DescribeTable,normal,464,536,false,false,false,""
Entry,normal,510,532,true,false,false,""
DescribeTable,normal,539,611,false,false,false,""
Entry,normal,585,607,true,false,false,""
Describe,NodotFixture,71,614,false,false,false,
Describe,normal,113,233,false,false,false,
It,normal,150,229,true,false,false,
By,normal,182,201,false,false,false,
By,normal,205,224,false,false,false,
Context,normal,236,310,false,false,false,
It,normal,272,306,true,false,false,
When,normal,313,384,false,false,false,
It,normal,346,380,true,false,false,
It,normal,387,420,true,false,false,
Specify,normal,423,461,true,false,false,
DescribeTable,normal,464,536,false,false,false,
Entry,normal,510,532,true,false,false,
DescribeTable,normal,539,611,false,false,false,
Entry,normal,585,607,true,false,false,
30 changes: 15 additions & 15 deletions ginkgo/outline/_testdata/normal_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,NormalFixture,73,512,false,false,false,""
Describe,normal,109,201,false,false,false,""
It,normal,139,197,true,false,false,""
By,step 1,164,176,false,false,false,""
By,step 2,180,192,false,false,false,""
Context,normal,204,264,false,false,false,""
It,normal,233,260,true,false,false,""
When,normal,267,324,false,false,false,""
It,normal,293,320,true,false,false,""
It,normal,327,353,true,false,false,""
Specify,normal,356,387,true,false,false,""
DescribeTable,normal,390,448,false,false,false,""
Entry,normal,429,444,true,false,false,""
DescribeTable,normal,451,509,false,false,false,""
Entry,normal,490,505,true,false,false,""
Describe,NormalFixture,73,512,false,false,false,
Describe,normal,109,201,false,false,false,
It,normal,139,197,true,false,false,
By,step 1,164,176,false,false,false,
By,step 2,180,192,false,false,false,
Context,normal,204,264,false,false,false,
It,normal,233,260,true,false,false,
When,normal,267,324,false,false,false,
It,normal,293,320,true,false,false,
It,normal,327,353,true,false,false,
Specify,normal,356,387,true,false,false,
DescribeTable,normal,390,448,false,false,false,
Entry,normal,429,444,true,false,false,
DescribeTable,normal,451,509,false,false,false,
Entry,normal,490,505,true,false,false,
26 changes: 13 additions & 13 deletions ginkgo/outline/_testdata/pending_decorator_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,NormalFixture,73,591,false,false,false,""
Describe,NormalFixture,73,591,false,false,false,
Describe,normal,109,237,false,false,true,"normal, serial"
It,normal,175,233,true,false,true,""
By,step 1,200,212,false,false,true,""
By,step 2,216,228,false,false,true,""
Context,normal,240,332,false,false,false,""
It,normal,175,233,true,false,true,
By,step 1,200,212,false,false,true,
By,step 2,216,228,false,false,true,
Context,normal,240,332,false,false,false,
It,normal,269,328,true,false,false,"medium, slow"
When,normal,335,392,false,false,false,""
It,normal,361,388,true,false,false,""
It,normal,395,432,true,false,true,""
Specify,normal,435,466,true,false,false,""
DescribeTable,normal,469,527,false,false,false,""
Entry,normal,508,523,true,false,false,""
DescribeTable,normal,530,588,false,false,false,""
Entry,normal,569,584,true,false,false,""
When,normal,335,392,false,false,false,
It,normal,361,388,true,false,false,
It,normal,395,432,true,false,true,
Specify,normal,435,466,true,false,false,
DescribeTable,normal,469,527,false,false,false,
Entry,normal,508,523,true,false,false,
DescribeTable,normal,530,588,false,false,false,
Entry,normal,569,584,true,false,false,
30 changes: 15 additions & 15 deletions ginkgo/outline/_testdata/pending_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,PendingFixture,73,534,false,false,false,""
PDescribe,pending,110,207,false,false,true,""
It,pending,142,203,true,false,true,""
By,pending,168,181,false,false,true,""
By,pending,185,198,false,false,true,""
PContext,pending,210,273,false,false,true,""
It,pending,241,269,true,false,true,""
PWhen,pending,276,336,false,false,true,""
It,pending,304,332,true,false,true,""
PIt,pending,339,367,true,false,true,""
PSpecify,pending,370,403,true,false,true,""
PDescribeTable,pending,406,467,false,false,true,""
Entry,pending,447,463,true,false,true,""
DescribeTable,pending,470,531,false,false,false,""
PEntry,pending,510,527,true,false,true,""
Describe,PendingFixture,73,534,false,false,false,
PDescribe,pending,110,207,false,false,true,
It,pending,142,203,true,false,true,
By,pending,168,181,false,false,true,
By,pending,185,198,false,false,true,
PContext,pending,210,273,false,false,true,
It,pending,241,269,true,false,true,
PWhen,pending,276,336,false,false,true,
It,pending,304,332,true,false,true,
PIt,pending,339,367,true,false,true,
PSpecify,pending,370,403,true,false,true,
PDescribeTable,pending,406,467,false,false,true,
Entry,pending,447,463,true,false,true,
DescribeTable,pending,470,531,false,false,false,
PEntry,pending,510,527,true,false,true,
4 changes: 2 additions & 2 deletions ginkgo/outline/_testdata/position_test.go.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Name,Text,Start,End,Spec,Focused,Pending,Labels
Describe,104,240,104,240,false,false,false,""
It,209,236,209,236,true,false,false,""
Describe,"104,240",104,240,false,false,false,
It,"209,236",209,236,true,false,false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I laughed to discover that ginkgo/outline/_testdata/position_test.go already has text in place which should have forced using a CSV writer. The test in question:

package example_test
import (
. "github.com/onsi/ginkgo/v2"
)
// Describe start=104, end=240
var _ = Describe("104,240", func() {
/*
* block comment
*
*/
// line comment
// It start=209, end=236
It("209,236", func() {
})
})

A test which uses a csv Reader to load each _testdata/*_test.go.csv would have caught this, but it's easy to miss out on writing meta-tests.

In order not to clutter this PR, I have not added such a test to the suite. If the reviewers like, I can add such a commit onto this branch; or provide a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a picture to hopefully illustrate the previous error in this file:

image

Counting the comma-separated fields in each line, you'll see:

Comment Row Fields
Header Name,Text,Start,End,Spec,Focused,Pending,Labels 8
Removed line Describe,104,240,104,240,false,false,false,"" 9
Removed line It,209,236,209,236,true,false,false,"" 9
Added line Describe,"104,240",104,240,false,false,false, 8
Added line It,"209,236",209,236,true,false,false, 8

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha! this is great, thanks. yes please feel free to add that test to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

I wrote the test and saw it pass on the HEAD of my branch. I rebased it to pretend I'd written this meta-test as the first, rather than last, commit on this branch.

Then I just checked out each commit and ran

 belden@laptop github/onsi/ginkgo/ginkgo/outline $ go test .

to see how the test failed. With the meta-test as the first commit, it properly fails due to outline/_testdata/position_test.go.csv having the extra fields, as shewn in the screenshot above.

The test continues to fail in various ways for the subsequent commits; and when we finally get to the HEAD of this branch, the test passes as expected.


I broke the test up into two functions so as not to make the DescribeTable test body be too long. When I initially looked at that file, I immediately saw the super helpful comment about how to extend the test. I wanted to let the next person immediately see that same helpful comment.

26 changes: 23 additions & 3 deletions ginkgo/outline/outline.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package outline

import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"go/ast"
"go/token"
"strconv"
"strings"

"golang.org/x/tools/go/ast/inspector"
Expand Down Expand Up @@ -84,9 +87,11 @@ func (o *outline) String() string {
// StringIndent returns a CSV-formated outline, but every line is indented by
// one 'width' of spaces for every level of nesting.
func (o *outline) StringIndent(width int) string {
var b strings.Builder
var b bytes.Buffer
b.WriteString("Name,Text,Start,End,Spec,Focused,Pending,Labels\n")

csvWriter := csv.NewWriter(&b)

currentIndent := 0
pre := func(n *ginkgoNode) {
b.WriteString(fmt.Sprintf("%*s", currentIndent, ""))
Expand All @@ -96,8 +101,22 @@ func (o *outline) StringIndent(width int) string {
} else {
labels = strings.Join(n.Labels, ", ")
}
//enclosing labels in a double quoted comma separate listed so that when inmported into a CSV app the Labels column has comma separate strings
b.WriteString(fmt.Sprintf("%s,%s,%d,%d,%t,%t,%t,\"%s\"\n", n.Name, n.Text, n.Start, n.End, n.Spec, n.Focused, n.Pending, labels))

row := []string{
n.Name,
n.Text,
strconv.Itoa(n.Start),
strconv.Itoa(n.End),
strconv.FormatBool(n.Spec),
strconv.FormatBool(n.Focused),
strconv.FormatBool(n.Pending),
labels,
}
csvWriter.Write(row)

// Ensure we write to `b' before the next `b.WriteString()', which might be adding indentation
csvWriter.Flush()

currentIndent += width
}
post := func(n *ginkgoNode) {
Expand All @@ -106,5 +125,6 @@ func (o *outline) StringIndent(width int) string {
for _, n := range o.Nodes {
n.Walk(pre, post)
}

return b.String()
}
Loading
Loading