Repository navigation
Expand file tree
/
Copy pathgraphwire_test.go
More file actions
696 lines (575 loc) · 20 KB
/
Copy pathgraphwire_test.go
File metadata and controls
696 lines (575 loc) · 20 KB
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// SPDX-License-Identifier: Apache-2.0
package graphwire
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
var testConfig = Config{
ExecImport: "example.com/myapp/graph",
CoreImport: "example.com/myapp/internal/graphres",
CoreSchemaGlobs: []string{"graph/schema/*.graphqls"},
WiringPath: "cmd/myapp/graph_gen.go",
License: "Apache-2.0",
}
const coreSchema = `
directive @goField(forceResolver: Boolean, name: String) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
type Query {
things: [Thing!]!
}
type Mutation {
makeThing(name: String!): Thing!
}
type Thing {
id: ID!
name: String!
owner: Owner @goField(forceResolver: true)
}
type Owner {
id: ID!
things(limit: Int): [Thing!]!
}
`
const alphaSchema = `
extend type Query {
alphaItems: [AlphaItem!]!
}
type AlphaItem {
id: ID!
thing: Thing @goField(forceResolver: true)
}
extend type Thing {
alphaItems: [AlphaItem!]! @goField(forceResolver: true)
}
`
const betaSchema = `
extend type Query {
betaCount: Int!
}
`
// writeCore writes the core schema fixture under root.
func writeCore(t *testing.T, root string) {
t.Helper()
dir := filepath.Join(root, "graph", "schema")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("creating %s: %v", dir, err)
}
if err := os.WriteFile(filepath.Join(dir, "core.graphqls"), []byte(coreSchema), 0o644); err != nil {
t.Fatalf("writing core schema: %v", err)
}
}
// writePlugin writes a plugin fixture with a manifest and optional SDL.
func writePlugin(t *testing.T, root, id, manifestJSON, sdl string) {
t.Helper()
writePluginIn(t, root, "plugins", id, manifestJSON, sdl)
}
// writePluginIn writes a plugin fixture under the named plugin root.
func writePluginIn(t *testing.T, root, pluginRoot, id, manifestJSON, sdl string) {
t.Helper()
pluginDir := filepath.Join(root, pluginRoot, id)
if err := os.MkdirAll(pluginDir, 0o755); err != nil {
t.Fatalf("creating %s: %v", pluginDir, err)
}
if err := os.WriteFile(filepath.Join(pluginDir, "plugin.json"), []byte(manifestJSON), 0o644); err != nil {
t.Fatalf("writing manifest: %v", err)
}
if sdl == "" {
return
}
graphDir := filepath.Join(pluginDir, "graph")
if err := os.MkdirAll(graphDir, 0o755); err != nil {
t.Fatalf("creating %s: %v", graphDir, err)
}
if err := os.WriteFile(filepath.Join(graphDir, "schema.graphqls"), []byte(sdl), 0o644); err != nil {
t.Fatalf("writing plugin schema: %v", err)
}
}
// generated runs the generator and returns the wiring file contents.
func generated(t *testing.T, root string) string {
t.Helper()
return generatedWith(t, root, testConfig)
}
// generatedWith runs the generator under cfg and returns the wiring file contents.
func generatedWith(t *testing.T, root string, cfg Config) string {
t.Helper()
if err := os.MkdirAll(filepath.Join(root, "cmd", "myapp"), 0o755); err != nil {
t.Fatalf("creating the output directory: %v", err)
}
if err := Run(root, cfg); err != nil {
t.Fatalf("Run() error = %v, want nil", err)
}
content, err := os.ReadFile(filepath.Join(root, "cmd", "myapp", "graph_gen.go"))
if err != nil {
t.Fatalf("reading the wiring file: %v", err)
}
return string(content)
}
const zeroPluginWiring = `// SPDX-License-Identifier: Apache-2.0
// Code generated by pluginwire. DO NOT EDIT.
package main
import (
graph "example.com/myapp/graph"
)
// newGraphRoot returns the core resolver root, no plugin extends the graph.
func newGraphRoot(core graph.ResolverRoot) graph.ResolverRoot {
return core
}
`
func TestZeroGraphQLPluginsYieldACoreOnlyRoot(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "feed",
`{"id": "feed", "name": "Feed", "backend": "example.com/myapp/plugins/feed"}`, "")
if got := generated(t, root); got != zeroPluginWiring {
t.Errorf("wiring mismatch\n got:\n%s\nwant:\n%s", got, zeroPluginWiring)
}
}
const composedWiring = `// SPDX-License-Identifier: Apache-2.0
// Code generated by pluginwire. DO NOT EDIT.
package main
import (
graph "example.com/myapp/graph"
graphres "example.com/myapp/internal/graphres"
alpha "example.com/myapp/plugins/alpha"
beta "example.com/myapp/plugins/beta"
)
// coreGraphResolvers lists the resolver sets the core contributes to the graph.
type coreGraphResolvers interface {
MutationResolvers() graphres.MutationResolvers
OwnerResolvers() graphres.OwnerResolvers
QueryResolvers() graphres.QueryResolvers
ThingResolvers() graphres.ThingResolvers
}
// alphaGraphResolvers lists the resolver sets the alpha plugin contributes to the graph.
type alphaGraphResolvers interface {
AlphaItemResolvers() alpha.AlphaItemResolvers
QueryResolvers() alpha.QueryResolvers
ThingResolvers() alpha.ThingResolvers
}
// betaGraphResolvers lists the resolver sets the beta plugin contributes to the graph.
type betaGraphResolvers interface {
QueryResolvers() beta.QueryResolvers
}
// coreQueryResolvers names the core Query resolver set for embedding.
type coreQueryResolvers = graphres.QueryResolvers
// alphaQueryResolvers names the alpha Query resolver set for embedding.
type alphaQueryResolvers = alpha.QueryResolvers
// betaQueryResolvers names the beta Query resolver set for embedding.
type betaQueryResolvers = beta.QueryResolvers
// composedQueryResolver merges every contributed Query resolver set.
type composedQueryResolver struct {
coreQueryResolvers
alphaQueryResolvers
betaQueryResolvers
}
// coreThingResolvers names the core Thing resolver set for embedding.
type coreThingResolvers = graphres.ThingResolvers
// alphaThingResolvers names the alpha Thing resolver set for embedding.
type alphaThingResolvers = alpha.ThingResolvers
// composedThingResolver merges every contributed Thing resolver set.
type composedThingResolver struct {
coreThingResolvers
alphaThingResolvers
}
// graphRoot composes the core and plugin resolver sets into the resolver root.
type graphRoot struct {
core coreGraphResolvers
alpha alphaGraphResolvers
beta betaGraphResolvers
}
// newGraphRoot composes the core resolver sets with every graphql plugin's.
func newGraphRoot(
core coreGraphResolvers,
alphaPlugin alphaGraphResolvers,
betaPlugin betaGraphResolvers,
) graph.ResolverRoot {
return graphRoot{core: core, alpha: alphaPlugin, beta: betaPlugin}
}
// AlphaItem returns the AlphaItem resolver set.
func (g graphRoot) AlphaItem() graph.AlphaItemResolver {
return g.alpha.AlphaItemResolvers()
}
// Mutation returns the Mutation resolver set.
func (g graphRoot) Mutation() graph.MutationResolver {
return g.core.MutationResolvers()
}
// Owner returns the Owner resolver set.
func (g graphRoot) Owner() graph.OwnerResolver {
return g.core.OwnerResolvers()
}
// Query returns the Query resolver set.
func (g graphRoot) Query() graph.QueryResolver {
return composedQueryResolver{
g.core.QueryResolvers(),
g.alpha.QueryResolvers(),
g.beta.QueryResolvers(),
}
}
// Thing returns the Thing resolver set.
func (g graphRoot) Thing() graph.ThingResolver {
return composedThingResolver{
g.core.ThingResolvers(),
g.alpha.ThingResolvers(),
}
}
`
const alphaManifest = `{
"id": "alpha", "name": "Alpha",
"backend": "example.com/myapp/plugins/alpha", "graphql": true
}`
var packageConfig = Config{
ExecImport: "example.com/myapp/graph",
CoreImport: "example.com/myapp/internal/graphres",
CoreSchemaGlobs: []string{"graph/schema/*.graphqls"},
WiringPath: "internal/graphroot/graphroot_gen.go",
License: "Apache-2.0",
Package: "graphroot",
SDKImport: "example.com/myapp/sdk",
}
// generatedPackage runs the generator in package mode and returns the wiring.
func generatedPackage(t *testing.T, root string) string {
t.Helper()
if err := os.MkdirAll(filepath.Join(root, "internal", "graphroot"), 0o755); err != nil {
t.Fatalf("creating the output directory: %v", err)
}
if err := Run(root, packageConfig); err != nil {
t.Fatalf("Run() error = %v, want nil", err)
}
content, err := os.ReadFile(filepath.Join(root, "internal", "graphroot", "graphroot_gen.go"))
if err != nil {
t.Fatalf("reading the wiring file: %v", err)
}
return string(content)
}
const zeroPluginPackageWiring = `// SPDX-License-Identifier: Apache-2.0
// Code generated by pluginwire. DO NOT EDIT.
package graphroot
import (
graph "example.com/myapp/graph"
sdk "example.com/myapp/sdk"
)
// NewGraphRoot returns the core resolver root, no plugin extends the graph.
func NewGraphRoot(core graph.ResolverRoot) graph.ResolverRoot {
return core
}
// FromPlugins composes the resolver root, no registered plugin extends the graph.
func FromPlugins(core graph.ResolverRoot, _ []sdk.Plugin) (graph.ResolverRoot, error) {
return core, nil
}
`
func TestPackageModeZeroPluginsExportsThePassthrough(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "feed",
`{"id": "feed", "name": "Feed", "backend": "example.com/myapp/plugins/feed"}`, "")
if got := generatedPackage(t, root); got != zeroPluginPackageWiring {
t.Errorf("wiring mismatch\n got:\n%s\nwant:\n%s", got, zeroPluginPackageWiring)
}
}
func TestPackageModeComposesAndAssemblesFromPlugins(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha", alphaManifest, alphaSchema)
got := generatedPackage(t, root)
wants := []string{
"package graphroot",
"type CoreGraphResolvers interface {",
"type AlphaGraphResolvers interface {",
"func NewGraphRoot(",
"core CoreGraphResolvers,",
"alphaPlugin AlphaGraphResolvers,",
"func FromPlugins(core CoreGraphResolvers, plugins []sdk.Plugin) (graph.ResolverRoot, error) {",
"if candidate, ok := plugin.(AlphaGraphResolvers); ok {",
`return nil, errors.New("graphroot: no registered plugin provides the alpha resolver sets")`,
}
for _, want := range wants {
if !strings.Contains(got, want) {
t.Errorf("wiring misses %q\n%s", want, got)
}
}
}
func TestFromPluginsLetsALaterPluginOverrideAnEarlierOne(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha", alphaManifest, alphaSchema)
got := generatedPackage(t, root)
unconditional := "if candidate, ok := plugin.(AlphaGraphResolvers); ok {\n\t\t\talphaPlugin = candidate\n\t\t}"
if !strings.Contains(got, unconditional) {
t.Errorf("wiring guards the assignment, a later plugin must override an earlier one\n%s", got)
}
loop := got[strings.Index(got, "for _, plugin := range plugins {"):]
loop = loop[:strings.Index(loop, "\n\t}")]
if strings.Contains(loop, "break") {
t.Errorf("the assignment loop breaks early, a later plugin must override an earlier one\n%s", loop)
}
}
func TestPackageModeRequiresTheSDKImport(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
incomplete := packageConfig
incomplete.SDKImport = ""
err := Run(root, incomplete)
if err == nil || !strings.Contains(err.Error(), "SDKImport") {
t.Errorf("Run() error = %v, want the missing SDKImport reported", err)
}
}
func TestPluginsComposeWithTheCoreSets(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha", alphaManifest, alphaSchema)
writePlugin(t, root, "beta",
`{"id": "beta", "name": "Beta", "backend": "example.com/myapp/plugins/beta", "graphql": true}`,
betaSchema)
if got := generated(t, root); got != composedWiring {
t.Errorf("wiring mismatch\n got:\n%s\nwant:\n%s", got, composedWiring)
}
}
func TestUnflaggedPluginsContributeNothing(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha", alphaManifest, alphaSchema)
writePlugin(t, root, "beta",
`{"id": "beta", "name": "Beta", "backend": "example.com/myapp/plugins/beta"}`,
betaSchema)
if got := generated(t, root); strings.Contains(got, "beta") {
t.Errorf("wiring mentions the unflagged beta plugin:\n%s", got)
}
}
func TestRunWiresAPluginFromASecondRoot(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`,
alphaSchema)
writePluginIn(t, root, "enterprise", "tenancy",
`{"id": "tenancy", "name": "Tenancy", "backend": "example.com/enterprise/tenancy", "graphql": true}`,
betaSchema)
cfg := testConfig
cfg.Roots = []string{"plugins", "enterprise"}
got := generatedWith(t, root, cfg)
if !strings.Contains(got, "example.com/myapp/plugins/alpha") ||
!strings.Contains(got, "example.com/enterprise/tenancy") {
t.Fatalf("wiring = %q, want contributors from both roots", got)
}
alphaAt := strings.Index(got, "alphaPlugin")
tenancyAt := strings.Index(got, "tenancyPlugin")
if alphaAt < 0 || tenancyAt < 0 {
t.Fatalf("wiring = %q, want composed parameters for both plugins", got)
}
if alphaAt > tenancyAt {
t.Errorf("wiring composes the enterprise root before plugins, want root order")
}
}
func TestRunEmptySecondRootReproducesDefaultBytes(t *testing.T) {
t.Parallel()
defaultRoot := t.TempDir()
overlayRoot := t.TempDir()
for _, root := range []string{defaultRoot, overlayRoot} {
writeCore(t, root)
writePlugin(t, root, "alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`,
alphaSchema)
}
if err := os.MkdirAll(filepath.Join(overlayRoot, "enterprise"), 0o755); err != nil {
t.Fatalf("creating the empty enterprise root: %v", err)
}
readme := filepath.Join(overlayRoot, "enterprise", "README.md")
if err := os.WriteFile(readme, []byte("enterprise plugins land here"), 0o644); err != nil {
t.Fatalf("writing the enterprise README: %v", err)
}
overlayConfig := testConfig
overlayConfig.Roots = []string{"plugins", "enterprise"}
defaultWiring := generated(t, defaultRoot)
overlayWiring := generatedWith(t, overlayRoot, overlayConfig)
if defaultWiring != overlayWiring {
t.Errorf("wiring differs between the default and the empty enterprise root")
}
}
func TestRunRejectsDuplicateIDAcrossRoots(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`,
alphaSchema)
writePluginIn(t, root, "enterprise", "alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/enterprise/alpha", "graphql": true}`,
alphaSchema)
cfg := testConfig
cfg.Roots = []string{"plugins", "enterprise"}
err := Run(root, cfg)
if err == nil {
t.Fatal("Run() error = nil, want a duplicate id error")
}
if !strings.Contains(err.Error(), "alpha") {
t.Errorf("error = %q, want it to name the duplicated id", err)
}
}
func TestRunReportsAMissingNamedRoot(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha",
`{"id": "alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`,
alphaSchema)
if err := os.MkdirAll(filepath.Join(root, "cmd", "myapp"), 0o755); err != nil {
t.Fatalf("creating the output directory: %v", err)
}
cfg := testConfig
cfg.Roots = []string{"plugins", "enterprise"}
if err := Run(root, cfg); err == nil {
t.Fatal("Run() error = nil, want a missing root error")
}
}
func TestUnreadableSDLFailsLoudly(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha", alphaManifest, "type {{{ nope")
err := Run(root, testConfig)
if err == nil || !strings.Contains(err.Error(), "schema.graphqls") {
t.Errorf("Run() error = %v, want the failing SDL path named", err)
}
}
func TestFlaggedPluginWithoutSchemaFails(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha", alphaManifest, "")
err := Run(root, testConfig)
if err == nil || !strings.Contains(err.Error(), "graph/*.graphqls") {
t.Errorf("Run() error = %v, want the missing SDL reported", err)
}
}
func TestRunRefusesAPluginIDCollidingWithTheWiring(t *testing.T) {
t.Parallel()
modes := []struct {
name string
cfg Config
output string
ids []string
}{
{"main mode", testConfig, filepath.Join("cmd", "myapp"),
[]string{"core", "graph", "graphres", "init", "main", "type"}},
{"package mode", packageConfig, filepath.Join("internal", "graphroot"),
[]string{"core", "graph", "graphres", "init", "sdk", "errors", "error", "nil", "type"}},
}
for _, mode := range modes {
for _, id := range mode.ids {
t.Run(mode.name+" "+id, func(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, id,
`{"id": "`+id+`", "name": "Colliding", "backend": "example.com/myapp/plugins/`+id+`", "graphql": true}`,
betaSchema)
if err := os.MkdirAll(filepath.Join(root, mode.output), 0o755); err != nil {
t.Fatalf("creating the output directory: %v", err)
}
wiring := filepath.Join(root, filepath.FromSlash(mode.cfg.WiringPath))
if err := os.WriteFile(wiring, []byte("earlier wiring\n"), 0o644); err != nil {
t.Fatalf("writing the earlier wiring: %v", err)
}
err := Run(root, mode.cfg)
want := "graphwire: plugin " + id + ": its Go name " + id + " collides with the generated wiring"
if err == nil || err.Error() != want {
t.Errorf("Run() error = %v, want %q", err, want)
}
if kept, _ := os.ReadFile(wiring); string(kept) != "earlier wiring\n" {
t.Errorf("wiring after the refusal = %q, want the earlier file untouched", kept)
}
})
}
}
}
func TestRunAcceptsAnIDOnlyTheOtherModeOwns(t *testing.T) {
t.Parallel()
modes := []struct {
name string
cfg Config
output string
ids []string
}{
{"main mode", testConfig, filepath.Join("cmd", "myapp"), []string{"sdk", "errors", "error", "nil"}},
{"package mode", packageConfig, filepath.Join("internal", "graphroot"), []string{"main"}},
}
for _, mode := range modes {
for _, id := range mode.ids {
t.Run(mode.name+" "+id, func(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, id,
`{"id": "`+id+`", "name": "Usable", "backend": "example.com/myapp/plugins/`+id+`", "graphql": true}`,
betaSchema)
if err := os.MkdirAll(filepath.Join(root, mode.output), 0o755); err != nil {
t.Fatalf("creating the output directory: %v", err)
}
if err := Run(root, mode.cfg); err != nil {
t.Errorf("Run() error = %v, want nil", err)
}
})
}
}
}
func TestRunRefusesAHyphenatedIDWhoseGoNameIsTheCoreImport(t *testing.T) {
t.Parallel()
for _, core := range []string{"example.com/myapp/internal/graph_res", "example.com/myapp/internal/graph-res"} {
t.Run(core, func(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "graph-res",
`{"id": "graph-res", "name": "Colliding", "backend": "example.com/myapp/plugins/graph-res", "graphql": true}`,
betaSchema)
cfg := packageConfig
cfg.CoreImport = core
err := Run(root, cfg)
want := "graphwire: plugin graph-res: its Go name graph_res collides with the generated wiring"
if err == nil || err.Error() != want {
t.Errorf("Run() error = %v, want %q", err, want)
}
})
}
}
func TestACoreImportWithATrailingSlashKeepsOneName(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "graphres",
`{"id": "graphres", "name": "Colliding", "backend": "example.com/myapp/plugins/graphres", "graphql": true}`,
betaSchema)
cfg := packageConfig
cfg.CoreImport = "example.com/myapp/internal/graphres/"
err := Run(root, cfg)
want := "graphwire: plugin graphres: its Go name graphres collides with the generated wiring"
if err == nil || err.Error() != want {
t.Errorf("Run() error = %v, want %q", err, want)
}
imports := wiringImports(cfg, []contributor{{alias: "beta", path: "example.com/myapp/plugins/beta"}}, namingFor(cfg))
if !slices.Contains(imports, imported{"graphres", cfg.CoreImport}) {
t.Errorf("wiringImports() = %v, want the core imported as graphres", imports)
}
}
func TestGraphQLPluginsRequireABackend(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeCore(t, root)
writePlugin(t, root, "alpha",
`{"id": "alpha", "name": "Alpha", "frontend": "@myapp/alpha", "graphql": true}`,
alphaSchema)
err := Run(root, testConfig)
if err == nil || !strings.Contains(err.Error(), "backend") {
t.Errorf("Run() error = %v, want the missing backend reported", err)
}
}