@@ -11,60 +11,152 @@ package main
1111// the workspace. Further, this should be run from the root of the workspace.
1212//
1313// The binary can be invoked in one of two ways:
14- // * single test lookup: `testowner PKG TEST`. The owner(s) for the test will be
14+ // * single test lookup: `testowner PKG. TEST`. The owner(s) for the test will be
1515// printed to stdout, separated by a space.
1616// * multi-test lookup: `testowner` (with no arguments). In this case, stdin
17- // will be a list of test lookups, one on each line, of the form `PKG TEST`.
17+ // will be a list of test lookups, one on each line, of the form `PKG. TEST`.
1818// the output will be one owner per line.
19+ // You can additionally pass in a command-line flag -roachtest. If true, the
20+ // test lookup will performed as if the test were a roachtest rather than a Go
21+ // test. In this case, the input should be passed in as the test name alone
22+ // instead of PKG.TEST.
23+ //
24+ // Examples:
25+ // testowner github.com/cockroachdb/cockroach/pkg/cmd/dev.TestDataDriven
26+ // testowner pkg/cmd/dev.TestDataDriven
27+ // testowner -roachtest acceptance/build-analyze
1928
2029import (
2130 "bufio"
31+ "flag"
2232 "fmt"
2333 "io"
2434 "os"
2535 "strings"
2636
37+ "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry"
38+ "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/spec"
39+ "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/tests"
2740 "github.com/cockroachdb/cockroach/pkg/internal/codeowners"
2841 "github.com/cockroachdb/cockroach/pkg/internal/team"
42+ "github.com/prometheus/client_golang/prometheus/promauto"
2943)
3044
31- func main () {
32- co , err := codeowners .DefaultLoadCodeOwners ()
33- if err != nil {
34- panic (err )
45+ type registryImpl struct {
46+ nameToOwner map [string ]registry.Owner
47+ }
48+
49+ func (r * registryImpl ) MakeClusterSpec (nodeCount int , opts ... spec.Option ) spec.ClusterSpec {
50+ return spec.ClusterSpec {}
51+ }
52+
53+ func (r * registryImpl ) Add (spec registry.TestSpec ) {
54+ name , owner := spec .Name , spec .Owner
55+ _ , ok := r .nameToOwner [name ]
56+ if ok {
57+ panic (fmt .Sprintf ("duplicate test of name %s" , name ))
3558 }
59+ r .nameToOwner [name ] = owner
60+ }
61+
62+ func (r * registryImpl ) AddOperation (registry.OperationSpec ) {}
63+
64+ func (r * registryImpl ) PromFactory () promauto.Factory {
65+ return promauto .With (nil )
66+ }
67+
68+ var (
69+ roachtest = flag .Bool ("roachtest" , false , "whether to look up the test as a roachtest" )
70+ _ registry.Registry = (* registryImpl )(nil )
71+ )
72+
73+ func mainImpl () int {
74+ flag .Parse ()
3675 stdout := bufio .NewWriter (os .Stdout )
3776 stderr := bufio .NewWriter (os .Stderr )
38- if len (os .Args ) == 1 {
39- stdin := bufio .NewScanner (os .Stdin )
40- for stdin .Scan () {
41- line := stdin .Text ()
42- pkg , test := parseQuery (line )
77+ defer func () {
78+ _ = stdout .Flush ()
79+ }()
80+ defer func () {
81+ _ = stderr .Flush ()
82+ }()
83+
84+ if * roachtest {
85+ reg := registryImpl {
86+ nameToOwner : make (map [string ]registry.Owner ),
87+ }
88+ tests .RegisterTests (& reg )
89+ if len (flag .Args ()) == 0 {
90+ stdin := bufio .NewScanner (os .Stdin )
91+ for stdin .Scan () {
92+ line := stdin .Text ()
93+ owner , ok := reg .nameToOwner [line ]
94+ if ! ok {
95+ panic (fmt .Sprintf ("unknown roachtest of name %s" , line ))
96+ }
97+ _ , err := stdout .WriteString (string (owner .ToTeamAlias ()))
98+ if err != nil {
99+ panic (err )
100+ }
101+ err = stdout .WriteByte ('\n' )
102+ if err != nil {
103+ panic (err )
104+ }
105+ }
106+ if err := stdin .Err (); err != nil {
107+ panic (err )
108+ }
109+
110+ } else if len (flag .Args ()) == 1 {
111+ owner , ok := reg .nameToOwner [flag .Args ()[0 ]]
112+ if ! ok {
113+ panic (fmt .Sprintf ("unknown roachtest of name %s" , flag .Args ()[0 ]))
114+ }
115+ _ , err := stdout .WriteString (string (owner .ToTeamAlias ()))
116+ if err != nil {
117+ panic (err )
118+ }
119+ err = stdout .WriteByte ('\n' )
120+ if err != nil {
121+ panic (err )
122+ }
123+ } else {
124+ fmt .Fprintf (stderr , "usage: `testowner TEST` or `testowner` (with queries in stdin); got %d arguments, expected 0-1\n " , len (flag .Args ()))
125+ return 1
126+ }
127+ } else {
128+ co , err := codeowners .DefaultLoadCodeOwners ()
129+ if err != nil {
130+ panic (err )
131+ }
132+
133+ if len (flag .Args ()) == 0 {
134+ stdin := bufio .NewScanner (os .Stdin )
135+ for stdin .Scan () {
136+ line := stdin .Text ()
137+ pkg , test := parseQuery (line )
138+ teams , logs := co .GetTestOwner (pkg , test )
139+ writeOwners (stdout , teams )
140+ for _ , log := range logs {
141+ fmt .Fprintln (stderr , log )
142+ }
143+ }
144+ if err := stdin .Err (); err != nil {
145+ panic (err )
146+ }
147+ } else if len (flag .Args ()) == 1 {
148+ pkg , test := parseQuery (flag .Args ()[0 ])
43149 teams , logs := co .GetTestOwner (pkg , test )
44150 writeOwners (stdout , teams )
45151 for _ , log := range logs {
46152 fmt .Fprintln (stderr , log )
47153 }
154+ } else {
155+ fmt .Fprintf (stderr , "usage: `testowner PKG.TEST` or `testowner` (with queries in stdin); got %d arguments, expected 0-1\n " , len (flag .Args ()))
156+ return 1
48157 }
49- if err := stdin .Err (); err != nil {
50- panic (err )
51- }
52- } else if len (os .Args ) == 2 {
53- pkg , test := parseQuery (os .Args [1 ])
54- teams , logs := co .GetTestOwner (pkg , test )
55- writeOwners (stdout , teams )
56- for _ , log := range logs {
57- fmt .Fprintln (stderr , log )
58- }
59- } else {
60- fmt .Fprintf (stderr , "usage: `testowner PKG.TEST` or `testowner` (with queries in stdin); got %d arguments, expected 1-2" , len (os .Args ))
61- }
62- if err := stdout .Flush (); err != nil {
63- panic (err )
64- }
65- if err := stderr .Flush (); err != nil {
66- panic (err )
67158 }
159+ return 0
68160}
69161
70162func parseQuery (pkgAndTest string ) (pkg string , test string ) {
@@ -106,3 +198,7 @@ func writeOwners(w io.Writer, teams []team.Team) {
106198 panic (err )
107199 }
108200}
201+
202+ func main () {
203+ os .Exit (mainImpl ())
204+ }
0 commit comments