golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/cmd/apidiff/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"go/types"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"golang.org/x/exp/apidiff"
     9  )
    10  
    11  func TestIsInternalPackage(t *testing.T) {
    12  	for _, tst := range []struct {
    13  		name, pkg, mod string
    14  		want           bool
    15  	}{
    16  		{
    17  			name: "not internal no module",
    18  			pkg:  "foo",
    19  			want: false,
    20  		},
    21  		{
    22  			name: "not internal with module",
    23  			pkg:  "example.com/bar/foo",
    24  			mod:  "example.com/bar",
    25  			want: false,
    26  		},
    27  		{
    28  			name: "internal no module",
    29  			pkg:  "internal",
    30  			want: true,
    31  		},
    32  		{
    33  			name: "leading internal no module",
    34  			pkg:  "internal/foo",
    35  			want: true,
    36  		},
    37  		{
    38  			name: "middle internal no module",
    39  			pkg:  "foo/internal/bar",
    40  			want: true,
    41  		},
    42  		{
    43  			name: "ending internal no module",
    44  			pkg:  "foo/internal",
    45  			want: true,
    46  		},
    47  
    48  		{
    49  			name: "leading internal with module",
    50  			pkg:  "example.com/baz/internal/foo",
    51  			mod:  "example.com/baz",
    52  			want: true,
    53  		},
    54  		{
    55  			name: "middle internal with module",
    56  			pkg:  "example.com/baz/foo/internal/bar",
    57  			mod:  "example.com/baz",
    58  			want: true,
    59  		},
    60  		{
    61  			name: "ending internal with module",
    62  			pkg:  "example.com/baz/foo/internal",
    63  			mod:  "example.com/baz",
    64  			want: true,
    65  		},
    66  		{
    67  			name: "not package internal with internal module",
    68  			pkg:  "example.com/internal/foo",
    69  			mod:  "example.com/internal",
    70  			want: false,
    71  		},
    72  	} {
    73  		t.Run(tst.name, func(t *testing.T) {
    74  			if got := isInternalPackage(tst.pkg, tst.mod); got != tst.want {
    75  				t.Errorf("expected %v, got %v for %s/%s", tst.want, got, tst.mod, tst.pkg)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  func TestFilterInternal(t *testing.T) {
    82  	for _, tst := range []struct {
    83  		name  string
    84  		mod   *apidiff.Module
    85  		allow bool
    86  		want  []*types.Package
    87  	}{
    88  		{
    89  			name: "allow internal",
    90  			mod: &apidiff.Module{
    91  				Path: "example.com/foo",
    92  				Packages: []*types.Package{
    93  					types.NewPackage("example.com/foo/bar", "bar"),
    94  					types.NewPackage("example.com/foo/internal", "internal"),
    95  					types.NewPackage("example.com/foo/internal/buz", "buz"),
    96  					types.NewPackage("example.com/foo/bar/internal", "internal"),
    97  				},
    98  			},
    99  			allow: true,
   100  			want: []*types.Package{
   101  				types.NewPackage("example.com/foo/bar", "bar"),
   102  				types.NewPackage("example.com/foo/internal", "internal"),
   103  				types.NewPackage("example.com/foo/internal/buz", "buz"),
   104  				types.NewPackage("example.com/foo/bar/internal", "internal"),
   105  			},
   106  		},
   107  		{
   108  			name: "filter internal",
   109  			mod: &apidiff.Module{
   110  				Path: "example.com/foo",
   111  				Packages: []*types.Package{
   112  					types.NewPackage("example.com/foo/bar", "bar"),
   113  					types.NewPackage("example.com/foo/internal", "internal"),
   114  					types.NewPackage("example.com/foo/internal/buz", "buz"),
   115  					types.NewPackage("example.com/foo/bar/internal", "internal"),
   116  				},
   117  			},
   118  			want: []*types.Package{
   119  				types.NewPackage("example.com/foo/bar", "bar"),
   120  			},
   121  		},
   122  		{
   123  			name: "filter internal nothing left",
   124  			mod: &apidiff.Module{
   125  				Path: "example.com/foo",
   126  				Packages: []*types.Package{
   127  					types.NewPackage("example.com/foo/internal", "internal"),
   128  				},
   129  			},
   130  			want: nil,
   131  		},
   132  	} {
   133  		t.Run(tst.name, func(t *testing.T) {
   134  			filterInternal(tst.mod, tst.allow)
   135  			if diff := cmp.Diff(tst.mod.Packages, tst.want, cmp.Comparer(comparePath)); diff != "" {
   136  				t.Errorf("got(-),want(+):\n%s", diff)
   137  			}
   138  		})
   139  	}
   140  }
   141  
   142  func comparePath(x, y *types.Package) bool {
   143  	return x.Path() == y.Path()
   144  }