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

     1  package rules_python
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/bazelbuild/bazel-gazelle/resolve"
     9  	"github.com/bazelbuild/bazel-gazelle/rule"
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/stackb/rules_proto/pkg/protoc"
    12  )
    13  
    14  func TestMaybeStripImportPrefix(t *testing.T) {
    15  	for name, tc := range map[string]struct {
    16  		specs             []resolve.ImportSpec
    17  		stripImportPrefix string
    18  		want              []resolve.ImportSpec
    19  	}{
    20  		"degenerate": {},
    21  		"empty string": {
    22  			specs: []resolve.ImportSpec{
    23  				{Imp: "foo/bar/baz.proto"},
    24  			},
    25  			want: []resolve.ImportSpec{
    26  				{Imp: "foo/bar/baz.proto"},
    27  			},
    28  		},
    29  		"non-matching-prefix": {
    30  			specs: []resolve.ImportSpec{
    31  				{Imp: "foo/bar/baz.proto"},
    32  			},
    33  			stripImportPrefix: "nope",
    34  			want: []resolve.ImportSpec{
    35  				{Imp: "foo/bar/baz.proto"},
    36  			},
    37  		},
    38  		"matching-prefix-absolute": {
    39  			specs: []resolve.ImportSpec{
    40  				{Imp: "foo/bar/baz.proto"},
    41  			},
    42  			stripImportPrefix: "/foo/bar",
    43  			want: []resolve.ImportSpec{
    44  				{Imp: "baz.proto"},
    45  			},
    46  		},
    47  	} {
    48  		t.Run(name, func(t *testing.T) {
    49  			got := maybeStripImportPrefix(tc.specs, tc.stripImportPrefix)
    50  			if diff := cmp.Diff(tc.want, got); diff != "" {
    51  				t.Errorf("(-want +got):\n%s", diff)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestImports(t *testing.T) {
    58  	kind := "mykind"
    59  	suffix := "_suffix"
    60  	pkg := "mypkg"
    61  	protoName := "test"
    62  	cases := []struct {
    63  		Name        string
    64  		Outputs     []string
    65  		WantImports []resolve.ImportSpec
    66  	}{{
    67  		Name: "Empty",
    68  		// If for some reason, no python files were output...
    69  		Outputs: []string{},
    70  		// Always include the output from the proto_library
    71  		WantImports: []resolve.ImportSpec{{Lang: kind, Imp: fmt.Sprintf("%s/%s", pkg, protoName)}},
    72  	}, {
    73  		Name:    "One output",
    74  		Outputs: []string{path.Join(pkg, "test_pb2.py")},
    75  		WantImports: []resolve.ImportSpec{
    76  			{Lang: kind, Imp: fmt.Sprintf("%s/%s", pkg, protoName)},
    77  			{Lang: "py", Imp: fmt.Sprintf("%s.%s_pb2", pkg, protoName)},
    78  		},
    79  	}}
    80  
    81  	for _, c := range cases {
    82  		t.Run(c.Name, func(t *testing.T) {
    83  			py := &PyLibrary{
    84  				KindName:       kind,
    85  				RuleNameSuffix: suffix,
    86  				Outputs:        c.Outputs,
    87  				Resolver:       protoc.ResolveDepsAttr("deps", true),
    88  			}
    89  			protoLib := protoc.NewOtherProtoLibrary(&rule.File{}, rule.NewRule("proto_library", protoName+"_proto"), protoc.NewFile(pkg, protoName))
    90  			r := rule.NewRule(kind, "test"+suffix)
    91  			r.SetPrivateAttr(protoc.ProtoLibraryKey, protoLib)
    92  			imps := py.Imports(nil, r, &rule.File{Pkg: pkg})
    93  			if diff := cmp.Diff(imps, c.WantImports); diff != "" {
    94  				t.Fatalf("import mismatch: (-got, +want): %s", diff)
    95  			}
    96  		})
    97  	}
    98  }