golden_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package main
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "go/build"
  7. "go/parser"
  8. "go/token"
  9. "io/ioutil"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "regexp"
  14. "runtime"
  15. "strings"
  16. "testing"
  17. )
  18. // Set --regenerate to regenerate the golden files.
  19. var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
  20. // When the environment variable RUN_AS_PROTOC_GEN_GO is set, we skip running
  21. // tests and instead act as protoc-gen-go. This allows the test binary to
  22. // pass itself to protoc.
  23. func init() {
  24. if os.Getenv("RUN_AS_PROTOC_GEN_GO") != "" {
  25. main()
  26. os.Exit(0)
  27. }
  28. }
  29. func TestGolden(t *testing.T) {
  30. workdir, err := ioutil.TempDir("", "proto-test")
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. defer os.RemoveAll(workdir)
  35. // Find all the proto files we need to compile. We assume that each directory
  36. // contains the files for a single package.
  37. supportTypeAliases := hasReleaseTag("go1.9")
  38. packages := map[string][]string{}
  39. err = filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
  40. if filepath.Base(path) == "import_public" && !supportTypeAliases {
  41. // Public imports require type alias support.
  42. return filepath.SkipDir
  43. }
  44. if !strings.HasSuffix(path, ".proto") {
  45. return nil
  46. }
  47. dir := filepath.Dir(path)
  48. packages[dir] = append(packages[dir], path)
  49. return nil
  50. })
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. // Compile each package, using this binary as protoc-gen-go.
  55. for _, sources := range packages {
  56. args := []string{"-Itestdata", "--go_out=plugins=grpc,paths=source_relative:" + workdir}
  57. args = append(args, sources...)
  58. protoc(t, args)
  59. }
  60. // Compare each generated file to the golden version.
  61. filepath.Walk(workdir, func(genPath string, info os.FileInfo, _ error) error {
  62. if info.IsDir() {
  63. return nil
  64. }
  65. // For each generated file, figure out the path to the corresponding
  66. // golden file in the testdata directory.
  67. relPath, err := filepath.Rel(workdir, genPath)
  68. if err != nil {
  69. t.Errorf("filepath.Rel(%q, %q): %v", workdir, genPath, err)
  70. return nil
  71. }
  72. if filepath.SplitList(relPath)[0] == ".." {
  73. t.Errorf("generated file %q is not relative to %q", genPath, workdir)
  74. }
  75. goldenPath := filepath.Join("testdata", relPath)
  76. got, err := ioutil.ReadFile(genPath)
  77. if err != nil {
  78. t.Error(err)
  79. return nil
  80. }
  81. if *regenerate {
  82. // If --regenerate set, just rewrite the golden files.
  83. err := ioutil.WriteFile(goldenPath, got, 0666)
  84. if err != nil {
  85. t.Error(err)
  86. }
  87. return nil
  88. }
  89. want, err := ioutil.ReadFile(goldenPath)
  90. if err != nil {
  91. t.Error(err)
  92. return nil
  93. }
  94. want = fdescRE.ReplaceAll(want, nil)
  95. got = fdescRE.ReplaceAll(got, nil)
  96. if bytes.Equal(got, want) {
  97. return nil
  98. }
  99. cmd := exec.Command("diff", "-u", goldenPath, genPath)
  100. out, _ := cmd.CombinedOutput()
  101. t.Errorf("golden file differs: %v\n%v", relPath, string(out))
  102. return nil
  103. })
  104. }
  105. var fdescRE = regexp.MustCompile(`(?ms)^var fileDescriptor.*}`)
  106. // Source files used by TestParameters.
  107. const (
  108. aProto = `
  109. syntax = "proto3";
  110. package test.alpha;
  111. option go_package = "package/alpha";
  112. import "beta/b.proto";
  113. message M { test.beta.M field = 1; }`
  114. bProto = `
  115. syntax = "proto3";
  116. package test.beta;
  117. // no go_package option
  118. message M {}`
  119. )
  120. func TestParameters(t *testing.T) {
  121. for _, test := range []struct {
  122. parameters string
  123. wantFiles map[string]bool
  124. wantImportsA map[string]bool
  125. wantPackageA string
  126. wantPackageB string
  127. }{{
  128. parameters: "",
  129. wantFiles: map[string]bool{
  130. "package/alpha/a.pb.go": true,
  131. "beta/b.pb.go": true,
  132. },
  133. wantPackageA: "alpha",
  134. wantPackageB: "test_beta",
  135. wantImportsA: map[string]bool{
  136. "github.com/golang/protobuf/proto": true,
  137. "beta": true,
  138. },
  139. }, {
  140. parameters: "import_prefix=prefix",
  141. wantFiles: map[string]bool{
  142. "package/alpha/a.pb.go": true,
  143. "beta/b.pb.go": true,
  144. },
  145. wantPackageA: "alpha",
  146. wantPackageB: "test_beta",
  147. wantImportsA: map[string]bool{
  148. // This really doesn't seem like useful behavior.
  149. "prefixgithub.com/golang/protobuf/proto": true,
  150. "prefixbeta": true,
  151. },
  152. }, {
  153. // import_path only affects the 'package' line.
  154. parameters: "import_path=import/path/of/pkg",
  155. wantPackageA: "alpha",
  156. wantPackageB: "pkg",
  157. wantFiles: map[string]bool{
  158. "package/alpha/a.pb.go": true,
  159. "beta/b.pb.go": true,
  160. },
  161. }, {
  162. parameters: "Mbeta/b.proto=package/gamma",
  163. wantFiles: map[string]bool{
  164. "package/alpha/a.pb.go": true,
  165. "beta/b.pb.go": true,
  166. },
  167. wantPackageA: "alpha",
  168. wantPackageB: "test_beta",
  169. wantImportsA: map[string]bool{
  170. "github.com/golang/protobuf/proto": true,
  171. // Rewritten by the M parameter.
  172. "package/gamma": true,
  173. },
  174. }, {
  175. parameters: "import_prefix=prefix,Mbeta/b.proto=package/gamma",
  176. wantFiles: map[string]bool{
  177. "package/alpha/a.pb.go": true,
  178. "beta/b.pb.go": true,
  179. },
  180. wantPackageA: "alpha",
  181. wantPackageB: "test_beta",
  182. wantImportsA: map[string]bool{
  183. // import_prefix applies after M.
  184. "prefixpackage/gamma": true,
  185. },
  186. }, {
  187. parameters: "paths=source_relative",
  188. wantFiles: map[string]bool{
  189. "alpha/a.pb.go": true,
  190. "beta/b.pb.go": true,
  191. },
  192. wantPackageA: "alpha",
  193. wantPackageB: "test_beta",
  194. }, {
  195. parameters: "paths=source_relative,import_prefix=prefix",
  196. wantFiles: map[string]bool{
  197. // import_prefix doesn't affect filenames.
  198. "alpha/a.pb.go": true,
  199. "beta/b.pb.go": true,
  200. },
  201. wantPackageA: "alpha",
  202. wantPackageB: "test_beta",
  203. }} {
  204. name := test.parameters
  205. if name == "" {
  206. name = "defaults"
  207. }
  208. // TODO: Switch to t.Run when we no longer support Go 1.6.
  209. t.Logf("TEST: %v", name)
  210. workdir, err := ioutil.TempDir("", "proto-test")
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. defer os.RemoveAll(workdir)
  215. for _, dir := range []string{"alpha", "beta", "out"} {
  216. if err := os.MkdirAll(filepath.Join(workdir, dir), 0777); err != nil {
  217. t.Fatal(err)
  218. }
  219. }
  220. if err := ioutil.WriteFile(filepath.Join(workdir, "alpha", "a.proto"), []byte(aProto), 0666); err != nil {
  221. t.Fatal(err)
  222. }
  223. if err := ioutil.WriteFile(filepath.Join(workdir, "beta", "b.proto"), []byte(bProto), 0666); err != nil {
  224. t.Fatal(err)
  225. }
  226. protoc(t, []string{
  227. "-I" + workdir,
  228. "--go_out=" + test.parameters + ":" + filepath.Join(workdir, "out"),
  229. filepath.Join(workdir, "alpha", "a.proto"),
  230. })
  231. protoc(t, []string{
  232. "-I" + workdir,
  233. "--go_out=" + test.parameters + ":" + filepath.Join(workdir, "out"),
  234. filepath.Join(workdir, "beta", "b.proto"),
  235. })
  236. contents := make(map[string]string)
  237. gotFiles := make(map[string]bool)
  238. outdir := filepath.Join(workdir, "out")
  239. filepath.Walk(outdir, func(p string, info os.FileInfo, _ error) error {
  240. if info.IsDir() {
  241. return nil
  242. }
  243. base := filepath.Base(p)
  244. if base == "a.pb.go" || base == "b.pb.go" {
  245. b, err := ioutil.ReadFile(p)
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. contents[base] = string(b)
  250. }
  251. relPath, _ := filepath.Rel(outdir, p)
  252. gotFiles[relPath] = true
  253. return nil
  254. })
  255. for got := range gotFiles {
  256. if runtime.GOOS == "windows" {
  257. got = filepath.ToSlash(got)
  258. }
  259. if !test.wantFiles[got] {
  260. t.Errorf("unexpected output file: %v", got)
  261. }
  262. }
  263. for want := range test.wantFiles {
  264. if runtime.GOOS == "windows" {
  265. want = filepath.FromSlash(want)
  266. }
  267. if !gotFiles[want] {
  268. t.Errorf("missing output file: %v", want)
  269. }
  270. }
  271. gotPackageA, gotImports, err := parseFile(contents["a.pb.go"])
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. gotPackageB, _, err := parseFile(contents["b.pb.go"])
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. if got, want := gotPackageA, test.wantPackageA; want != got {
  280. t.Errorf("output file a.pb.go is package %q, want %q", got, want)
  281. }
  282. if got, want := gotPackageB, test.wantPackageB; want != got {
  283. t.Errorf("output file b.pb.go is package %q, want %q", got, want)
  284. }
  285. missingImport := false
  286. WantImport:
  287. for want := range test.wantImportsA {
  288. for _, imp := range gotImports {
  289. if `"`+want+`"` == imp {
  290. continue WantImport
  291. }
  292. }
  293. t.Errorf("output file a.pb.go does not contain expected import %q", want)
  294. missingImport = true
  295. }
  296. if missingImport {
  297. t.Error("got imports:")
  298. for _, imp := range gotImports {
  299. t.Errorf(" %v", imp)
  300. }
  301. }
  302. }
  303. }
  304. func TestPackageComment(t *testing.T) {
  305. workdir, err := ioutil.TempDir("", "proto-test")
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. defer os.RemoveAll(workdir)
  310. var packageRE = regexp.MustCompile(`(?m)^package .*`)
  311. for i, test := range []struct {
  312. goPackageOption string
  313. wantPackage string
  314. }{{
  315. goPackageOption: ``,
  316. wantPackage: `package proto_package`,
  317. }, {
  318. goPackageOption: `option go_package = "go_package";`,
  319. wantPackage: `package go_package`,
  320. }, {
  321. goPackageOption: `option go_package = "import/path/of/go_package";`,
  322. wantPackage: `package go_package // import "import/path/of/go_package"`,
  323. }, {
  324. goPackageOption: `option go_package = "import/path/of/something;go_package";`,
  325. wantPackage: `package go_package // import "import/path/of/something"`,
  326. }, {
  327. goPackageOption: `option go_package = "import_path;go_package";`,
  328. wantPackage: `package go_package // import "import_path"`,
  329. }} {
  330. srcName := filepath.Join(workdir, fmt.Sprintf("%d.proto", i))
  331. tgtName := filepath.Join(workdir, fmt.Sprintf("%d.pb.go", i))
  332. buf := &bytes.Buffer{}
  333. fmt.Fprintln(buf, `syntax = "proto3";`)
  334. fmt.Fprintln(buf, `package proto_package;`)
  335. fmt.Fprintln(buf, test.goPackageOption)
  336. if err := ioutil.WriteFile(srcName, buf.Bytes(), 0666); err != nil {
  337. t.Fatal(err)
  338. }
  339. protoc(t, []string{"-I" + workdir, "--go_out=paths=source_relative:" + workdir, srcName})
  340. out, err := ioutil.ReadFile(tgtName)
  341. if err != nil {
  342. t.Fatal(err)
  343. }
  344. pkg := packageRE.Find(out)
  345. if pkg == nil {
  346. t.Errorf("generated .pb.go contains no package line\n\nsource:\n%v\n\noutput:\n%v", buf.String(), string(out))
  347. continue
  348. }
  349. if got, want := string(pkg), test.wantPackage; got != want {
  350. t.Errorf("unexpected package statement with go_package = %q\n got: %v\nwant: %v", test.goPackageOption, got, want)
  351. }
  352. }
  353. }
  354. // parseFile returns a file's package name and a list of all packages it imports.
  355. func parseFile(source string) (packageName string, imports []string, err error) {
  356. fset := token.NewFileSet()
  357. f, err := parser.ParseFile(fset, "<source>", source, parser.ImportsOnly)
  358. if err != nil {
  359. return "", nil, err
  360. }
  361. for _, imp := range f.Imports {
  362. imports = append(imports, imp.Path.Value)
  363. }
  364. return f.Name.Name, imports, nil
  365. }
  366. func protoc(t *testing.T, args []string) {
  367. cmd := exec.Command("protoc", "--plugin=protoc-gen-go="+os.Args[0])
  368. cmd.Args = append(cmd.Args, args...)
  369. // We set the RUN_AS_PROTOC_GEN_GO environment variable to indicate that
  370. // the subprocess should act as a proto compiler rather than a test.
  371. cmd.Env = append(os.Environ(), "RUN_AS_PROTOC_GEN_GO=1")
  372. out, err := cmd.CombinedOutput()
  373. if len(out) > 0 || err != nil {
  374. t.Log("RUNNING: ", strings.Join(cmd.Args, " "))
  375. }
  376. if len(out) > 0 {
  377. t.Log(string(out))
  378. }
  379. if err != nil {
  380. t.Fatalf("protoc: %v", err)
  381. }
  382. }
  383. func hasReleaseTag(want string) bool {
  384. for _, tag := range build.Default.ReleaseTags {
  385. if tag == want {
  386. return true
  387. }
  388. }
  389. return false
  390. }