github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/packages/fileinfo_proto_test.go (about)

     1  /* Copyright 2017 The Bazel Authors. All rights reserved.
     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  */
    15  
    16  package packages
    17  
    18  import (
    19  	"io/ioutil"
    20  	"os"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/bazelbuild/bazel-gazelle/internal/config"
    25  )
    26  
    27  func TestProtoRegexpGroupNames(t *testing.T) {
    28  	names := protoRe.SubexpNames()
    29  	nameMap := map[string]int{
    30  		"import":     importSubexpIndex,
    31  		"package":    packageSubexpIndex,
    32  		"go_package": goPackageSubexpIndex,
    33  		"service":    serviceSubexpIndex,
    34  	}
    35  	for name, index := range nameMap {
    36  		if names[index] != name {
    37  			t.Errorf("proto regexp subexp %d is %s ; want %s", index, names[index], name)
    38  		}
    39  	}
    40  	if len(names)-1 != len(nameMap) {
    41  		t.Errorf("proto regexp has %d groups ; want %d", len(names), len(nameMap))
    42  	}
    43  }
    44  
    45  func TestProtoFileInfo(t *testing.T) {
    46  	c := &config.Config{}
    47  	dir := "."
    48  	rel := ""
    49  	for _, tc := range []struct {
    50  		desc, name, proto string
    51  		want              fileInfo
    52  	}{
    53  		{
    54  			desc:  "empty",
    55  			name:  "empty^file.proto",
    56  			proto: "",
    57  			want: fileInfo{
    58  				packageName: "empty_file",
    59  			},
    60  		}, {
    61  			desc:  "simple package",
    62  			name:  "package.proto",
    63  			proto: "package foo;",
    64  			want: fileInfo{
    65  				packageName: "foo",
    66  			},
    67  		}, {
    68  			desc:  "full package",
    69  			name:  "full.proto",
    70  			proto: "package foo.bar.baz;",
    71  			want: fileInfo{
    72  				packageName: "foo_bar_baz",
    73  			},
    74  		}, {
    75  			desc: "import simple",
    76  			name: "imp.proto",
    77  			proto: `import 'single.proto';
    78  import "double.proto";`,
    79  			want: fileInfo{
    80  				packageName: "imp",
    81  				imports:     []string{"double.proto", "single.proto"},
    82  			},
    83  		}, {
    84  			desc: "import quote",
    85  			name: "quote.proto",
    86  			proto: `import '""\".proto"';
    87  import "'.proto";`,
    88  			want: fileInfo{
    89  				packageName: "quote",
    90  				imports:     []string{"\"\"\".proto\"", "'.proto"},
    91  			},
    92  		}, {
    93  			desc:  "import escape",
    94  			name:  "escape.proto",
    95  			proto: `import '\n\012\x0a.proto';`,
    96  			want: fileInfo{
    97  				packageName: "escape",
    98  				imports:     []string{"\n\n\n.proto"},
    99  			},
   100  		}, {
   101  			desc: "import two",
   102  			name: "two.proto",
   103  			proto: `import "first.proto";
   104  import "second.proto";`,
   105  			want: fileInfo{
   106  				packageName: "two",
   107  				imports:     []string{"first.proto", "second.proto"},
   108  			},
   109  		}, {
   110  			desc:  "go_package",
   111  			name:  "gopkg.proto",
   112  			proto: `option go_package = "github.com/example/project;projectpb";`,
   113  			want: fileInfo{
   114  				packageName: "projectpb",
   115  				importPath:  "github.com/example/project",
   116  			},
   117  		}, {
   118  			desc:  "go_package_simple",
   119  			name:  "gopkg_simple.proto",
   120  			proto: `option go_package = "bar";`,
   121  			want: fileInfo{
   122  				packageName: "bar",
   123  				importPath:  "",
   124  			},
   125  		}, {
   126  			desc:  "service",
   127  			name:  "service.proto",
   128  			proto: `service ChatService {}`,
   129  			want: fileInfo{
   130  				packageName: "service",
   131  				hasServices: true,
   132  			},
   133  		},
   134  	} {
   135  		t.Run(tc.desc, func(t *testing.T) {
   136  			if err := ioutil.WriteFile(tc.name, []byte(tc.proto), 0600); err != nil {
   137  				t.Fatal(err)
   138  			}
   139  			defer os.Remove(tc.name)
   140  
   141  			got := protoFileInfo(c, dir, rel, tc.name)
   142  
   143  			// Clear fields we don't care about for testing.
   144  			got = fileInfo{
   145  				packageName: got.packageName,
   146  				imports:     got.imports,
   147  				importPath:  got.importPath,
   148  				hasServices: got.hasServices,
   149  			}
   150  			if !reflect.DeepEqual(got, tc.want) {
   151  				t.Errorf("got %#v; want %#v", got, tc.want)
   152  			}
   153  		})
   154  	}
   155  }