...
1
16
17 package printers
18
19 import (
20 "testing"
21 )
22
23 func TestIllegalPackageSourceChecker(t *testing.T) {
24 disallowedPrefixes := []string{
25 "foo/bar",
26 "k8s.io/foo/bar/vendor/k8s.io/baz/buz",
27 "bar/foo/baz",
28 }
29
30 testCases := []struct {
31 name string
32 pkgPath string
33 shouldBeAllowed bool
34 }{
35 {
36 name: "package path beginning with forbidden prefix is rejected",
37 pkgPath: "foo/bar/baz/buz",
38 shouldBeAllowed: false,
39 },
40 {
41 name: "package path not fully matching forbidden prefix is allowed",
42 pkgPath: "bar/foo",
43 shouldBeAllowed: true,
44 },
45 {
46 name: "package path containing forbidden prefix (not as prefix) is allowed",
47 pkgPath: "k8s.io/bar/foo/baz/etc",
48 shouldBeAllowed: true,
49 },
50 }
51
52 checker := &illegalPackageSourceChecker{disallowedPrefixes}
53
54 for _, tc := range testCases {
55 if checker.IsForbidden(tc.pkgPath) {
56 if tc.shouldBeAllowed {
57 t.Fatalf("expected package path %q to have been allowed", tc.pkgPath)
58 }
59 continue
60 }
61
62 if !tc.shouldBeAllowed {
63 t.Fatalf("expected package path %q to have been rejected", tc.pkgPath)
64 }
65 }
66 }
67
View as plain text