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