1
2
3
4
5 package load
6
7 import (
8 "cmd/go/internal/modload"
9 "fmt"
10 "path/filepath"
11 "reflect"
12 "testing"
13 )
14
15 type ppfTestPackage struct {
16 path string
17 dir string
18 cmdline bool
19 flags []string
20 }
21
22 type ppfTest struct {
23 args []string
24 pkgs []ppfTestPackage
25 }
26
27 var ppfTests = []ppfTest{
28
29 {
30 args: []string{"-S"},
31 pkgs: []ppfTestPackage{
32 {cmdline: true, flags: []string{"-S"}},
33 {cmdline: false, flags: []string{}},
34 },
35 },
36
37
38 {
39 args: []string{"-S", ""},
40 pkgs: []ppfTestPackage{
41 {cmdline: true, flags: []string{}},
42 },
43 },
44
45
46 {
47 args: []string{"net=-S"},
48 pkgs: []ppfTestPackage{
49 {path: "math", cmdline: true, flags: []string{}},
50 {path: "net", flags: []string{"-S"}},
51 },
52 },
53
54
55 {
56 args: []string{"net=-S", "net="},
57 pkgs: []ppfTestPackage{
58 {path: "net", flags: []string{}},
59 },
60 },
61
62
63
64 {
65 args: []string{"net/...=-S"},
66 pkgs: []ppfTestPackage{
67 {path: "net", flags: []string{"-S"}},
68 {path: "net/http", flags: []string{"-S"}},
69 {path: "math", flags: []string{}},
70 },
71 },
72
73
74
75
76 {
77 args: []string{"net/...=-S", "-m"},
78 pkgs: []ppfTestPackage{
79 {path: "net", cmdline: true, flags: []string{"-m"}},
80 {path: "math", cmdline: true, flags: []string{"-m"}},
81 {path: "net", cmdline: false, flags: []string{"-S"}},
82 {path: "net/http", flags: []string{"-S"}},
83 {path: "math", flags: []string{}},
84 },
85 },
86
87
88
89 ppfDirTest(".", 1, "/my/test/dir", "/my/test", "/my/test/other", "/my/test/dir/sub"),
90 ppfDirTest("..", 1, "/my/test", "/my/test/dir", "/my/test/other", "/my/test/dir/sub"),
91 ppfDirTest("./sub", 1, "/my/test/dir/sub", "/my/test", "/my/test/dir", "/my/test/other", "/my/test/dir/sub/sub"),
92 ppfDirTest("../other", 1, "/my/test/other", "/my/test", "/my/test/dir", "/my/test/other/sub", "/my/test/dir/other", "/my/test/dir/sub"),
93 ppfDirTest("./...", 3, "/my/test/dir", "/my/test/dir/sub", "/my/test/dir/sub/sub", "/my/test/other", "/my/test/other/sub"),
94 ppfDirTest("../...", 4, "/my/test/dir", "/my/test/other", "/my/test/dir/sub", "/my/test/other/sub", "/my/other/test"),
95 ppfDirTest("../...sub...", 3, "/my/test/dir/sub", "/my/test/othersub", "/my/test/yellowsubmarine", "/my/other/test"),
96 }
97
98 func ppfDirTest(pattern string, nmatch int, dirs ...string) ppfTest {
99 var pkgs []ppfTestPackage
100 for i, d := range dirs {
101 flags := []string{}
102 if i < nmatch {
103 flags = []string{"-S"}
104 }
105 pkgs = append(pkgs, ppfTestPackage{path: "p", dir: d, flags: flags})
106 }
107 return ppfTest{args: []string{pattern + "=-S"}, pkgs: pkgs}
108 }
109
110 func TestPerPackageFlag(t *testing.T) {
111 nativeDir := func(d string) string {
112 if filepath.Separator == '\\' {
113 return `C:` + filepath.FromSlash(d)
114 }
115 return d
116 }
117
118 for i, tt := range ppfTests {
119 t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
120 ppFlags := new(PerPackageFlag)
121 for _, arg := range tt.args {
122 t.Logf("set(%s)", arg)
123 if err := ppFlags.set(arg, nativeDir("/my/test/dir")); err != nil {
124 t.Fatal(err)
125 }
126 }
127 for _, p := range tt.pkgs {
128 dir := nativeDir(p.dir)
129 flags := ppFlags.For(modload.NewState(), &Package{PackagePublic: PackagePublic{ImportPath: p.path, Dir: dir}, Internal: PackageInternal{CmdlinePkg: p.cmdline}})
130 if !reflect.DeepEqual(flags, p.flags) {
131 t.Errorf("For(%v, %v, %v) = %v, want %v", p.path, dir, p.cmdline, flags, p.flags)
132 }
133 }
134 })
135 }
136 }
137
View as plain text