/
test_go_buildgen.py
440 lines (364 loc) · 14.6 KB
/
test_go_buildgen.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import types
from textwrap import dedent
from pants.build_graph.target import Target
from pants.testutil.task_test_base import TaskTestBase
from pants.contrib.go import register
from pants.contrib.go.subsystems.fetcher import Fetcher
from pants.contrib.go.targets.go_binary import GoBinary
from pants.contrib.go.targets.go_library import GoLibrary
from pants.contrib.go.targets.go_remote_library import GoRemoteLibrary
from pants.contrib.go.tasks.go_buildgen import GoBuildgen, GoTargetGenerator
class FakeFetcher(Fetcher):
def root(self):
return "pantsbuild.org/fake"
def fetch(self, dest, rev=None):
raise AssertionError("No fetches should be executed during go.buildgen")
class FakeFetcherFactory:
def get_fetcher(self, import_path):
return FakeFetcher(import_path)
class GoBuildgenTest(TaskTestBase):
@classmethod
def task_type(cls):
return GoBuildgen
def create_task(self, context, workdir=None):
task = super().create_task(context, workdir)
task.get_fetcher_factory = types.MethodType(lambda s: FakeFetcherFactory(), task)
return task
@classmethod
def alias_groups(cls):
# Needed for test_stitch_deps_remote_existing_rev_respected which re-loads a synthetic target
# from a generated BUILD file on disk that needs access to Go target aliases
return register.build_file_aliases()
def test_noop_no_targets(self):
context = self.context()
task = self.create_task(context)
task.execute()
self.assertEqual([], context.targets())
def test_noop_no_applicable_targets(self):
context = self.context(target_roots=[self.make_target(":a", Target)])
expected = context.targets()
task = self.create_task(context)
task.execute()
self.assertEqual(expected, context.targets())
def test_multiple_local_roots_failure(self):
self.create_dir("src/go/src")
self.create_dir("src/main/go/src")
context = self.context(
target_roots=[
self.make_target("src/go/src/fred", GoBinary),
self.make_target("src/main/go/src/barney", GoBinary),
]
)
task = self.create_task(context)
with self.assertRaises(task.InvalidLocalRootsError):
task.execute()
def test_existing_targets_wrong_type(self):
self.create_file(
relpath="src/go/src/fred/foo.go",
contents=dedent(
"""
package main
import "fmt"
func main() {
fmt.Printf("Hello World!")
}
"""
),
)
context = self.context(target_roots=[self.make_target("src/go/src/fred", GoLibrary)])
task = self.create_task(context)
with self.assertRaises(task.GenerationError) as exc:
task.execute()
self.assertEqual(
GoTargetGenerator.WrongLocalSourceTargetTypeError, type(exc.exception.cause)
)
def test_noop_applicable_targets_simple(self):
self.create_file(
relpath="src/go/src/fred/foo.go",
contents=dedent(
"""
package main
import "fmt"
func main() {
fmt.Printf("Hello World!")
}
"""
),
)
context = self.context(target_roots=[self.make_target("src/go/src/fred", GoBinary)])
expected = context.targets()
task = self.create_task(context)
task.execute()
self.assertEqual(expected, context.targets())
def test_noop_applicable_targets_complete_graph(self):
self.create_file(
relpath="src/go/src/jane/bar.go",
contents=dedent(
"""
package jane
var PublicConstant = 42
"""
),
)
jane = self.make_target("src/go/src/jane", GoLibrary)
self.create_file(
relpath="src/go/src/fred/foo.go",
contents=dedent(
"""
package main
import (
"fmt"
"jane"
)
func main() {
fmt.Printf("Hello %s!", jane.PublicConstant)
}
"""
),
)
fred = self.make_target("src/go/src/fred", GoBinary, dependencies=[jane])
context = self.context(target_roots=[fred])
expected = context.targets()
task = self.create_task(context)
task.execute()
self.assertEqual(expected, context.targets())
def stitch_deps_local(self, materialize):
self.set_options(materialize=materialize)
if materialize:
# We need physical directories on disk for `--materialize` since it does scans.
self.create_dir("src/go/src")
self.create_file(
relpath="src/go/src/jane/bar.go",
contents=dedent(
"""
package jane
var PublicConstant = 42
"""
),
)
self.create_file(
relpath="src/go/src/fred/foo.go",
contents=dedent(
"""
package main
import (
"fmt"
"jane"
)
func main() {
fmt.Printf("Hello %s!", jane.PublicConstant)
}
"""
),
)
if materialize:
# We need physical BUILD files on disk for `--materialize` since it does scans.
self.add_to_build_file("src/go/src/fred", "go_binary()")
fred = self.target("src/go/src/fred")
target_roots = None
else:
fred = self.make_target("src/go/src/fred", GoBinary)
target_roots = [fred]
context = self.context(target_roots=target_roots)
pre_execute_files = self.buildroot_files()
task = self.create_task(context)
task.execute()
jane = self.target("src/go/src/jane")
self.assertIsNotNone(jane)
self.assertEqual([jane], fred.dependencies)
self.assertEqual({jane, fred}, set(self.build_graph.targets()))
return pre_execute_files
def test_stitch_deps(self):
pre_execute_files = self.stitch_deps_local(materialize=False)
self.assertEqual(pre_execute_files, self.buildroot_files())
def test_stitch_deps_generate_builds(self):
pre_execute_files = self.stitch_deps_local(materialize=True)
self.assertEqual({"src/go/src/jane/BUILD"}, self.buildroot_files() - pre_execute_files)
def test_stitch_deps_generate_builds_custom_extension(self):
self.set_options(extension=".gen")
pre_execute_files = self.stitch_deps_local(materialize=True)
# NB: The src/go/fred/BUILD file on disk was deleted and replaced with src/go/fred/BUILD.gen.
self.assertEqual(
{"src/go/src/fred/BUILD.gen", "src/go/src/jane/BUILD.gen"},
self.buildroot_files() - pre_execute_files,
)
def stitch_deps_remote(self, remote=True, materialize=False, fail_floating=False):
self.set_options(remote=remote, materialize=materialize, fail_floating=fail_floating)
if materialize:
# We need physical directories on disk for `--materialize` since it does scans.
self.create_dir("3rdparty/go")
self.create_dir("src/go/src")
self.create_file(
relpath="src/go/src/jane/bar.go",
contents=dedent(
"""
package jane
import "pantsbuild.org/fake/prod"
var PublicConstant = prod.DoesNotExistButWeShouldNotCareWhenCheckingDepsAndNotInstalling
"""
),
)
self.create_file(
relpath="src/go/src/fred/foo.go",
contents=dedent(
"""
package main
import (
"fmt"
"jane"
)
func main() {
fmt.Printf("Hello %s!", jane.PublicConstant)
}
"""
),
)
if materialize:
# We need physical BUILD files on disk for `--materialize` since it does a scan.
self.add_to_build_file("src/go/src/fred", "go_binary()")
fred = self.target("src/go/src/fred")
target_roots = None
else:
fred = self.make_target("src/go/src/fred", GoBinary)
target_roots = [fred]
self.set_options(remote_root="3rdparty/go")
context = self.context(target_roots=target_roots)
pre_execute_files = self.buildroot_files()
task = self.create_task(context)
task.execute()
jane = self.target("src/go/src/jane")
self.assertIsNotNone(jane)
self.assertEqual([jane], fred.dependencies)
prod = self.target("3rdparty/go/pantsbuild.org/fake:prod")
self.assertIsNotNone(prod)
self.assertEqual([prod], jane.dependencies)
self.assertEqual({prod, jane, fred}, set(self.build_graph.targets()))
return pre_execute_files
def test_stitch_deps_remote(self):
self.create_dir("3rdparty/go")
pre_execute_files = self.stitch_deps_remote(materialize=False)
self.assertEqual(pre_execute_files, self.buildroot_files())
def test_stitch_deps_remote_unused(self):
# An unused remote lib
self.add_to_build_file("3rdparty/go/github.com/user/repo", "go_remote_library()")
pre_execute_files = self.stitch_deps_remote(materialize=False)
# Check the unused remote lib was not deleted since we can't know if it was actually unused or
# a transitive dep of a used remote_lib.
self.assertIn("3rdparty/go/github.com/user/repo/BUILD", self.buildroot_files())
self.assertEqual(pre_execute_files, self.buildroot_files())
def test_stitch_deps_remote_existing_rev_respected(self):
self.make_target(
"3rdparty/go/pantsbuild.org/fake:prod", GoRemoteLibrary, pkg="prod", rev="v1.2.3"
)
pre_execute_files = self.stitch_deps_remote(materialize=True)
self.reset_build_graph(reset_build_files=True) # Force targets to be loaded off disk
self.assertEqual("v1.2.3", self.target("3rdparty/go/pantsbuild.org/fake:prod").rev)
self.assertEqual(
{"src/go/src/jane/BUILD", "3rdparty/go/pantsbuild.org/fake/BUILD"},
self.buildroot_files() - pre_execute_files,
)
def test_stitch_deps_remote_generate_builds(self):
pre_execute_files = self.stitch_deps_remote(materialize=True)
self.assertEqual(
{"src/go/src/jane/BUILD", "3rdparty/go/pantsbuild.org/fake/BUILD"},
self.buildroot_files() - pre_execute_files,
)
def test_stitch_deps_remote_disabled_fails(self):
self.create_dir("3rdparty/go")
with self.assertRaises(GoBuildgen.GenerationError) as exc:
self.stitch_deps_remote(remote=False)
self.assertEqual(
GoTargetGenerator.NewRemoteEncounteredButRemotesNotAllowedError,
type(exc.exception.cause),
)
def test_fail_floating(self):
with self.assertRaises(GoBuildgen.FloatingRemoteError):
self.stitch_deps_remote(remote=True, materialize=True, fail_floating=True)
def test_issues_2616(self):
self.set_options(remote=False)
self.create_file(
relpath="src/go/src/jane/bar.go",
contents=dedent(
"""
package jane
var PublicConstant = 42
"""
),
)
self.create_file(
relpath="src/go/src/fred/foo.go",
contents=dedent(
"""
package main
/*
#include <stdlib.h>
*/
import "C" // C was erroneously categorized as a remote lib in issue 2616.
import (
"fmt"
"jane"
)
func main() {
fmt.Printf("Hello %s!", jane.PublicConstant)
fmt.Printf("Random from C: %d", int(C.random()))
}
"""
),
)
fred = self.make_target("src/go/src/fred", GoBinary)
context = self.context(target_roots=[fred])
task = self.create_task(context)
task.execute()
jane = self.target("src/go/src/jane")
self.assertIsNotNone(jane)
self.assertEqual([jane], fred.dependencies)
self.assertEqual({jane, fred}, set(self.build_graph.targets()))
def test_issues_2787(self):
# Previously, `XTestImports` were not handled. These imports are those of out-of-package black
# box tests. We create one of these below in the `lib/` dir with `lib_test.go` in the
# `lib_test` package.
self.set_options(remote=False, materialize=False)
self.create_file(
relpath="src/go/src/helper/helper.go",
contents=dedent(
"""
package helper
const PublicConstant = 42
"""
),
)
self.create_file(
relpath="src/go/src/lib/lib.go",
contents=dedent(
"""
package lib
const privateConstant = 42
"""
),
)
self.create_file(
relpath="src/go/src/lib/lib_test.go",
contents=dedent(
"""
package lib_test
import (
"helper"
"testing"
)
func TestAdd(t *testing.T) {
if privateConstant != helper.PublicConstant {
t.Fatalf("got: %d, expected: %d", privateConstant, helper.PublicConstant)
}
}
"""
),
)
lib = self.make_target("src/go/src/lib", GoLibrary)
self.assertEqual([], lib.dependencies)
context = self.context(target_roots=[lib])
task = self.create_task(context)
task.execute()
helper = self.target("src/go/src/helper")
self.assertEqual([helper], lib.dependencies)