mockgen.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // Copyright 2010 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // MockGen generates mock implementations of Go interfaces.
  15. package main
  16. // TODO: This does not support recursive embedded interfaces.
  17. // TODO: This does not support embedding package-local interfaces in a separate file.
  18. import (
  19. "bytes"
  20. "flag"
  21. "fmt"
  22. "go/build"
  23. "go/format"
  24. "go/token"
  25. "io"
  26. "log"
  27. "os"
  28. "path"
  29. "path/filepath"
  30. "sort"
  31. "strconv"
  32. "strings"
  33. "unicode"
  34. "github.com/golang/mock/mockgen/model"
  35. )
  36. const (
  37. gomockImportPath = "github.com/golang/mock/gomock"
  38. )
  39. var (
  40. source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
  41. destination = flag.String("destination", "", "Output file; defaults to stdout.")
  42. mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.")
  43. packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
  44. selfPackage = flag.String("self_package", "", "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.")
  45. writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
  46. debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
  47. )
  48. func main() {
  49. flag.Usage = usage
  50. flag.Parse()
  51. var pkg *model.Package
  52. var err error
  53. if *source != "" {
  54. pkg, err = ParseFile(*source)
  55. } else {
  56. if flag.NArg() != 2 {
  57. usage()
  58. log.Fatal("Expected exactly two arguments")
  59. }
  60. pkg, err = Reflect(flag.Arg(0), strings.Split(flag.Arg(1), ","))
  61. }
  62. if err != nil {
  63. log.Fatalf("Loading input failed: %v", err)
  64. }
  65. if *debugParser {
  66. pkg.Print(os.Stdout)
  67. return
  68. }
  69. dst := os.Stdout
  70. if len(*destination) > 0 {
  71. f, err := os.Create(*destination)
  72. if err != nil {
  73. log.Fatalf("Failed opening destination file: %v", err)
  74. }
  75. defer f.Close()
  76. dst = f
  77. }
  78. packageName := *packageOut
  79. if packageName == "" {
  80. // pkg.Name in reflect mode is the base name of the import path,
  81. // which might have characters that are illegal to have in package names.
  82. packageName = "mock_" + sanitize(pkg.Name)
  83. }
  84. // outputPackagePath represents the fully qualified name of the package of
  85. // the generated code. Its purposes are to prevent the module from importing
  86. // itself and to prevent qualifying type names that come from its own
  87. // package (i.e. if there is a type called X then we want to print "X" not
  88. // "package.X" since "package" is this package). This can happen if the mock
  89. // is output into an already existing package.
  90. outputPackagePath := *selfPackage
  91. if len(outputPackagePath) == 0 && len(*destination) > 0 {
  92. dst, _ := filepath.Abs(filepath.Dir(*destination))
  93. for _, prefix := range build.Default.SrcDirs() {
  94. if strings.HasPrefix(dst, prefix) {
  95. if rel, err := filepath.Rel(prefix, dst); err == nil {
  96. outputPackagePath = rel
  97. break
  98. }
  99. }
  100. }
  101. }
  102. g := new(generator)
  103. if *source != "" {
  104. g.filename = *source
  105. } else {
  106. g.srcPackage = flag.Arg(0)
  107. g.srcInterfaces = flag.Arg(1)
  108. }
  109. if *mockNames != "" {
  110. g.mockNames = parseMockNames(*mockNames)
  111. }
  112. if err := g.Generate(pkg, packageName, outputPackagePath); err != nil {
  113. log.Fatalf("Failed generating mock: %v", err)
  114. }
  115. if _, err := dst.Write(g.Output()); err != nil {
  116. log.Fatalf("Failed writing to destination: %v", err)
  117. }
  118. }
  119. func parseMockNames(names string) map[string]string {
  120. mocksMap := make(map[string]string)
  121. for _, kv := range strings.Split(names, ",") {
  122. parts := strings.SplitN(kv, "=", 2)
  123. if len(parts) != 2 || parts[1] == "" {
  124. log.Fatalf("bad mock names spec: %v", kv)
  125. }
  126. mocksMap[parts[0]] = parts[1]
  127. }
  128. return mocksMap
  129. }
  130. func usage() {
  131. io.WriteString(os.Stderr, usageText)
  132. flag.PrintDefaults()
  133. }
  134. const usageText = `mockgen has two modes of operation: source and reflect.
  135. Source mode generates mock interfaces from a source file.
  136. It is enabled by using the -source flag. Other flags that
  137. may be useful in this mode are -imports and -aux_files.
  138. Example:
  139. mockgen -source=foo.go [other options]
  140. Reflect mode generates mock interfaces by building a program
  141. that uses reflection to understand interfaces. It is enabled
  142. by passing two non-flag arguments: an import path, and a
  143. comma-separated list of symbols.
  144. Example:
  145. mockgen database/sql/driver Conn,Driver
  146. `
  147. type generator struct {
  148. buf bytes.Buffer
  149. indent string
  150. mockNames map[string]string //may be empty
  151. filename string // may be empty
  152. srcPackage, srcInterfaces string // may be empty
  153. packageMap map[string]string // map from import path to package name
  154. }
  155. func (g *generator) p(format string, args ...interface{}) {
  156. fmt.Fprintf(&g.buf, g.indent+format+"\n", args...)
  157. }
  158. func (g *generator) in() {
  159. g.indent += "\t"
  160. }
  161. func (g *generator) out() {
  162. if len(g.indent) > 0 {
  163. g.indent = g.indent[0 : len(g.indent)-1]
  164. }
  165. }
  166. func removeDot(s string) string {
  167. if len(s) > 0 && s[len(s)-1] == '.' {
  168. return s[0 : len(s)-1]
  169. }
  170. return s
  171. }
  172. // sanitize cleans up a string to make a suitable package name.
  173. func sanitize(s string) string {
  174. t := ""
  175. for _, r := range s {
  176. if t == "" {
  177. if unicode.IsLetter(r) || r == '_' {
  178. t += string(r)
  179. continue
  180. }
  181. } else {
  182. if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
  183. t += string(r)
  184. continue
  185. }
  186. }
  187. t += "_"
  188. }
  189. if t == "_" {
  190. t = "x"
  191. }
  192. return t
  193. }
  194. func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePath string) error {
  195. g.p("// Code generated by MockGen. DO NOT EDIT.")
  196. if g.filename != "" {
  197. g.p("// Source: %v", g.filename)
  198. } else {
  199. g.p("// Source: %v (interfaces: %v)", g.srcPackage, g.srcInterfaces)
  200. }
  201. g.p("")
  202. // Get all required imports, and generate unique names for them all.
  203. im := pkg.Imports()
  204. im[gomockImportPath] = true
  205. // Only import reflect if it's used. We only use reflect in mocked methods
  206. // so only import if any of the mocked interfaces have methods.
  207. for _, intf := range pkg.Interfaces {
  208. if len(intf.Methods) > 0 {
  209. im["reflect"] = true
  210. break
  211. }
  212. }
  213. // Sort keys to make import alias generation predictable
  214. sorted_paths := make([]string, len(im), len(im))
  215. x := 0
  216. for pth := range im {
  217. sorted_paths[x] = pth
  218. x++
  219. }
  220. sort.Strings(sorted_paths)
  221. g.packageMap = make(map[string]string, len(im))
  222. localNames := make(map[string]bool, len(im))
  223. for _, pth := range sorted_paths {
  224. base := sanitize(path.Base(pth))
  225. // Local names for an imported package can usually be the basename of the import path.
  226. // A couple of situations don't permit that, such as duplicate local names
  227. // (e.g. importing "html/template" and "text/template"), or where the basename is
  228. // a keyword (e.g. "foo/case").
  229. // try base0, base1, ...
  230. pkgName := base
  231. i := 0
  232. for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() {
  233. pkgName = base + strconv.Itoa(i)
  234. i++
  235. }
  236. g.packageMap[pth] = pkgName
  237. localNames[pkgName] = true
  238. }
  239. if *writePkgComment {
  240. g.p("// Package %v is a generated GoMock package.", pkgName)
  241. }
  242. g.p("package %v", pkgName)
  243. g.p("")
  244. g.p("import (")
  245. g.in()
  246. for path, pkg := range g.packageMap {
  247. if path == outputPackagePath {
  248. continue
  249. }
  250. g.p("%v %q", pkg, path)
  251. }
  252. for _, path := range pkg.DotImports {
  253. g.p(". %q", path)
  254. }
  255. g.out()
  256. g.p(")")
  257. for _, intf := range pkg.Interfaces {
  258. if err := g.GenerateMockInterface(intf, outputPackagePath); err != nil {
  259. return err
  260. }
  261. }
  262. return nil
  263. }
  264. // The name of the mock type to use for the given interface identifier.
  265. func (g *generator) mockName(typeName string) string {
  266. if mockName, ok := g.mockNames[typeName]; ok {
  267. return mockName
  268. }
  269. return "Mock" + typeName
  270. }
  271. func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePath string) error {
  272. mockType := g.mockName(intf.Name)
  273. g.p("")
  274. g.p("// %v is a mock of %v interface", mockType, intf.Name)
  275. g.p("type %v struct {", mockType)
  276. g.in()
  277. g.p("ctrl *gomock.Controller")
  278. g.p("recorder *%vMockRecorder", mockType)
  279. g.out()
  280. g.p("}")
  281. g.p("")
  282. g.p("// %vMockRecorder is the mock recorder for %v", mockType, mockType)
  283. g.p("type %vMockRecorder struct {", mockType)
  284. g.in()
  285. g.p("mock *%v", mockType)
  286. g.out()
  287. g.p("}")
  288. g.p("")
  289. // TODO: Re-enable this if we can import the interface reliably.
  290. //g.p("// Verify that the mock satisfies the interface at compile time.")
  291. //g.p("var _ %v = (*%v)(nil)", typeName, mockType)
  292. //g.p("")
  293. g.p("// New%v creates a new mock instance", mockType)
  294. g.p("func New%v(ctrl *gomock.Controller) *%v {", mockType, mockType)
  295. g.in()
  296. g.p("mock := &%v{ctrl: ctrl}", mockType)
  297. g.p("mock.recorder = &%vMockRecorder{mock}", mockType)
  298. g.p("return mock")
  299. g.out()
  300. g.p("}")
  301. g.p("")
  302. // XXX: possible name collision here if someone has EXPECT in their interface.
  303. g.p("// EXPECT returns an object that allows the caller to indicate expected use")
  304. g.p("func (m *%v) EXPECT() *%vMockRecorder {", mockType, mockType)
  305. g.in()
  306. g.p("return m.recorder")
  307. g.out()
  308. g.p("}")
  309. g.GenerateMockMethods(mockType, intf, outputPackagePath)
  310. return nil
  311. }
  312. func (g *generator) GenerateMockMethods(mockType string, intf *model.Interface, pkgOverride string) {
  313. for _, m := range intf.Methods {
  314. g.p("")
  315. g.GenerateMockMethod(mockType, m, pkgOverride)
  316. g.p("")
  317. g.GenerateMockRecorderMethod(mockType, m)
  318. }
  319. }
  320. func makeArgString(argNames, argTypes []string) string {
  321. args := make([]string, len(argNames))
  322. for i, name := range argNames {
  323. // specify the type only once for consecutive args of the same type
  324. if i+1 < len(argTypes) && argTypes[i] == argTypes[i+1] {
  325. args[i] = name
  326. } else {
  327. args[i] = name + " " + argTypes[i]
  328. }
  329. }
  330. return strings.Join(args, ", ")
  331. }
  332. // GenerateMockMethod generates a mock method implementation.
  333. // If non-empty, pkgOverride is the package in which unqualified types reside.
  334. func (g *generator) GenerateMockMethod(mockType string, m *model.Method, pkgOverride string) error {
  335. argNames := g.getArgNames(m)
  336. argTypes := g.getArgTypes(m, pkgOverride)
  337. argString := makeArgString(argNames, argTypes)
  338. rets := make([]string, len(m.Out))
  339. for i, p := range m.Out {
  340. rets[i] = p.Type.String(g.packageMap, pkgOverride)
  341. }
  342. retString := strings.Join(rets, ", ")
  343. if len(rets) > 1 {
  344. retString = "(" + retString + ")"
  345. }
  346. if retString != "" {
  347. retString = " " + retString
  348. }
  349. ia := newIdentifierAllocator(argNames)
  350. idRecv := ia.allocateIdentifier("m")
  351. g.p("// %v mocks base method", m.Name)
  352. g.p("func (%v *%v) %v(%v)%v {", idRecv, mockType, m.Name, argString, retString)
  353. g.in()
  354. var callArgs string
  355. if m.Variadic == nil {
  356. if len(argNames) > 0 {
  357. callArgs = ", " + strings.Join(argNames, ", ")
  358. }
  359. } else {
  360. // Non-trivial. The generated code must build a []interface{},
  361. // but the variadic argument may be any type.
  362. idVarArgs := ia.allocateIdentifier("varargs")
  363. idVArg := ia.allocateIdentifier("a")
  364. g.p("%s := []interface{}{%s}", idVarArgs, strings.Join(argNames[:len(argNames)-1], ", "))
  365. g.p("for _, %s := range %s {", idVArg, argNames[len(argNames)-1])
  366. g.in()
  367. g.p("%s = append(%s, %s)", idVarArgs, idVarArgs, idVArg)
  368. g.out()
  369. g.p("}")
  370. callArgs = ", " + idVarArgs + "..."
  371. }
  372. if len(m.Out) == 0 {
  373. g.p(`%v.ctrl.Call(%v, %q%v)`, idRecv, idRecv, m.Name, callArgs)
  374. } else {
  375. idRet := ia.allocateIdentifier("ret")
  376. g.p(`%v := %v.ctrl.Call(%v, %q%v)`, idRet, idRecv, idRecv, m.Name, callArgs)
  377. // Go does not allow "naked" type assertions on nil values, so we use the two-value form here.
  378. // The value of that is either (x.(T), true) or (Z, false), where Z is the zero value for T.
  379. // Happily, this coincides with the semantics we want here.
  380. retNames := make([]string, len(rets))
  381. for i, t := range rets {
  382. retNames[i] = ia.allocateIdentifier(fmt.Sprintf("ret%d", i))
  383. g.p("%s, _ := %s[%d].(%s)", retNames[i], idRet, i, t)
  384. }
  385. g.p("return " + strings.Join(retNames, ", "))
  386. }
  387. g.out()
  388. g.p("}")
  389. return nil
  390. }
  391. func (g *generator) GenerateMockRecorderMethod(mockType string, m *model.Method) error {
  392. argNames := g.getArgNames(m)
  393. var argString string
  394. if m.Variadic == nil {
  395. argString = strings.Join(argNames, ", ")
  396. } else {
  397. argString = strings.Join(argNames[:len(argNames)-1], ", ")
  398. }
  399. if argString != "" {
  400. argString += " interface{}"
  401. }
  402. if m.Variadic != nil {
  403. if argString != "" {
  404. argString += ", "
  405. }
  406. argString += fmt.Sprintf("%s ...interface{}", argNames[len(argNames)-1])
  407. }
  408. ia := newIdentifierAllocator(argNames)
  409. idRecv := ia.allocateIdentifier("mr")
  410. g.p("// %v indicates an expected call of %v", m.Name, m.Name)
  411. g.p("func (%s *%vMockRecorder) %v(%v) *gomock.Call {", idRecv, mockType, m.Name, argString)
  412. g.in()
  413. var callArgs string
  414. if m.Variadic == nil {
  415. if len(argNames) > 0 {
  416. callArgs = ", " + strings.Join(argNames, ", ")
  417. }
  418. } else {
  419. if len(argNames) == 1 {
  420. // Easy: just use ... to push the arguments through.
  421. callArgs = ", " + argNames[0] + "..."
  422. } else {
  423. // Hard: create a temporary slice.
  424. idVarArgs := ia.allocateIdentifier("varargs")
  425. g.p("%s := append([]interface{}{%s}, %s...)",
  426. idVarArgs,
  427. strings.Join(argNames[:len(argNames)-1], ", "),
  428. argNames[len(argNames)-1])
  429. callArgs = ", " + idVarArgs + "..."
  430. }
  431. }
  432. g.p(`return %s.mock.ctrl.RecordCallWithMethodType(%s.mock, "%s", reflect.TypeOf((*%s)(nil).%s)%s)`, idRecv, idRecv, m.Name, mockType, m.Name, callArgs)
  433. g.out()
  434. g.p("}")
  435. return nil
  436. }
  437. func (g *generator) getArgNames(m *model.Method) []string {
  438. argNames := make([]string, len(m.In))
  439. for i, p := range m.In {
  440. name := p.Name
  441. if name == "" {
  442. name = fmt.Sprintf("arg%d", i)
  443. }
  444. argNames[i] = name
  445. }
  446. if m.Variadic != nil {
  447. name := m.Variadic.Name
  448. if name == "" {
  449. name = fmt.Sprintf("arg%d", len(m.In))
  450. }
  451. argNames = append(argNames, name)
  452. }
  453. return argNames
  454. }
  455. func (g *generator) getArgTypes(m *model.Method, pkgOverride string) []string {
  456. argTypes := make([]string, len(m.In))
  457. for i, p := range m.In {
  458. argTypes[i] = p.Type.String(g.packageMap, pkgOverride)
  459. }
  460. if m.Variadic != nil {
  461. argTypes = append(argTypes, "..."+m.Variadic.Type.String(g.packageMap, pkgOverride))
  462. }
  463. return argTypes
  464. }
  465. type identifierAllocator map[string]struct{}
  466. func newIdentifierAllocator(taken []string) identifierAllocator {
  467. a := make(identifierAllocator, len(taken))
  468. for _, s := range taken {
  469. a[s] = struct{}{}
  470. }
  471. return a
  472. }
  473. func (o identifierAllocator) allocateIdentifier(want string) string {
  474. id := want
  475. for i := 2; ; i++ {
  476. if _, ok := o[id]; !ok {
  477. o[id] = struct{}{}
  478. return id
  479. }
  480. id = want + "_" + strconv.Itoa(i)
  481. }
  482. }
  483. // Output returns the generator's output, formatted in the standard Go style.
  484. func (g *generator) Output() []byte {
  485. src, err := format.Source(g.buf.Bytes())
  486. if err != nil {
  487. log.Fatalf("Failed to format generated source code: %s\n%s", err, g.buf.String())
  488. }
  489. return src
  490. }