Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Oct 20, 2018
1 parent fc3fa5f commit ecfff5b
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 65 deletions.
11 changes: 6 additions & 5 deletions parse/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,20 @@ const (
)

func (a Action) String() string {
return strings.ToUpper(string(a))
return string(a)
}

func (a Action) WithColor() string {
s := strings.ToUpper(a.String())
switch a {
case ActionPass:
return Green(a.String())
return Green(s)
case ActionSkip:
return Yellow(a.String())
return Yellow(s)
case ActionFail:
return Red(a.String())
return Red(s)
default:
return a.String()
return s
}
}

Expand Down
2 changes: 2 additions & 0 deletions parse/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (p Packages) Print() {
}

// Package is the representation of a single package being tested.
// The summary event contains all relevant information about the package.
type Package struct {
Summary *Event // single summary event describing the result of the package
Tests []*Test
Expand Down Expand Up @@ -109,6 +110,7 @@ func Do(r io.Reader) (Packages, error) {
}

if e.SkipLine() {
pkg.Summary = &Event{Action: ActionPass}
pkg.NoTest = true
}

Expand Down
131 changes: 71 additions & 60 deletions tests/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestStatus(t *testing.T) {

t.Parallel()

filepath.Walk("./testdata", func(path string, info os.FileInfo, err error) error {
filepath.Walk("./testdata/big", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand All @@ -24,78 +24,89 @@ func TestStatus(t *testing.T) {
return nil
}

by, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
actionsByFilename := []struct {
prefix string
parse.Action
}{
{"pass", parse.ActionPass},
// although one or more test(s) are marked as skipped, the test is considered passed.
{"skip", parse.ActionPass},
{"fail", parse.ActionFail},
}

pkgs, err := parse.Do(bytes.NewReader(by))
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
t.Run(info.Name(), func(t *testing.T) {

var want parse.Action

s := info.Name()
switch {
case strings.Contains(s, "package_pass"):
want = parse.ActionPass
case strings.Contains(s, "package_skip"):
// although one or more test(s) are marked as skipped, the test is
// considered passed.
want = parse.ActionPass
case strings.Contains(s, "package_fail"):
want = parse.ActionFail
default:
t.Logf("log: file: %s", info.Name())
t.Fatalf("unexpected action based on filename: %v", info.Name())
}
var want parse.Action
var supported bool

for i := range actionsByFilename {
if strings.HasPrefix(info.Name(), actionsByFilename[i].prefix) {
want = actionsByFilename[i].Action
supported = true
break
}
}

for _, pkg := range pkgs {
if pkg.Summary.Action != want {
t.Logf("log: file: %s", info.Name())
t.Fatalf("failed package summary action: got %q, want %q", pkg.Summary.Action, want)
if !supported {
t.Fatalf("got unsupported filename %q; want testdata/big file name prefixed pass|fail|skip", info.Name())
}

// zero tests [no tests to run] should always yield a pass
if len(pkg.Tests) == 0 && pkg.Summary.Action != parse.ActionPass {
t.Fatalf("zero test should always return pass: got %q, want %q", pkg.Summary.Action, parse.ActionPass)
by, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}

// As a sanity check, we're going to iterate over the tests and make sure
// it reflects the correct package outcome

switch pkg.Summary.Action {
case parse.ActionPass:
// one or more tests must be explicitly marked as either pass or skip
// anything else should result in the test failing
for _, test := range pkg.Tests {
switch test.Status() {
case parse.ActionPass, parse.ActionSkip:
continue
default:
t.Fatalf("all tests, within a package marked passed, should have a status of pass or skip: got %q", test.Status())
}
pkgs, err := parse.Do(bytes.NewReader(by))
if err != nil {
t.Fatalf("failed to parse event: %v", err)
}

for _, pkg := range pkgs {
if pkg.Summary.Action != want {
t.Logf("log: file: %s", info.Name())
t.Fatalf("failed package summary action: got %q, want %q", pkg.Summary.Action, want)
}
case parse.ActionFail:
// one or more tests must be marked as failed, otherwise the test is considered failed
var failed bool
for _, test := range pkg.Tests {
if test.Status() == parse.ActionFail {
failed = true
break
}

// zero tests [no tests to run] should always yield a pass
if len(pkg.Tests) == 0 && pkg.Summary.Action != parse.ActionPass {
t.Fatalf("zero test should always return pass: got %q, want %q", pkg.Summary.Action, parse.ActionPass)
}

if !failed {
t.Fatalf("got no failed tests, want one or more tests to be marked as %q", parse.ActionFail)
// As a sanity check, we're going to iterate over the tests and make sure
// it reflects the correct package outcome

switch pkg.Summary.Action {
case parse.ActionPass:
// one or more tests must be explicitly marked as either pass or skip
// anything else should result in the test failing
for _, test := range pkg.Tests {
switch test.Status() {
case parse.ActionPass, parse.ActionSkip:
continue
default:
t.Fatalf("all tests, within a package marked passed, should have a status of pass or skip: got %q", test.Status())
}
}
case parse.ActionFail:
// one or more tests must be marked as failed, otherwise the test is considered failed
var failed bool
for _, test := range pkg.Tests {
if test.Status() == parse.ActionFail {
failed = true
break
}
}

if !failed {
t.Fatalf("got no failed tests, want one or more tests to be marked as %q", parse.ActionFail)
}
default:
// catch all failure, should never occur
t.Fatalf("got package summary action %q, want pass or fail", pkg.Summary.Action)
}
default:
// catch all failure, should never occur
t.Fatalf("got package summary action %q, want pass or fail", pkg.Summary.Action)
}

}
}
})

return nil
})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/testdata/big/skip03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"Time":"2018-10-20T13:32:27.452736-04:00","Action":"output","Package":"debug/errorcause","Output":"? \tdebug/errorcause\t[no test files]\n"}
{"Time":"2018-10-20T13:32:27.452995-04:00","Action":"skip","Package":"debug/errorcause","Elapsed":0}

0 comments on commit ecfff5b

Please sign in to comment.