Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(294)

Delta Between Two Patch Sets: src/cmd/go/test.go

Issue 10264045: code review 10264045: cmd/go: write coverage to file, add percentage statistic (Closed)
Left Patch Set: Created 11 years, 9 months ago
Right Patch Set: diff -r f1e1d5d02b1c https://code.google.com/p/go Created 11 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/cmd/go/testflag.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 "go/ast" 10 "go/ast"
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 126
127 -cover set,count,atomic 127 -cover set,count,atomic
128 TODO: This feature is not yet fully implemented. 128 TODO: This feature is not yet fully implemented.
129 Set the mode for coverage analysis for the package[s] being tested. 129 Set the mode for coverage analysis for the package[s] being tested.
130 The default is to do none. 130 The default is to do none.
131 The values: 131 The values:
132 set: boolean: does this statement execute? 132 set: boolean: does this statement execute?
133 count: integer: how many times does this statement execute? 133 count: integer: how many times does this statement execute?
134 atomic: integer: like count, but correct in multithreaded tests; 134 atomic: integer: like count, but correct in multithreaded tests;
135 significantly more expensive. 135 significantly more expensive.
136 » Sets -v. 136 » Sets -v. TODO: This will change.
137 137
138 -coverprofile cover.out 138 -coverprofile cover.out
139 Write a coverage profile to the specified file after all tests 139 Write a coverage profile to the specified file after all tests
140 have passed. 140 have passed.
141 141
142 -cpu 1,2,4 142 -cpu 1,2,4
143 Specify a list of GOMAXPROCS values for which the tests or 143 Specify a list of GOMAXPROCS values for which the tests or
144 benchmarks should be executed. The default is the current value 144 benchmarks should be executed. The default is the current value
145 of GOMAXPROCS. 145 of GOMAXPROCS.
146 146
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 ptestObj := buildToolchain.pkgpath(testDir, p) 530 ptestObj := buildToolchain.pkgpath(testDir, p)
531 531
532 // Create the directory for the .a files. 532 // Create the directory for the .a files.
533 ptestDir, _ := filepath.Split(ptestObj) 533 ptestDir, _ := filepath.Split(ptestObj)
534 if err := b.mkdir(ptestDir); err != nil { 534 if err := b.mkdir(ptestDir); err != nil {
535 return nil, nil, nil, err 535 return nil, nil, nil, err
536 } 536 }
537 537
538 if testCover != "" { 538 if testCover != "" {
539 p.coverMode = testCover 539 p.coverMode = testCover
540 » » p.coverVars = declareCoverVars(p.GoFiles...) 540 » » p.coverVars = declareCoverVars(p.ImportPath, p.GoFiles...)
541 } 541 }
542 542
543 if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), p, p.cov erVars); err != nil { 543 if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), p, p.cov erVars); err != nil {
544 return nil, nil, nil, err 544 return nil, nil, nil, err
545 } 545 }
546 546
547 // Test package. 547 // Test package.
548 if len(p.TestGoFiles) > 0 || testCover != "" { 548 if len(p.TestGoFiles) > 0 || testCover != "" {
549 ptest = new(Package) 549 ptest = new(Package)
550 *ptest = *p 550 *ptest = *p
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 } 674 }
675 } 675 }
676 676
677 return pmainAction, runAction, printAction, nil 677 return pmainAction, runAction, printAction, nil
678 } 678 }
679 679
680 var coverIndex = 0 680 var coverIndex = 0
681 681
682 // declareCoverVars attaches the required cover variables names 682 // declareCoverVars attaches the required cover variables names
683 // to the files, to be used when annotating the files. 683 // to the files, to be used when annotating the files.
684 func declareCoverVars(files ...string) map[string]*CoverVar { 684 func declareCoverVars(importPath string, files ...string) map[string]*CoverVar {
685 coverVars := make(map[string]*CoverVar) 685 coverVars := make(map[string]*CoverVar)
686 for _, file := range files { 686 for _, file := range files {
687 coverVars[file] = &CoverVar{ 687 coverVars[file] = &CoverVar{
688 » » » File: file, 688 » » » File: filepath.Join(importPath, file),
689 Var: fmt.Sprintf("GoCover_%d", coverIndex), 689 Var: fmt.Sprintf("GoCover_%d", coverIndex),
690 } 690 }
691 coverIndex++ 691 coverIndex++
692 } 692 }
693 return coverVars 693 return coverVars
694 } 694 }
695 695
696 // runTest is the action for running a test binary. 696 // runTest is the action for running a test binary.
697 func (b *builder) runTest(a *action) error { 697 func (b *builder) runTest(a *action) error {
698 args := stringList(a.deps[0].target, testArgs) 698 args := stringList(a.deps[0].target, testArgs)
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 {{if .CoverEnabled}} 974 {{if .CoverEnabled}}
975 975
976 // Only updated by init functions, so no need for atomicity. 976 // Only updated by init functions, so no need for atomicity.
977 var ( 977 var (
978 coverCounters = make(map[string][]uint32) 978 coverCounters = make(map[string][]uint32)
979 coverBlocks = make(map[string][]testing.CoverBlock) 979 coverBlocks = make(map[string][]testing.CoverBlock)
980 ) 980 )
981 981
982 func init() { 982 func init() {
983 {{range $file, $cover := .CoverVars}} 983 {{range $file, $cover := .CoverVars}}
984 » coverRegisterFile({{printf "%q" $file}}, _test.{{$cover.Var}}.Count[:], _test.{{$cover.Var}}.Pos[:], _test.{{$cover.Var}}.NumStmt[:]) 984 » coverRegisterFile({{printf "%q" $cover.File}}, _test.{{$cover.Var}}.Coun t[:], _test.{{$cover.Var}}.Pos[:], _test.{{$cover.Var}}.NumStmt[:])
985 {{end}} 985 {{end}}
986 } 986 }
987 987
988 func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) { 988 func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
989 if 3*len(counter) != len(pos) || len(counter) != len(numStmts) { 989 if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
990 panic("coverage: mismatched sizes") 990 panic("coverage: mismatched sizes")
991 } 991 }
992 if coverCounters[fileName] != nil { 992 if coverCounters[fileName] != nil {
993 panic("coverage: duplicate counter array for " + fileName) 993 panic("coverage: duplicate counter array for " + fileName)
994 } 994 }
995 coverCounters[fileName] = counter 995 coverCounters[fileName] = counter
996 block := make([]testing.CoverBlock, len(counter)) 996 block := make([]testing.CoverBlock, len(counter))
997 for i := range counter { 997 for i := range counter {
998 block[i] = testing.CoverBlock{ 998 block[i] = testing.CoverBlock{
999 Line0: pos[3*i+0], 999 Line0: pos[3*i+0],
1000 Col0: uint16(pos[3*i+2]), 1000 Col0: uint16(pos[3*i+2]),
1001 Line1: pos[3*i+1], 1001 Line1: pos[3*i+1],
1002 Col1: uint16(pos[3*i+2]>>16), 1002 Col1: uint16(pos[3*i+2]>>16),
1003 Stmts: numStmts[i], 1003 Stmts: numStmts[i],
1004 } 1004 }
1005 } 1005 }
1006 coverBlocks[fileName] = block 1006 coverBlocks[fileName] = block
1007 } 1007 }
1008 {{end}} 1008 {{end}}
1009 1009
1010 func main() { 1010 func main() {
1011 {{if .CoverEnabled}} 1011 {{if .CoverEnabled}}
1012 » testing.CoverRegister(coverCounters, coverBlocks) 1012 » testing.RegisterCover(coverCounters, coverBlocks)
1013 {{end}} 1013 {{end}}
1014 testing.Main(matchString, tests, benchmarks, examples) 1014 testing.Main(matchString, tests, benchmarks, examples)
1015 } 1015 }
1016 1016
1017 `)) 1017 `))
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b