forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimports_test.go
163 lines (138 loc) · 2.54 KB
/
imports_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package packages_test
import (
"os"
"path/filepath"
"testing"
"github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/packages"
"github.com/stretchr/testify/require"
)
func TestImports(t *testing.T) {
workingDir, err := os.Getwd()
require.NoError(t, err)
// create external dir
tmpDir := t.TempDir()
// cd to tmp directory
os.Chdir(tmpDir)
defer os.Chdir(workingDir)
files := []struct {
name, data string
}{
{
name: "file1.gno",
data: `
package tmp
import (
"std"
"gno.land/p/demo/pkg1"
)
`,
},
{
name: "file2.gno",
data: `
package tmp
import (
"gno.land/p/demo/pkg1"
"gno.land/p/demo/pkg2"
)
`,
},
{
name: "file1_test.gno",
data: `
package tmp
import (
"testing"
"gno.land/p/demo/testpkg"
)
`,
},
{
name: "file2_test.gno",
data: `
package tmp_test
import (
"testing"
"gno.land/p/demo/testpkg"
"gno.land/p/demo/xtestdep"
)
`,
},
{
name: "z_0_filetest.gno",
data: `
package main
import (
"gno.land/p/demo/filetestdep"
)
`,
},
// subpkg files
{
name: filepath.Join("subtmp", "file1.gno"),
data: `
package subtmp
import (
"std"
"gno.land/p/demo/subpkg1"
)
`,
},
{
name: filepath.Join("subtmp", "file2.gno"),
data: `
package subtmp
import (
"gno.land/p/demo/subpkg1"
"gno.land/p/demo/subpkg2"
)
`,
},
}
// Expected lists of imports
// - ignore subdirs
// - ignore duplicate
// - should be sorted
expected := map[packages.FileKind][]string{
packages.FileKindPackageSource: {
"gno.land/p/demo/pkg1",
"gno.land/p/demo/pkg2",
"std",
},
packages.FileKindTest: {
"gno.land/p/demo/testpkg",
"testing",
},
packages.FileKindXTest: {
"gno.land/p/demo/testpkg",
"gno.land/p/demo/xtestdep",
"testing",
},
packages.FileKindFiletest: {
"gno.land/p/demo/filetestdep",
},
}
// Create subpkg dir
err = os.Mkdir("subtmp", 0o700)
require.NoError(t, err)
// Create files
for _, f := range files {
err = os.WriteFile(f.name, []byte(f.data), 0o644)
require.NoError(t, err)
}
pkg, err := gnolang.ReadMemPackage(tmpDir, "test")
require.NoError(t, err)
importsMap, err := packages.Imports(pkg, nil)
require.NoError(t, err)
// ignore specs
got := map[packages.FileKind][]string{}
for key, vals := range importsMap {
stringVals := make([]string, len(vals))
for i, val := range vals {
stringVals[i] = val.PkgPath
}
got[key] = stringVals
}
require.Equal(t, expected, got)
}