blob: 7646716e6285f3609d072bce3bce9a253064604b [file] [log] [blame]
Damien Neil220c2022018-08-15 11:24:18 -07001// 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
Joe Tsai42fa50d2018-10-15 17:34:43 -07005// +build golden
Damien Neil2dc67182018-09-21 15:03:34 -07006
Damien Neil220c2022018-08-15 11:24:18 -07007package main
8
9import (
Damien Neil162c1272018-10-04 12:42:37 -070010 "bytes"
Damien Neil220c2022018-08-15 11:24:18 -070011 "flag"
Damien Neil162c1272018-10-04 12:42:37 -070012 "io/ioutil"
13 "os"
14 "path/filepath"
Damien Neil220c2022018-08-15 11:24:18 -070015 "testing"
Damien Neil2dc67182018-09-21 15:03:34 -070016
Damien Neil162c1272018-10-04 12:42:37 -070017 "github.com/golang/protobuf/proto"
18 descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
Damien Neil2dc67182018-09-21 15:03:34 -070019 "github.com/golang/protobuf/v2/internal/protogen/goldentest"
Damien Neil220c2022018-08-15 11:24:18 -070020)
21
22// Set --regenerate to regenerate the golden files.
23var regenerate = flag.Bool("regenerate", false, "regenerate golden files")
24
Damien Neil220c2022018-08-15 11:24:18 -070025func init() {
Damien Neil2dc67182018-09-21 15:03:34 -070026 goldentest.Plugin(main)
Damien Neil220c2022018-08-15 11:24:18 -070027}
28
29func TestGolden(t *testing.T) {
Damien Neil2dc67182018-09-21 15:03:34 -070030 goldentest.Run(t, *regenerate)
Damien Neil220c2022018-08-15 11:24:18 -070031}
Damien Neil162c1272018-10-04 12:42:37 -070032
33func TestAnnotations(t *testing.T) {
34 workdir, err := ioutil.TempDir("", "proto-test")
35 if err != nil {
36 t.Fatal(err)
37 }
38 defer os.RemoveAll(workdir)
39
40 goldentest.Protoc(t, []string{"--go_out=paths=source_relative,annotate_code:" + workdir, "-Itestdata/annotations", "testdata/annotations/annotations.proto"})
41 sourceFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go"))
42 if err != nil {
43 t.Fatal(err)
44 }
45 metaFile, err := ioutil.ReadFile(filepath.Join(workdir, "annotations.pb.go.meta"))
46 if err != nil {
47 t.Fatal(err)
48 }
49 gotInfo := &descpb.GeneratedCodeInfo{}
50 if err := proto.UnmarshalText(string(metaFile), gotInfo); err != nil {
51 t.Fatalf("can't parse meta file: %v", err)
52 }
53
54 wantInfo := &descpb.GeneratedCodeInfo{}
55 for _, want := range []struct {
56 prefix, text, suffix string
57 path []int32
58 }{{
59 "type ", "AnnotationsTestEnum", " int32",
60 []int32{5 /* enum_type */, 0},
61 }, {
62 "\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0",
63 []int32{5 /* enum_type */, 0, 2 /* value */, 0},
64 }, {
65 "type ", "AnnotationsTestMessage", " struct {",
66 []int32{4 /* message_type */, 0},
67 }, {
68 "\t", "AnnotationsTestField", " ",
69 []int32{4 /* message_type */, 0, 2 /* field */, 0},
70 }, {
71 "func (m *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {",
72 []int32{4 /* message_type */, 0, 2 /* field */, 0},
73 }} {
74 s := want.prefix + want.text + want.suffix
75 pos := bytes.Index(sourceFile, []byte(s))
76 if pos < 0 {
77 t.Errorf("source file does not contain: %v", s)
78 continue
79 }
80 begin := pos + len(want.prefix)
81 end := begin + len(want.text)
82 wantInfo.Annotation = append(wantInfo.Annotation, &descpb.GeneratedCodeInfo_Annotation{
83 Path: want.path,
84 Begin: proto.Int32(int32(begin)),
85 End: proto.Int32(int32(end)),
86 SourceFile: proto.String("annotations.proto"),
87 })
88 }
89 if !proto.Equal(gotInfo, wantInfo) {
90 t.Errorf("unexpected annotations for annotations.proto; got:\n%v\nwant:\n%v",
91 proto.MarshalTextString(gotInfo), proto.MarshalTextString(wantInfo))
92 }
93}