Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "flag" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 10 | "io/ioutil" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "regexp" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | ) |
| 18 | |
| 19 | // Set --regenerate to regenerate the golden files. |
| 20 | var regenerate = flag.Bool("regenerate", false, "regenerate golden files") |
| 21 | |
| 22 | // When the environment variable RUN_AS_PROTOC_GEN_GO is set, we skip running |
| 23 | // tests and instead act as protoc-gen-go. This allows the test binary to |
| 24 | // pass itself to protoc. |
| 25 | func init() { |
| 26 | if os.Getenv("RUN_AS_PROTOC_GEN_GO") != "" { |
| 27 | main() |
| 28 | os.Exit(0) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestGolden(t *testing.T) { |
| 33 | workdir, err := ioutil.TempDir("", "proto-test") |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | defer os.RemoveAll(workdir) |
| 38 | |
| 39 | // Find all the proto files we need to compile. We assume that each directory |
| 40 | // contains the files for a single package. |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 41 | packages := map[string][]string{} |
| 42 | err = filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error { |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 43 | if !strings.HasSuffix(path, ".proto") { |
| 44 | return nil |
| 45 | } |
| 46 | dir := filepath.Dir(path) |
| 47 | packages[dir] = append(packages[dir], path) |
| 48 | return nil |
| 49 | }) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 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 | |
| 61 | // Compare each generated file to the golden version. |
| 62 | filepath.Walk(workdir, func(genPath string, info os.FileInfo, _ error) error { |
| 63 | if info.IsDir() { |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // For each generated file, figure out the path to the corresponding |
| 68 | // golden file in the testdata directory. |
| 69 | relPath, err := filepath.Rel(workdir, genPath) |
| 70 | if err != nil { |
| 71 | t.Errorf("filepath.Rel(%q, %q): %v", workdir, genPath, err) |
| 72 | return nil |
| 73 | } |
| 74 | if filepath.SplitList(relPath)[0] == ".." { |
| 75 | t.Errorf("generated file %q is not relative to %q", genPath, workdir) |
| 76 | } |
| 77 | goldenPath := filepath.Join("testdata", relPath) |
| 78 | |
| 79 | got, err := ioutil.ReadFile(genPath) |
| 80 | if err != nil { |
| 81 | t.Error(err) |
| 82 | return nil |
| 83 | } |
| 84 | if *regenerate { |
| 85 | // If --regenerate set, just rewrite the golden files. |
| 86 | err := ioutil.WriteFile(goldenPath, got, 0666) |
| 87 | if err != nil { |
| 88 | t.Error(err) |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | want, err := ioutil.ReadFile(goldenPath) |
| 94 | if err != nil { |
| 95 | t.Error(err) |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | want = fdescRE.ReplaceAll(want, nil) |
| 100 | got = fdescRE.ReplaceAll(got, nil) |
| 101 | if bytes.Equal(got, want) { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | cmd := exec.Command("diff", "-u", goldenPath, genPath) |
| 106 | out, _ := cmd.CombinedOutput() |
| 107 | t.Errorf("golden file differs: %v\n%v", relPath, string(out)) |
| 108 | return nil |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | var fdescRE = regexp.MustCompile(`(?ms)^var fileDescriptor.*}`) |
| 113 | |
| 114 | func protoc(t *testing.T, args []string) { |
| 115 | cmd := exec.Command("protoc", "--plugin=protoc-gen-go="+os.Args[0]) |
| 116 | cmd.Args = append(cmd.Args, args...) |
| 117 | // We set the RUN_AS_PROTOC_GEN_GO environment variable to indicate that |
| 118 | // the subprocess should act as a proto compiler rather than a test. |
| 119 | cmd.Env = append(os.Environ(), "RUN_AS_PROTOC_GEN_GO=1") |
| 120 | out, err := cmd.CombinedOutput() |
| 121 | if len(out) > 0 || err != nil { |
| 122 | t.Log("RUNNING: ", strings.Join(cmd.Args, " ")) |
| 123 | } |
| 124 | if len(out) > 0 { |
| 125 | t.Log(string(out)) |
| 126 | } |
| 127 | if err != nil { |
| 128 | t.Fatalf("protoc: %v", err) |
| 129 | } |
| 130 | } |