Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display file:line definition location of failed and skipped tests.
We already display file:line info corresponding with the stack of test failures, but in cases where the failure happened in setup or teardown we don't have any entry in the stack that points back to the test definition. This commit fixes #11. We have never been able to show file:line info corresponding to skipped tests. This commit fixes #15. The reason we want to output file:line info is because the GoLand IDE lacks full support for gunit but what it does do is parse file:line log entries in the test runner console as links for quick navigation back to the source code. So, we are taking advantage of a nice built-in feature to make up for others that are missing. See #12.
- Loading branch information
1 parent
7e2d9ec
commit 8e7b5a5
Showing
13 changed files
with
555 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package scan | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
) | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
func scanForFixtures(code string) ([]*fixtureInfo, error) { | ||
fileset := token.NewFileSet() | ||
file, err := parser.ParseFile(fileset, "", code, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// ast.Print(fileset, file) // helps with debugging... | ||
return findAndListFixtures(file) | ||
} | ||
|
||
func findAndListFixtures(file *ast.File) ([]*fixtureInfo, error) { | ||
collection := newFixtureCollector().Collect(file) | ||
collection = newFixtureMethodFinder(collection).Find(file) | ||
return listFixtures(collection) | ||
} | ||
|
||
func listFixtures(collection map[string]*fixtureInfo) ([]*fixtureInfo, error) { | ||
var fixtures []*fixtureInfo | ||
errorMessage := new(bytes.Buffer) | ||
|
||
for _, fixture := range collection { | ||
fixtures = append(fixtures, fixture) | ||
} | ||
if errorMessage.Len() > 0 { | ||
return nil, errors.New(errorMessage.String()) | ||
} | ||
return fixtures, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package scan | ||
|
||
import "go/ast" | ||
|
||
type fixtureCollector struct { | ||
candidates map[string]*fixtureInfo | ||
fixtures map[string]*fixtureInfo | ||
} | ||
|
||
func newFixtureCollector() *fixtureCollector { | ||
return &fixtureCollector{ | ||
candidates: make(map[string]*fixtureInfo), | ||
fixtures: make(map[string]*fixtureInfo), | ||
} | ||
} | ||
|
||
func (this *fixtureCollector) Collect(file *ast.File) map[string]*fixtureInfo { | ||
ast.Walk(this, file) // Calls this.Visit(...) recursively which populates this.fixtures | ||
return this.fixtures | ||
} | ||
|
||
func (this *fixtureCollector) Visit(node ast.Node) ast.Visitor { | ||
switch t := node.(type) { | ||
case *ast.TypeSpec: | ||
name := t.Name.Name | ||
this.candidates[name] = &fixtureInfo{StructName: name} | ||
return &fixtureValidator{Parent: this, FixtureName: name} | ||
default: | ||
return this | ||
} | ||
} | ||
|
||
func (this *fixtureCollector) Validate(fixture string) { | ||
this.fixtures[fixture] = this.candidates[fixture] | ||
delete(this.candidates, fixture) | ||
} | ||
|
||
func (this *fixtureCollector) Invalidate(fixture string) { | ||
this.Validate(fixture) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package scan | ||
|
||
import "go/ast" | ||
|
||
type fixtureValidator struct { | ||
Parent *fixtureCollector | ||
FixtureName string | ||
} | ||
|
||
func (this *fixtureValidator) Visit(node ast.Node) ast.Visitor { | ||
// We start at a TypeSpec and look for an embedded pointer field: `*gunit.Fixture`. | ||
field, isField := node.(*ast.Field) | ||
if !isField { | ||
return this | ||
} | ||
pointer, isPointer := field.Type.(*ast.StarExpr) | ||
if !isPointer { | ||
return this | ||
} | ||
|
||
selector, isSelector := pointer.X.(*ast.SelectorExpr) | ||
if !isSelector { | ||
return this | ||
} | ||
gunit, isGunit := selector.X.(*ast.Ident) | ||
if selector.Sel.Name != "Fixture" || !isGunit || gunit.Name != "gunit" { | ||
return this | ||
} | ||
this.Parent.Validate(this.FixtureName) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package scan | ||
|
||
import ( | ||
"go/ast" | ||
"strings" | ||
) | ||
|
||
type fixtureMethodFinder struct { | ||
fixtures map[string]*fixtureInfo | ||
} | ||
|
||
func newFixtureMethodFinder(fixtures map[string]*fixtureInfo) *fixtureMethodFinder { | ||
return &fixtureMethodFinder{fixtures: fixtures} | ||
} | ||
|
||
func (this *fixtureMethodFinder) Find(file *ast.File) map[string]*fixtureInfo { | ||
ast.Walk(this, file) // Calls this.Visit(...) recursively. | ||
return this.fixtures | ||
} | ||
|
||
func (this *fixtureMethodFinder) Visit(node ast.Node) ast.Visitor { | ||
function, isFunction := node.(*ast.FuncDecl) | ||
if !isFunction { | ||
return this | ||
} | ||
|
||
if function.Recv.NumFields() == 0 { | ||
return nil | ||
} | ||
|
||
receiver, isPointer := function.Recv.List[0].Type.(*ast.StarExpr) | ||
if !isPointer { | ||
return this | ||
} | ||
|
||
fixtureName := receiver.X.(*ast.Ident).Name | ||
fixture, functionMatchesFixture := this.fixtures[fixtureName] | ||
if !functionMatchesFixture { | ||
return nil | ||
} | ||
|
||
if !isExportedAndVoidAndNiladic(function) { | ||
return this | ||
} | ||
|
||
this.attach(function, fixture) | ||
return nil | ||
} | ||
|
||
func isExportedAndVoidAndNiladic(function *ast.FuncDecl) bool { | ||
if isExported := function.Name.IsExported(); !isExported { | ||
return false | ||
} | ||
if isNiladic := function.Type.Params.NumFields() == 0; !isNiladic { | ||
return false | ||
} | ||
isVoid := function.Type.Results.NumFields() == 0 | ||
return isVoid | ||
} | ||
|
||
func (this *fixtureMethodFinder) attach(function *ast.FuncDecl, fixture *fixtureInfo) { | ||
if IsTestCase(function.Name.Name) { | ||
fixture.TestCases = append(fixture.TestCases, &testCaseInfo{ | ||
CharacterPosition: int(function.Pos()), | ||
Name: function.Name.Name, | ||
}) | ||
} | ||
} | ||
|
||
func IsTestCase(name string) bool { | ||
return strings.HasPrefix(name, "Test") || | ||
strings.HasPrefix(name, "LongTest") || | ||
strings.HasPrefix(name, "FocusTest") || | ||
strings.HasPrefix(name, "FocusLongTest") || | ||
strings.HasPrefix(name, "SkipTest") || | ||
strings.HasPrefix(name, "SkipLongTest") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package scan | ||
|
||
type fixtureInfo struct { | ||
Filename string | ||
StructName string | ||
TestCases []*testCaseInfo | ||
} | ||
|
||
type testCaseInfo struct { | ||
CharacterPosition int | ||
LineNumber int | ||
Name string | ||
} |
Oops, something went wrong.