File: main_test.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (144 lines) | stat: -rw-r--r-- 3,359 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main

import (
	"go/types"
	"testing"

	"github.com/google/go-cmp/cmp"
	"golang.org/x/exp/apidiff"
)

func TestIsInternalPackage(t *testing.T) {
	for _, tst := range []struct {
		name, pkg, mod string
		want           bool
	}{
		{
			name: "not internal no module",
			pkg:  "foo",
			want: false,
		},
		{
			name: "not internal with module",
			pkg:  "example.com/bar/foo",
			mod:  "example.com/bar",
			want: false,
		},
		{
			name: "internal no module",
			pkg:  "internal",
			want: true,
		},
		{
			name: "leading internal no module",
			pkg:  "internal/foo",
			want: true,
		},
		{
			name: "middle internal no module",
			pkg:  "foo/internal/bar",
			want: true,
		},
		{
			name: "ending internal no module",
			pkg:  "foo/internal",
			want: true,
		},

		{
			name: "leading internal with module",
			pkg:  "example.com/baz/internal/foo",
			mod:  "example.com/baz",
			want: true,
		},
		{
			name: "middle internal with module",
			pkg:  "example.com/baz/foo/internal/bar",
			mod:  "example.com/baz",
			want: true,
		},
		{
			name: "ending internal with module",
			pkg:  "example.com/baz/foo/internal",
			mod:  "example.com/baz",
			want: true,
		},
		{
			name: "not package internal with internal module",
			pkg:  "example.com/internal/foo",
			mod:  "example.com/internal",
			want: false,
		},
	} {
		t.Run(tst.name, func(t *testing.T) {
			if got := isInternalPackage(tst.pkg, tst.mod); got != tst.want {
				t.Errorf("expected %v, got %v for %s/%s", tst.want, got, tst.mod, tst.pkg)
			}
		})
	}
}

func TestFilterInternal(t *testing.T) {
	for _, tst := range []struct {
		name  string
		mod   *apidiff.Module
		allow bool
		want  []*types.Package
	}{
		{
			name: "allow internal",
			mod: &apidiff.Module{
				Path: "example.com/foo",
				Packages: []*types.Package{
					types.NewPackage("example.com/foo/bar", "bar"),
					types.NewPackage("example.com/foo/internal", "internal"),
					types.NewPackage("example.com/foo/internal/buz", "buz"),
					types.NewPackage("example.com/foo/bar/internal", "internal"),
				},
			},
			allow: true,
			want: []*types.Package{
				types.NewPackage("example.com/foo/bar", "bar"),
				types.NewPackage("example.com/foo/internal", "internal"),
				types.NewPackage("example.com/foo/internal/buz", "buz"),
				types.NewPackage("example.com/foo/bar/internal", "internal"),
			},
		},
		{
			name: "filter internal",
			mod: &apidiff.Module{
				Path: "example.com/foo",
				Packages: []*types.Package{
					types.NewPackage("example.com/foo/bar", "bar"),
					types.NewPackage("example.com/foo/internal", "internal"),
					types.NewPackage("example.com/foo/internal/buz", "buz"),
					types.NewPackage("example.com/foo/bar/internal", "internal"),
				},
			},
			want: []*types.Package{
				types.NewPackage("example.com/foo/bar", "bar"),
			},
		},
		{
			name: "filter internal nothing left",
			mod: &apidiff.Module{
				Path: "example.com/foo",
				Packages: []*types.Package{
					types.NewPackage("example.com/foo/internal", "internal"),
				},
			},
			want: nil,
		},
	} {
		t.Run(tst.name, func(t *testing.T) {
			filterInternal(tst.mod, tst.allow)
			if diff := cmp.Diff(tst.mod.Packages, tst.want, cmp.Comparer(comparePath)); diff != "" {
				t.Errorf("got(-),want(+):\n%s", diff)
			}
		})
	}
}

func comparePath(x, y *types.Package) bool {
	return x.Path() == y.Path()
}