Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.

Commit c3a8a64

Browse files
authored
Merge pull request #314 from markdryan/master
test-cases: Fix two bugs
2 parents e49c874 + 2ba00d6 commit c3a8a64

File tree

1 file changed

+99
-37
lines changed

1 file changed

+99
-37
lines changed

test-cases/test-cases.go

Lines changed: 99 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type TestInfo struct {
7474

7575
// TimeTaken is a description of the time taken to run the test case.
7676
TimeTaken string
77+
78+
logs []string
7779
}
7880

7981
// PackageTests contains information about the tests that have been executed for
@@ -92,6 +94,7 @@ type PackageTests struct {
9294
type testResults struct {
9395
result string
9496
timeTaken string
97+
logs []string
9598
}
9699

97100
type colouredRow struct {
@@ -271,6 +274,8 @@ func findTestFiles(packs []string) ([]PackageInfo, error) {
271274
}
272275

273276
func dumpErrorOutput(errorOutput *bytes.Buffer) {
277+
fmt.Fprintln(os.Stderr, "Output from stderr")
278+
fmt.Fprintln(os.Stderr, "------------------")
274279
scanner := bufio.NewScanner(errorOutput)
275280
for scanner.Scan() {
276281
line := scanner.Text()
@@ -279,6 +284,8 @@ func dumpErrorOutput(errorOutput *bytes.Buffer) {
279284
}
280285

281286
func dumpColourErrorOutput(errorOutput *bytes.Buffer) {
287+
fmt.Fprintln(os.Stderr, "Output from stderr")
288+
fmt.Fprintln(os.Stderr, "------------------")
282289
scanner := bufio.NewScanner(errorOutput)
283290
for scanner.Scan() {
284291
line := scanner.Text()
@@ -288,9 +295,48 @@ func dumpColourErrorOutput(errorOutput *bytes.Buffer) {
288295
fmt.Fprintf(os.Stderr, "%c[%dm\n", 0x1b, 0)
289296
}
290297

298+
func parseTestOutput(output bytes.Buffer, results map[string]*testResults) string {
299+
var coverage string
300+
301+
scanner := bufio.NewScanner(&output)
302+
key := ""
303+
for scanner.Scan() {
304+
line := scanner.Text()
305+
306+
stripped := strings.TrimSpace(line)
307+
if strings.HasPrefix(stripped, "PASS") || strings.HasPrefix(stripped, "FAIL") ||
308+
strings.HasPrefix(stripped, "=== RUN") {
309+
key = ""
310+
continue
311+
}
312+
313+
matches := resultRegexp.FindStringSubmatch(line)
314+
if matches != nil && len(matches) == 4 {
315+
key = matches[2]
316+
results[key] = &testResults{
317+
result: matches[1],
318+
timeTaken: matches[3],
319+
logs: make([]string, 0, 16)}
320+
continue
321+
}
322+
323+
if key != "" {
324+
results[key].logs = append(results[key].logs, line)
325+
}
326+
327+
if coverage == "" {
328+
matches := coverageRegexp.FindStringSubmatch(line)
329+
if matches == nil || len(matches) != 2 {
330+
continue
331+
}
332+
coverage = matches[1]
333+
}
334+
}
335+
return coverage
336+
}
337+
291338
func runPackageTests(p *PackageTests, coverFile string, errorOutput *bytes.Buffer) (int, error) {
292339
var output bytes.Buffer
293-
var coverage string
294340

295341
exitCode := 0
296342
results := make(map[string]*testResults)
@@ -309,23 +355,7 @@ func runPackageTests(p *PackageTests, coverFile string, errorOutput *bytes.Buffe
309355
cmd.Stderr = errorOutput
310356
err := cmd.Run()
311357

312-
scanner := bufio.NewScanner(&output)
313-
for scanner.Scan() {
314-
line := scanner.Text()
315-
matches := resultRegexp.FindStringSubmatch(line)
316-
if matches != nil && len(matches) == 4 {
317-
results[matches[2]] = &testResults{matches[1], matches[3]}
318-
continue
319-
}
320-
321-
if coverage == "" {
322-
matches := coverageRegexp.FindStringSubmatch(line)
323-
if matches == nil || len(matches) != 2 {
324-
continue
325-
}
326-
coverage = matches[1]
327-
}
328-
}
358+
coverage := parseTestOutput(output, results)
329359

330360
for _, t := range p.Tests {
331361
res := results[t.Name]
@@ -340,6 +370,7 @@ func runPackageTests(p *PackageTests, coverFile string, errorOutput *bytes.Buffe
340370
exitCode = 1
341371
}
342372
t.TimeTaken = res.timeTaken
373+
t.logs = res.logs
343374
}
344375
}
345376

@@ -393,30 +424,53 @@ func generateHTMLReport(tests []*PackageTests) error {
393424
}
394425

395426
func findCommonPrefix(tests []*PackageTests) string {
396-
if len(tests) == 0 {
397-
return ""
398-
}
399-
400-
pkgName := tests[0].Name
401-
OUTER:
402-
for {
427+
for j := range tests {
428+
pkgName := tests[j].Name
403429
index := strings.LastIndex(pkgName, "/")
404430
if index == -1 {
405431
return ""
406432
}
407-
pkgName := pkgName[:index+1]
433+
pkgRoot := pkgName[:index+1]
408434

409435
var i int
410-
for i = 1; i < len(tests); i++ {
411-
if !strings.HasPrefix(tests[i].Name, pkgName) {
412-
continue OUTER
436+
for i = 0; i < len(tests); i++ {
437+
if i == j {
438+
continue
439+
}
440+
if !strings.HasPrefix(tests[i].Name, pkgRoot) {
441+
break
442+
}
443+
}
444+
445+
if i == len(tests) {
446+
return pkgRoot
447+
}
448+
}
449+
450+
return ""
451+
}
452+
453+
func dumpFailedTestOutput(prefix string, tests []*PackageTests, colourOn, colourOff string) {
454+
fmt.Fprintln(os.Stderr, "")
455+
fmt.Fprintln(os.Stderr, "Logs for failed tests")
456+
fmt.Fprintln(os.Stderr, "---------------------")
457+
for _, p := range tests {
458+
for _, t := range p.Tests {
459+
if !t.Pass && len(t.logs) > 0 {
460+
fmt.Fprintf(os.Stderr, "%s", colourOff)
461+
pkgName := p.Name[len(prefix):]
462+
fmt.Fprintf(os.Stderr, "Logs for %s.%s\n", pkgName, t.Name)
463+
for _, s := range t.logs {
464+
fmt.Fprintf(os.Stderr, "%s%s\n", colourOn, s)
465+
}
413466
}
414467
}
415-
return pkgName
416468
}
417469
}
418470

419-
func generateColourTextReport(tests []*PackageTests) {
471+
func generateColourTextReport(tests []*PackageTests, exitCode int) {
472+
colourOn := fmt.Sprintf("%c[%dm", 0x1b, 31)
473+
colourOff := fmt.Sprintf("%c[%dm", 0x1b, 0)
420474
prefix := findCommonPrefix(tests)
421475
table := make([]colouredRow, 0, 128)
422476
table = append(table, colouredRow{
@@ -434,11 +488,11 @@ func generateColourTextReport(tests []*PackageTests) {
434488
for _, t := range p.Tests {
435489
row := colouredRow{}
436490
if !t.Pass {
437-
row.ansiSeq = fmt.Sprintf("%c[%dm", 0x1b, 31)
491+
row.ansiSeq = colourOn
438492
coloured = true
439493
} else if t.Pass && coloured {
440494
coloured = false
441-
row.ansiSeq = fmt.Sprintf("%c[%dm", 0x1b, 0)
495+
row.ansiSeq = colourOff
442496
}
443497
row.columns = []string{pkgName, t.Name, t.TimeTaken, t.Result}
444498
for i := range colWidth {
@@ -463,9 +517,14 @@ func generateColourTextReport(tests []*PackageTests) {
463517
if coloured {
464518
fmt.Printf("%c[%dm\n", 0x1b, 0)
465519
}
520+
521+
if exitCode != 0 {
522+
dumpFailedTestOutput(prefix, tests, colourOn, colourOff)
523+
fmt.Println(colourOff)
524+
}
466525
}
467526

468-
func generateTextReport(tests []*PackageTests) {
527+
func generateTextReport(tests []*PackageTests, exitCode int) {
469528
prefix := findCommonPrefix(tests)
470529
w := new(tabwriter.Writer)
471530
w.Init(os.Stdout, 0, 8, 1, ' ', 0)
@@ -478,7 +537,10 @@ func generateTextReport(tests []*PackageTests) {
478537
}
479538
}
480539
_ = w.Flush()
481-
fmt.Println()
540+
if exitCode != 0 {
541+
dumpFailedTestOutput(prefix, tests, "", "")
542+
fmt.Println()
543+
}
482544
}
483545

484546
func createCoverFile() (*os.File, error) {
@@ -585,9 +647,9 @@ func main() {
585647

586648
if textOutput {
587649
if colour {
588-
generateColourTextReport(tests)
650+
generateColourTextReport(tests, exitCode)
589651
} else {
590-
generateTextReport(tests)
652+
generateTextReport(tests, exitCode)
591653
}
592654
} else {
593655
err = generateHTMLReport(tests)

0 commit comments

Comments
 (0)