github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/rewrite_test.go (about)

     1  package protoc
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bazelbuild/bazel-gazelle/rule"
     7  	"github.com/emicklei/proto"
     8  )
     9  
    10  func TestParseRewrite(t *testing.T) {
    11  	for name, tc := range map[string]struct {
    12  		spec string
    13  		in   string
    14  		want string
    15  	}{
    16  		"basic regexp": {
    17  			spec: "a b",
    18  			in:   "a",
    19  			want: "b",
    20  		},
    21  		"canonical example": {
    22  			spec: "google/protobuf/([a-z]+).proto @org_golang_google_protobuf//types/known/${1}pb",
    23  			in:   "google/protobuf/empty.proto",
    24  			want: "@org_golang_google_protobuf//types/known/emptypb",
    25  		},
    26  	} {
    27  		t.Run(name, func(t *testing.T) {
    28  			rw, err := ParseRewrite(tc.spec)
    29  			if err != nil {
    30  				t.Fatal(err)
    31  			}
    32  			got := rw.ReplaceAll(tc.in)
    33  			if got != tc.want {
    34  				t.Errorf("replaceall: want %s, got %s", tc.want, got)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func TestResolveLibraryRewrites(t *testing.T) {
    41  	for name, tc := range map[string]struct {
    42  		imports  []string
    43  		rewrites []string
    44  		want     []string
    45  	}{
    46  		"single match": {
    47  			imports:  []string{"google/protobuf/any.proto"},
    48  			rewrites: []string{"google/protobuf/([a-z]+).proto @org_golang_google_protobuf//types/known/${1}pb"},
    49  			want:     []string{"@org_golang_google_protobuf//types/known/anypb"},
    50  		},
    51  		"multiple match": {
    52  			imports: []string{
    53  				"google/protobuf/any.proto",
    54  				"google/protobuf/empty.proto",
    55  				"not/matched.proto",
    56  			},
    57  			rewrites: []string{"google/protobuf/([a-z]+).proto @org_golang_google_protobuf//types/known/${1}pb"},
    58  			want: []string{
    59  				"@org_golang_google_protobuf//types/known/anypb",
    60  				"@org_golang_google_protobuf//types/known/emptypb",
    61  			},
    62  		},
    63  	} {
    64  		t.Run(name, func(t *testing.T) {
    65  			file := NewFile("fake/rel/dir", "foo")
    66  			for _, v := range tc.imports {
    67  				file.imports = append(file.imports, proto.Import{Filename: v})
    68  			}
    69  			lib := NewOtherProtoLibrary(
    70  				rule.EmptyFile("", "fake/rel/dir"),
    71  				rule.NewRule("proto_library", "foo_proto_library"),
    72  				file)
    73  			rewrites := make([]Rewrite, len(tc.rewrites))
    74  			for i, d := range tc.rewrites {
    75  				rw, err := ParseRewrite(d)
    76  				if err != nil {
    77  					t.Fatal("parse", d, err)
    78  				}
    79  				rewrites[i] = *rw
    80  			}
    81  			got := ResolveLibraryRewrites(rewrites, lib)
    82  			if len(tc.want) != len(got) {
    83  				t.Fatalf("want %d, got %d (%v -vs- %v) ", len(tc.want), len(got), tc.want, got)
    84  			}
    85  			for i, actual := range got {
    86  				expected := tc.want[i]
    87  				if actual != expected {
    88  					t.Errorf("item %d: want %s, got %s", i, expected, actual)
    89  				}
    90  			}
    91  		})
    92  	}
    93  }