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

     1  package protoc
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/bazelbuild/bazel-gazelle/label"
     9  	"github.com/google/go-cmp/cmp"
    10  )
    11  
    12  func TestLoadStarlarkPlugin(t *testing.T) {
    13  	for name, tc := range map[string]struct {
    14  		code        string
    15  		ctx         *PluginContext
    16  		wantErr     error
    17  		wantPrinted string
    18  		want        *PluginConfiguration
    19  	}{
    20  		"degenerate": {
    21  			wantErr: fmt.Errorf(`test.star: plugin "test" was never declared`),
    22  		},
    23  		"wrong plugin name": {
    24  			code: `
    25  protoc.Plugin(
    26  	name = "not-test",
    27  	configure = lambda ctx: None,
    28  )
    29  			`,
    30  			wantErr: fmt.Errorf(`test.star: plugin "test" was never declared`),
    31  		},
    32  		"missing configure attribute": {
    33  			code: `
    34  protoc.Plugin(
    35  	name = "test", 
    36  )
    37  			`,
    38  			wantErr: fmt.Errorf(`test.star: eval: Plugin: missing argument for configure`),
    39  		},
    40  		"configure attribute not callable": {
    41  			code: `
    42  protoc.Plugin(
    43  	name = "test", 
    44  	configure = "not-callable",
    45  )
    46  			`,
    47  			wantErr: fmt.Errorf(`test.star: eval: Plugin: for parameter "configure": got string, want callable`),
    48  		},
    49  		"simple": {
    50  			code: `
    51  def configure(ctx):
    52  	print(ctx)
    53  	return protoc.PluginConfiguration(
    54  		label = "//%s:python_plugin" % ctx.rel,
    55  		outputs = ["foo.py", "bar.py"],
    56  	)
    57      
    58  protoc.Plugin(
    59  	name = "test", 
    60  	configure = configure,
    61  )
    62  `,
    63  			ctx: &PluginContext{
    64  				Rel: "mypkg",
    65  			},
    66  			want: &PluginConfiguration{
    67  				Label:   label.New("", "mypkg", "python_plugin"),
    68  				Outputs: []string{"foo.py", "bar.py"},
    69  				Options: []string{},
    70  			},
    71  			wantPrinted: `PluginContext(package_config = PackageConfig(config = Config(repo_name = "", repo_root = "", work_dir = "")), plugin_config = LanguagePluginConfig(deps = [], enabled = False, implementation = "", label = "", name = "", options = []), proto_library = ProtoLibrary(base_name = "", deps = [], files = [], imports = [], name = "", srcs = [], strip_import_prefix = ""), rel = "mypkg")` + "\n",
    72  		},
    73  	} {
    74  		t.Run(name, func(t *testing.T) {
    75  			var err error
    76  			var gotPrinted strings.Builder
    77  			var plugin Plugin
    78  			plugin, err = loadStarlarkPlugin("test", "test.star", strings.NewReader(tc.code), func(msg string) {
    79  				gotPrinted.WriteString(msg)
    80  				gotPrinted.Write([]byte{'\n'})
    81  			}, func(configureErr error) {
    82  				err = configureErr
    83  			})
    84  			if err != nil {
    85  				if tc.wantErr != nil {
    86  					if diff := cmp.Diff(tc.wantErr.Error(), err.Error()); diff != "" {
    87  						t.Fatalf("StarlarkPlugin.Configure error (-want +got):\n%s", diff)
    88  					}
    89  					return
    90  				} else {
    91  					t.Fatalf("StarlarkPlugin.Configure error: %v", err)
    92  				}
    93  			}
    94  
    95  			got := plugin.Configure(tc.ctx)
    96  			t.Log(gotPrinted.String())
    97  			if diff := cmp.Diff(tc.wantPrinted, gotPrinted.String()); diff != "" {
    98  				t.Errorf("StarlarkPlugin.Configure print (-want +got):\n%s", diff)
    99  			}
   100  
   101  			if diff := cmp.Diff(tc.want, got); diff != "" {
   102  				t.Errorf("StarlarkPlugin.Configure (-want +got):\n%s", diff)
   103  			}
   104  		})
   105  	}
   106  }