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

     1  package protoc
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bazelbuild/bazel-gazelle/config"
     7  	"github.com/bazelbuild/bazel-gazelle/label"
     8  )
     9  
    10  func TestStripRel(t *testing.T) {
    11  	for name, tc := range map[string]struct {
    12  		rel  string
    13  		in   string
    14  		want string
    15  	}{
    16  		"empty": {
    17  			rel:  "",
    18  			in:   "",
    19  			want: "",
    20  		},
    21  		"match": {
    22  			rel:  "proto",
    23  			in:   "proto/foo.txt",
    24  			want: "foo.txt",
    25  		},
    26  		"no-match": {
    27  			rel:  "proto",
    28  			in:   "foo/proto.txt",
    29  			want: "foo/proto.txt",
    30  		},
    31  	} {
    32  		t.Run(name, func(t *testing.T) {
    33  			got := StripRel(tc.rel, tc.in)
    34  			if got != tc.want {
    35  				t.Errorf("striprel: want %s, got %s", tc.want, got)
    36  			}
    37  		})
    38  	}
    39  }
    40  
    41  func TestIsSameImport(t *testing.T) {
    42  	for name, tc := range map[string]struct {
    43  		from, to label.Label
    44  		config   *config.Config
    45  		want     bool
    46  	}{
    47  		"same": {
    48  			from:   label.New("", "pkg", "a"),
    49  			to:     label.New("", "pkg", "a"),
    50  			config: &config.Config{RepoName: ""},
    51  			want:   true,
    52  		},
    53  		"different": {
    54  			from:   label.New("", "pkg", "a"),
    55  			to:     label.New("", "pkg", "b"),
    56  			config: &config.Config{RepoName: ""},
    57  			want:   false,
    58  		},
    59  		"same repo": {
    60  			from:   label.New("foo", "pkg", "a"),
    61  			to:     label.New("", "pkg", "a"),
    62  			config: &config.Config{RepoName: "foo"},
    63  			want:   true,
    64  		},
    65  		"different repo": {
    66  			from:   label.New("foo", "pkg", "a"),
    67  			to:     label.New("bar", "pkg", "a"),
    68  			config: &config.Config{RepoName: "foo"},
    69  			want:   false,
    70  		},
    71  	} {
    72  		t.Run(name, func(t *testing.T) {
    73  			got := isSameImport(tc.config, tc.from, tc.to)
    74  			if got != tc.want {
    75  				t.Errorf("isSameImport: want %t, got %t", tc.want, got)
    76  			}
    77  		})
    78  	}
    79  }