github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/proto/fileinfo_test.go (about)

     1  /* Copyright 2018 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 proto
    17  
    18  import (
    19  	"os"
    20  	"path/filepath"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestProtoRegexpGroupNames(t *testing.T) {
    26  	names := protoRe.SubexpNames()
    27  	nameMap := map[string]int{
    28  		"import":  importSubexpIndex,
    29  		"package": packageSubexpIndex,
    30  		"optkey":  optkeySubexpIndex,
    31  		"optval":  optvalSubexpIndex,
    32  		"service": serviceSubexpIndex,
    33  	}
    34  	for name, index := range nameMap {
    35  		if names[index] != name {
    36  			t.Errorf("proto regexp subexp %d is %s ; want %s", index, names[index], name)
    37  		}
    38  	}
    39  	if len(names)-1 != len(nameMap) {
    40  		t.Errorf("proto regexp has %d groups ; want %d", len(names), len(nameMap))
    41  	}
    42  }
    43  
    44  func TestProtoFileInfo(t *testing.T) {
    45  	for _, tc := range []struct {
    46  		desc, name, proto string
    47  		want              FileInfo
    48  	}{
    49  		{
    50  			desc:  "empty",
    51  			name:  "empty^file.proto",
    52  			proto: "",
    53  			want:  FileInfo{},
    54  		}, {
    55  			desc:  "simple package",
    56  			name:  "package.proto",
    57  			proto: "package foo;",
    58  			want: FileInfo{
    59  				PackageName: "foo",
    60  			},
    61  		}, {
    62  			desc:  "full package",
    63  			name:  "full.proto",
    64  			proto: "package foo.bar.baz;",
    65  			want: FileInfo{
    66  				PackageName: "foo.bar.baz",
    67  			},
    68  		}, {
    69  			desc: "import simple",
    70  			name: "imp.proto",
    71  			proto: `import 'single.proto';
    72  import "double.proto";`,
    73  			want: FileInfo{
    74  				Imports: []string{"double.proto", "single.proto"},
    75  			},
    76  		}, {
    77  			desc: "import quote",
    78  			name: "quote.proto",
    79  			proto: `import '""\".proto"';
    80  import "'.proto";`,
    81  			want: FileInfo{
    82  				Imports: []string{"\"\"\".proto\"", "'.proto"},
    83  			},
    84  		}, {
    85  			desc:  "import escape",
    86  			name:  "escape.proto",
    87  			proto: `import '\n\012\x0a.proto';`,
    88  			want: FileInfo{
    89  				Imports: []string{"\n\n\n.proto"},
    90  			},
    91  		}, {
    92  			desc: "import two",
    93  			name: "two.proto",
    94  			proto: `import "first.proto";
    95  import "second.proto";`,
    96  			want: FileInfo{
    97  				Imports: []string{"first.proto", "second.proto"},
    98  			},
    99  		}, {
   100  			desc:  "go_package",
   101  			name:  "gopkg.proto",
   102  			proto: `option go_package = "github.com/example/project;projectpb";`,
   103  			want: FileInfo{
   104  				Options: []Option{{Key: "go_package", Value: "github.com/example/project;projectpb"}},
   105  			},
   106  		}, {
   107  			desc:  "service def",
   108  			name:  "service.proto",
   109  			proto: `service ChatService {}`,
   110  			want: FileInfo{
   111  				HasServices: true,
   112  			},
   113  		}, {
   114  			desc:  "service as name",
   115  			name:  "service.proto",
   116  			proto: `message ServiceAccount { string service = 1; }`,
   117  			want: FileInfo{
   118  				HasServices: false,
   119  			},
   120  		},
   121  	} {
   122  		t.Run(tc.desc, func(t *testing.T) {
   123  			dir, err := os.MkdirTemp(os.Getenv("TEST_TEMPDIR"), "TestProtoFileinfo")
   124  			if err != nil {
   125  				t.Fatal(err)
   126  			}
   127  			defer os.RemoveAll(dir)
   128  			if err := os.WriteFile(filepath.Join(dir, tc.name), []byte(tc.proto), 0o600); err != nil {
   129  				t.Fatal(err)
   130  			}
   131  
   132  			got := protoFileInfo(dir, tc.name)
   133  
   134  			// Clear fields we don't care about for testing.
   135  			got = FileInfo{
   136  				PackageName: got.PackageName,
   137  				Imports:     got.Imports,
   138  				Options:     got.Options,
   139  				HasServices: got.HasServices,
   140  			}
   141  			if !reflect.DeepEqual(got, tc.want) {
   142  				t.Errorf("got %#v; want %#v", got, tc.want)
   143  			}
   144  		})
   145  	}
   146  }