| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 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 |
// -gcflags=-S applies only to packages on command line. |
| 29 |
{ |
| 30 |
args: []string{"-S"}, |
| 31 |
pkgs: []ppfTestPackage{ |
| 32 |
{cmdline: true, flags: []string{"-S"}}, |
| 33 |
{cmdline: false, flags: []string{}}, |
| 34 |
}, |
| 35 |
}, |
| 36 |
|
| 37 |
// -gcflags=-S -gcflags= overrides the earlier -S. |
| 38 |
{ |
| 39 |
args: []string{"-S", ""}, |
| 40 |
pkgs: []ppfTestPackage{ |
| 41 |
{cmdline: true, flags: []string{}}, |
| 42 |
}, |
| 43 |
}, |
| 44 |
|
| 45 |
// -gcflags=net=-S applies only to package net |
| 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 |
// -gcflags=net=-S -gcflags=net= also overrides the earlier -S |
| 55 |
{ |
| 56 |
args: []string{"net=-S", "net="}, |
| 57 |
pkgs: []ppfTestPackage{ |
| 58 |
{path: "net", flags: []string{}}, |
| 59 |
}, |
| 60 |
}, |
| 61 |
|
| 62 |
// -gcflags=net/...=-S net math |
| 63 |
// applies -S to net and net/http but not math |
| 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 |
// -gcflags=net/...=-S -gcflags=-m net math |
| 74 |
// applies -m to net and math and -S to other packages matching net/... |
| 75 |
// (net matches too, but it was grabbed by the later -gcflags). |
| 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 |
// relative path patterns |
| 88 |
// ppfDirTest(pattern, n, dirs...) says the first n dirs should match and the others should not. |
| 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 |
} |