/
packages114_test.go
77 lines (67 loc) · 2.11 KB
/
packages114_test.go
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
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.14
package packages_test
import (
"fmt"
"path/filepath"
"testing"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/packages/packagestest"
)
func TestInvalidFilesInOverlay(t *testing.T) { packagestest.TestAll(t, testInvalidFilesInOverlay) }
func testInvalidFilesInOverlay(t *testing.T, exporter packagestest.Exporter) {
exported := packagestest.Export(t, exporter, []packagestest.Module{
{
Name: "golang.org/fake",
Files: map[string]interface{}{
"d/d.go": `package d; import "net/http"; const d = http.MethodGet;`,
"d/util.go": ``,
"d/d_test.go": ``,
},
},
})
defer exported.Cleanup()
dir := filepath.Dir(filepath.Dir(exported.File("golang.org/fake", "d/d.go")))
// Additional tests for test variants.
for i, tt := range []struct {
name string
overlay map[string][]byte
want string // expected value of d.D
}{
// Overlay with a test variant.
{"test_variant",
map[string][]byte{
filepath.Join(dir, "d", "d_test.go"): []byte(`package d; import "testing"; const D = d + "_test"; func TestD(t *testing.T) {};`)},
`"GET_test"`},
// Overlay in package.
{"second_file",
map[string][]byte{
filepath.Join(dir, "d", "util.go"): []byte(`package d; const D = d + "_util";`)},
`"GET_util"`},
} {
t.Run(tt.name, func(t *testing.T) {
exported.Config.Overlay = tt.overlay
exported.Config.Mode = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles |
packages.NeedDeps | packages.NeedTypes | packages.NeedTypesSizes
exported.Config.Tests = true
for f := range tt.overlay {
initial, err := packages.Load(exported.Config, fmt.Sprintf("file=%s", f))
if err != nil {
t.Fatal(err)
}
d := initial[0]
// Check value of d.D.
dD := constant(d, "D")
if dD == nil {
t.Fatalf("%d. d.D: got nil", i)
}
got := dD.Val().String()
if got != tt.want {
t.Fatalf("%d. d.D: got %s, want %s", i, got, tt.want)
}
}
})
}
}