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

     1  package protoc
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  )
     8  
     9  func TestMergeSources(t *testing.T) {
    10  	for name, tc := range map[string]struct {
    11  		workDir           string
    12  		rel               string
    13  		plugins           []*PluginConfiguration
    14  		stripImportPrefix string
    15  		wantOutputs       []string
    16  		wantMappings      map[string]string
    17  	}{
    18  		"empty": {
    19  			wantOutputs:  []string{},
    20  			wantMappings: map[string]string{},
    21  		},
    22  		"root package, simple case": {
    23  			rel: "",
    24  			plugins: withPluginConfigurations(&PluginConfiguration{
    25  				Outputs: listOf("foo.py"),
    26  			}),
    27  			wantOutputs:  listOf("foo.py"),
    28  			wantMappings: map[string]string{},
    29  		},
    30  		"root package, go_package case": {
    31  			rel: "",
    32  			plugins: withPluginConfigurations(&PluginConfiguration{
    33  				Outputs:  listOf("foo.py"),
    34  				Mappings: map[string]string{"foo.py": "com/github/example/foo.py"},
    35  			}),
    36  			wantOutputs:  listOf("foo.py"),
    37  			wantMappings: map[string]string{"foo.py": "com/github/example/foo.py"},
    38  		},
    39  		"child package, simple case": {
    40  			rel: "test/proto",
    41  			plugins: withPluginConfigurations(&PluginConfiguration{
    42  				Outputs: listOf("test/proto/foo.py"),
    43  			}),
    44  			wantOutputs:  listOf("foo.py"),
    45  			wantMappings: map[string]string{},
    46  		},
    47  		"child package, mapped case": {
    48  			rel: "test/proto",
    49  			plugins: withPluginConfigurations(&PluginConfiguration{
    50  				Outputs: listOf("foo.py"),
    51  			}),
    52  			wantOutputs:  listOf("foo.py"),
    53  			wantMappings: map[string]string{"foo.py": "foo.py"},
    54  		},
    55  		"external workspace, mapped case": {
    56  			workDir: "/path/to/external/googleapis",
    57  			rel:     "test/proto",
    58  			plugins: withPluginConfigurations(&PluginConfiguration{
    59  				Outputs: listOf("foo.py"),
    60  			}),
    61  			wantOutputs:  listOf("foo.py"),
    62  			wantMappings: map[string]string{"foo.py": "foo.py"},
    63  		},
    64  		"child package, with strip_import_prefix": {
    65  			rel: "test/proto",
    66  			plugins: withPluginConfigurations(&PluginConfiguration{
    67  				Outputs: listOf("test/proto/foo.py"),
    68  			}),
    69  			stripImportPrefix: "/test",
    70  			wantOutputs:       listOf("foo.py"),
    71  			wantMappings:      map[string]string{"foo.py": "/proto/foo.py"},
    72  		},
    73  	} {
    74  		t.Run(name, func(t *testing.T) {
    75  			srcs, mappings := mergeSources(tc.workDir, tc.rel, tc.plugins, tc.stripImportPrefix)
    76  
    77  			if diff := cmp.Diff(tc.wantOutputs, srcs); diff != "" {
    78  				t.Errorf("srcs (-want +got):\n%s", diff)
    79  			}
    80  
    81  			if diff := cmp.Diff(tc.wantMappings, mappings); diff != "" {
    82  				t.Errorf("mappings (-want +got):\n%s", diff)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func withPluginConfigurations(cc ...*PluginConfiguration) []*PluginConfiguration {
    89  	return cc
    90  }
    91  
    92  func listOf(ss ...string) []string {
    93  	return ss
    94  }