github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/rule/rules_python/proto_py_library_test.go (about)

     1  package rules_python
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/bazelbuild/bazel-gazelle/config"
     8  	"github.com/bazelbuild/bazel-gazelle/rule"
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/stackb/rules_proto/pkg/protoc"
    11  )
    12  
    13  func makeTestProtoLibrary(cfg ...func(*rule.Rule)) *rule.Rule {
    14  	r := rule.NewRule("proto_library", "test_proto")
    15  	for _, fn := range cfg {
    16  		fn(r)
    17  	}
    18  	return r
    19  }
    20  
    21  func TestProtoPyLibraryRule(t *testing.T) {
    22  	for name, tc := range map[string]struct {
    23  		cfg  protoc.LanguageRuleConfig
    24  		pc   protoc.ProtocConfiguration
    25  		want string
    26  	}{
    27  		"degenerate": {
    28  			cfg: *protoc.NewLanguageRuleConfig(config.New(), "py"),
    29  			pc: protoc.ProtocConfiguration{
    30  				Library: protoc.NewOtherProtoLibrary(nil, makeTestProtoLibrary()),
    31  			},
    32  		},
    33  		"simple": {
    34  			cfg: *protoc.NewLanguageRuleConfig(config.New(), "py"),
    35  			pc: protoc.ProtocConfiguration{
    36  				Library: protoc.NewOtherProtoLibrary(nil, makeTestProtoLibrary()),
    37  				Plugins: []*protoc.PluginConfiguration{
    38  					{
    39  						Config: &protoc.LanguagePluginConfig{
    40  							Implementation: "builtin:python",
    41  						},
    42  						Outputs: []string{"test_pb2.py"},
    43  					},
    44  				},
    45  			},
    46  			want: `
    47  proto_py_library(
    48      name = "test_py_library",
    49      srcs = ["test_pb2.py"],
    50  )
    51  `,
    52  		},
    53  		"strip_import_prefix": {
    54  			cfg: *protoc.NewLanguageRuleConfig(config.New(), "py"),
    55  			pc: protoc.ProtocConfiguration{
    56  				Library: protoc.NewOtherProtoLibrary(nil, makeTestProtoLibrary(func(r *rule.Rule) {
    57  					r.SetAttr("strip_import_prefix", "/com/foo/")
    58  				})),
    59  				Plugins: []*protoc.PluginConfiguration{
    60  					{
    61  						Config: &protoc.LanguagePluginConfig{
    62  							Implementation: "builtin:python",
    63  						},
    64  						Outputs: []string{"test_pb2.py"},
    65  					},
    66  				},
    67                  Rel: "com/foo/baz/qux/v1",
    68  			},
    69  			want: `
    70  proto_py_library(
    71      name = "test_py_library",
    72      srcs = ["test_pb2.py"],
    73      imports = ["../../.."],
    74  )
    75  `,
    76  		},
    77  	} {
    78  		t.Run(name, func(t *testing.T) {
    79  			lib := protoPyLibrary{}
    80  			impl := lib.ProvideRule(&tc.cfg, &tc.pc)
    81  			var got string
    82  			if impl != nil {
    83  				rule := impl.Rule()
    84  				got = printRules(rule)
    85  			}
    86  			if diff := cmp.Diff(strings.TrimSpace(tc.want), strings.TrimSpace(got)); diff != "" {
    87  				t.Errorf("(-want +got):\n%s", diff)
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func printRules(rules ...*rule.Rule) string {
    94  	file := rule.EmptyFile("", "")
    95  	for _, r := range rules {
    96  		r.Insert(file)
    97  	}
    98  	return string(file.Format())
    99  }