github.com/please-build/go-rules/tools/please_go@v0.0.0-20240319165128-ea27d6f5caba/generate/generate_test.go (about)

     1  package generate
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestTrimPath(t *testing.T) {
     9  	tests := []struct {
    10  		name     string
    11  		base     string
    12  		target   string
    13  		expected string
    14  	}{
    15  		{
    16  			name:     "trims base path",
    17  			base:     "third_party/go/_foo#dl",
    18  			target:   "third_party/go/_foo#dl/foo",
    19  			expected: "foo",
    20  		},
    21  		{
    22  			name:     "returns target if base is shorter",
    23  			base:     "foo/bar/baz",
    24  			target:   "foo/bar",
    25  			expected: "foo/bar",
    26  		},
    27  		{
    28  			name:     "returns target if not in base",
    29  			base:     "foo/bar",
    30  			target:   "bar/baz",
    31  			expected: "bar/baz",
    32  		},
    33  	}
    34  
    35  	for _, test := range tests {
    36  		t.Run(test.name, func(t *testing.T) {
    37  			assert.Equal(t, test.expected, trimPath(test.target, test.base))
    38  		})
    39  	}
    40  }
    41  
    42  func TestBuildTarget(t *testing.T) {
    43  	tests := []struct {
    44  		test               string
    45  		name, pkg, subrepo string
    46  		expected           string
    47  	}{
    48  		{
    49  			test:     "fully qualified",
    50  			subrepo:  "subrepo",
    51  			pkg:      "pkg",
    52  			name:     "name",
    53  			expected: "///subrepo//pkg:name",
    54  		},
    55  		{
    56  			test:     "fully qualified local package",
    57  			subrepo:  "",
    58  			pkg:      "pkg",
    59  			name:     "name",
    60  			expected: "//pkg:name",
    61  		},
    62  		{
    63  			test:     "root package",
    64  			subrepo:  "",
    65  			pkg:      "",
    66  			name:     "name",
    67  			expected: "//:name",
    68  		},
    69  		{
    70  			test:     "root package via .",
    71  			subrepo:  "",
    72  			pkg:      ".",
    73  			name:     "name",
    74  			expected: "//:name",
    75  		},
    76  		{
    77  			test:     "root package in subrepo",
    78  			subrepo:  "subrepo",
    79  			pkg:      "",
    80  			name:     "name",
    81  			expected: "///subrepo//:name",
    82  		},
    83  		{
    84  			test:     "pkg base matches name",
    85  			subrepo:  "",
    86  			pkg:      "foo",
    87  			name:     "foo",
    88  			expected: "//foo",
    89  		},
    90  	}
    91  
    92  	for _, test := range tests {
    93  		t.Run(test.test, func(t *testing.T) {
    94  			assert.Equal(t, test.expected, buildTarget(test.name, test.pkg, test.subrepo))
    95  		})
    96  	}
    97  }
    98  
    99  func TestDepTarget(t *testing.T) {
   100  	tests := []struct {
   101  		name         string
   102  		deps         []string
   103  		importTarget string
   104  		expected     string
   105  	}{
   106  		{
   107  			name:         "resolves local import",
   108  			importTarget: "github.com/this/module/foo",
   109  			expected:     "//foo",
   110  		},
   111  		{
   112  			name:         "resolves local import in base",
   113  			importTarget: "github.com/this/module",
   114  			expected:     "//:module",
   115  		},
   116  		{
   117  			name:         "resolves import to another module",
   118  			importTarget: "github.com/some/module/foo",
   119  			expected:     "///third_party/go/github.com_some_module//foo",
   120  			deps:         []string{"github.com/some/module"},
   121  		},
   122  		{
   123  			name:         "resolves import to longest match",
   124  			importTarget: "github.com/some/module/foo/bar",
   125  			expected:     "///third_party/go/github.com_some_module_foo//bar",
   126  			deps:         []string{"github.com/some/module", "github.com/some/module/foo"},
   127  		},
   128  		{
   129  			name:         "root package matches module base",
   130  			importTarget: "github.com/some/module",
   131  			expected:     "///third_party/go/github.com_some_module//:module",
   132  			deps:         []string{"github.com/some/module"},
   133  		},
   134  		{
   135  			name:         "replaces all with lib locally",
   136  			importTarget: "github.com/this/module/all",
   137  			expected:     "//all:lib",
   138  		},
   139  		{
   140  			name:         "replaces all with lib in another repo",
   141  			importTarget: "github.com/some/module/all",
   142  			expected:     "///third_party/go/github.com_some_module//all:lib",
   143  			deps:         []string{"github.com/some/module"},
   144  		},
   145  	}
   146  
   147  	for _, test := range tests {
   148  		t.Run(test.name, func(t *testing.T) {
   149  			g := &Generate{
   150  				moduleName:         "github.com/this/module",
   151  				thirdPartyFolder:   "third_party/go",
   152  				replace:            map[string]string{},
   153  				knownImportTargets: map[string]string{},
   154  				moduleDeps:         test.deps,
   155  			}
   156  
   157  			assert.Equal(t, test.expected, g.depTarget(test.importTarget))
   158  		})
   159  	}
   160  }