-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_test.go
197 lines (160 loc) · 4.19 KB
/
loader_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package langd
import (
"path/filepath"
"runtime"
"sync"
"testing"
"github.com/object88/langd/collections"
"github.com/spf13/afero"
)
func Test_LoadContext_Same_Package_Same_Env(t *testing.T) {
src := `package bar
var BarVal int = 0`
rootPath, overlayFs := createOverlay(map[string]string{
filepath.Join("bar", "bar.go"): src,
})
barPath := filepath.Join(rootPath, "bar")
le := NewLoaderEngine()
defer le.Close()
l1 := NewLoader(le, "darwin", "x86", runtime.GOROOT(), func(l *Loader) {
l.fs = afero.NewCopyOnWriteFs(l.fs, overlayFs)
})
l2 := NewLoader(le, "linux", "arm", runtime.GOROOT(), func(l *Loader) {
l.fs = afero.NewCopyOnWriteFs(l.fs, overlayFs)
})
err := l1.LoadDirectory(barPath)
if err != nil {
t.Fatalf("(1) Error while loading: %s", err.Error())
}
err = l2.LoadDirectory(barPath)
if err != nil {
t.Fatalf("(2) Error while loading: %s", err.Error())
}
l1.Wait()
l2.Wait()
errCount := 0
fn := func(file string, errs []FileError) {
if errCount == 0 {
t.Errorf("Loading error in %s:\n", file)
}
for k, err := range errs {
t.Errorf("\t%d: %s\n", k, err.Message)
}
errCount++
}
l1.Errors(fn)
l2.Errors(fn)
if errCount != 0 {
t.Fatalf("Found %d errors", errCount)
}
packageCount := 0
le.caravan.Iter(func(hash collections.Hash, node *collections.Node) bool {
packageCount++
return true
})
if packageCount != 2 {
t.Errorf("Expected to find 1 package; found %d\n", packageCount)
}
failed := false
for _, l := range []*Loader{l1, l2} {
_, err := l.FindDistinctPackage(barPath)
if err != nil {
failed = true
t.Errorf("Failed to find package '%s'", barPath)
}
}
if failed {
le.caravan.Iter(func(hash collections.Hash, n *collections.Node) bool {
t.Errorf("Have hash 0x%x: %s\n", hash, n.Element.(*DistinctPackage))
return true
})
}
}
func Test_LoadContext_Same_Package_Different_Env(t *testing.T) {
src := `package bar
var BarVal BarType = 0`
srcDarwin := `package bar
type BarType int32`
srcLinux := `package bar
type BarType int64`
rootPath, overlayFs := createOverlay(map[string]string{
filepath.Join("bar", "bar.go"): src,
filepath.Join("bar", "bar_darwin.go"): srcDarwin,
filepath.Join("bar", "bar_linux.go"): srcLinux,
})
barPath := filepath.Join(rootPath, "bar")
le := NewLoaderEngine()
defer le.Close()
envs := [][]string{
[]string{"darwin", "amd"},
[]string{"linux", "arm"},
}
wg := sync.WaitGroup{}
wg.Add(2)
ls := make([]*Loader, 2)
for i := 0; i < 2; i++ {
ii := i
env := envs[i]
go func() {
l := NewLoader(le, env[0], env[1], runtime.GOROOT(), func(l *Loader) {
l.fs = afero.NewCopyOnWriteFs(l.fs, overlayFs)
})
ls[ii] = l
err := l.LoadDirectory(barPath)
if err != nil {
t.Fatalf("Error while loading: %s", err.Error())
}
l.Wait()
wg.Done()
}()
}
wg.Wait()
errCount := 0
for i := 0; i < 2; i++ {
ls[i].Errors(func(file string, errs []FileError) {
if errCount == 0 {
t.Errorf("Loading error in %s:\n", file)
}
for k, err := range errs {
t.Errorf("\t%d: %s\n", k, err.Message)
}
errCount++
})
}
if errCount != 0 {
t.Fatalf("Found %d errors", errCount)
}
packageCount := 0
le.caravan.Iter(func(hash collections.Hash, node *collections.Node) bool {
packageCount++
return true
})
if packageCount != 2 {
t.Errorf("Expected to find 1 packages; found %d\n", packageCount)
}
for _, l := range ls {
_, err := l.FindDistinctPackage(barPath)
if err != nil {
t.Errorf("Failed to find package '%s'", barPath)
}
}
// dp := n.Element.(*DistinctPackage)
// if 2 != len(p.distincts) {
// t.Errorf("Expected to find 2 distinct packages; found %d", len(p.distincts))
// }
// for _, dp := range p.distincts {
// if len(dp.files) != 2 {
// t.Errorf("Package %s has the wrong number of files; expected 2, got %d", dp, len(dp.files))
// }
// }
// for i := 0; i < 2; i++ {
// le.caravan.Iter(func(hash collections.Hash, node *collections.Node) bool {
// p := node.Element.(*Package)
// dp := p.distincts[lcs[i].GetDistinctHash()]
// if len(dp.files) != 2 {
// t.Errorf("Package '%s' has the wrong number of files; expected 2, got %d", p.shortPath, len(dp.files))
// }
// return true
// })
// }
}