diff --git a/parse/event.go b/parse/event.go index 9ecc32f..84c6a82 100644 --- a/parse/event.go +++ b/parse/event.go @@ -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 } } diff --git a/parse/package.go b/parse/package.go index 5b8c37e..d0a8800 100644 --- a/parse/package.go +++ b/parse/package.go @@ -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 @@ -109,6 +110,7 @@ func Do(r io.Reader) (Packages, error) { } if e.SkipLine() { + pkg.Summary = &Event{Action: ActionPass} pkg.NoTest = true } diff --git a/tests/status_test.go b/tests/status_test.go index 80d2b4e..066b8e7 100644 --- a/tests/status_test.go +++ b/tests/status_test.go @@ -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 } @@ -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 }) diff --git a/tests/testdata/package_fail01.json b/tests/testdata/big/fail01.json similarity index 100% rename from tests/testdata/package_fail01.json rename to tests/testdata/big/fail01.json diff --git a/tests/testdata/package_pass01.json b/tests/testdata/big/pass01.json similarity index 100% rename from tests/testdata/package_pass01.json rename to tests/testdata/big/pass01.json diff --git a/tests/testdata/package_pass02.json b/tests/testdata/big/pass02.json similarity index 100% rename from tests/testdata/package_pass02.json rename to tests/testdata/big/pass02.json diff --git a/tests/testdata/package_pass03.json b/tests/testdata/big/pass03.json similarity index 100% rename from tests/testdata/package_pass03.json rename to tests/testdata/big/pass03.json diff --git a/tests/testdata/package_pass04.json b/tests/testdata/big/pass04.json similarity index 100% rename from tests/testdata/package_pass04.json rename to tests/testdata/big/pass04.json diff --git a/tests/testdata/package_skip01.json b/tests/testdata/big/skip01.json similarity index 100% rename from tests/testdata/package_skip01.json rename to tests/testdata/big/skip01.json diff --git a/tests/testdata/package_skip02.json b/tests/testdata/big/skip02.json similarity index 100% rename from tests/testdata/package_skip02.json rename to tests/testdata/big/skip02.json diff --git a/tests/testdata/big/skip03.json b/tests/testdata/big/skip03.json new file mode 100644 index 0000000..df1a36d --- /dev/null +++ b/tests/testdata/big/skip03.json @@ -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} \ No newline at end of file