forked from graphprotocol/graph-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_commands.rs
More file actions
670 lines (590 loc) · 20.3 KB
/
cli_commands.rs
File metadata and controls
670 lines (590 loc) · 20.3 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
//! Standalone command tests for `gnd` CLI.
//!
//! These tests verify commands that don't require a running Graph Node:
//! - `gnd init` - Scaffold new subgraph
//! - `gnd add` - Add datasource to existing subgraph
//! - `gnd build` - Compile subgraph to WASM
//!
//! # Prerequisites
//!
//! - Build the gnd binary: `cargo build -p gnd`
//! - IPFS running on localhost:5001 (for some tests)
//!
//! # Running
//!
//! ```bash
//! just test-gnd-commands
//! ```
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
/// Get the path to the gnd binary
fn gnd_binary_path() -> PathBuf {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(manifest_dir)
.parent()
.unwrap()
.join("target")
.join("debug")
.join("gnd")
}
/// Get the path to test ABIs
fn test_abis_path() -> PathBuf {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(manifest_dir)
.parent()
.unwrap()
.join("tests")
.join("contracts")
.join("abis")
}
/// Verify the gnd binary exists
fn verify_gnd_binary() -> PathBuf {
let gnd_path = gnd_binary_path();
if !gnd_path.exists() {
panic!(
"gnd binary not found at {}. Run `cargo build -p gnd` first.",
gnd_path.display()
);
}
gnd_path
}
/// Run gnd command and return output
fn run_gnd(args: &[&str], cwd: &Path) -> std::process::Output {
let gnd = verify_gnd_binary();
Command::new(&gnd)
.args(args)
.current_dir(cwd)
.output()
.expect("Failed to execute gnd")
}
/// Run gnd command and assert it succeeds
fn run_gnd_success(args: &[&str], cwd: &Path) -> std::process::Output {
let output = run_gnd(args, cwd);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
panic!(
"gnd {:?} failed:\nstdout: {}\nstderr: {}",
args, stdout, stderr
);
}
output
}
// ============================================================================
// gnd indexer tests
// ============================================================================
#[test]
fn test_indexer_missing_binary_error() {
let gnd = verify_gnd_binary();
// Run with an empty PATH so graph-indexer cannot be found.
let output = Command::new(&gnd)
.args(["indexer", "status", "--network", "mainnet"])
.env("PATH", "")
.output()
.expect("Failed to execute gnd");
assert!(
!output.status.success(),
"gnd indexer should fail when graph-indexer is not on $PATH"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("graph-indexer") && stderr.contains("not found"),
"Error should mention graph-indexer not found. Got:\n{stderr}"
);
assert!(
stderr.contains("indexer-cli"),
"Error should mention indexer-cli install instructions. Got:\n{stderr}"
);
}
#[test]
fn test_indexer_clap_help() {
let gnd = verify_gnd_binary();
// `--help` is intercepted by clap, not forwarded to graph-indexer.
let output = Command::new(&gnd)
.args(["indexer", "--help"])
.env("PATH", "")
.output()
.expect("Failed to execute gnd");
assert!(output.status.success(), "gnd indexer --help should succeed");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("indexer-cli"),
"Help should mention indexer-cli. Got:\n{stdout}"
);
assert!(
stdout.contains("graph-indexer"),
"Help should mention graph-indexer. Got:\n{stdout}"
);
}
// ============================================================================
// gnd init tests
// ============================================================================
#[test]
fn test_init_from_example() {
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("my-subgraph");
// Run gnd init --from-example. We pass --skip-install since `pnpm
// install` for the example will ask for github credentials (unclear
// why)
let output = run_gnd(
&[
"init",
"--skip-install",
"--from-example",
"ethereum-gravatar",
"my-subgraph",
],
temp_dir.path(),
);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
// Example downloads from GitHub, may fail in CI without network
// Skip test if it fails due to network issues
if stderr.contains("network")
|| stderr.contains("fetch")
|| stderr.contains("download")
|| stderr.contains("Failed to fetch")
{
eprintln!("Skipping test_init_from_example: network unavailable");
return;
}
panic!(
"gnd init --from-example failed:\nstdout: {}\nstderr: {}",
stdout, stderr
);
}
// Verify scaffold was created
assert!(subgraph_dir.exists(), "Subgraph directory should exist");
assert!(
subgraph_dir.join("subgraph.yaml").exists(),
"subgraph.yaml should exist"
);
assert!(
subgraph_dir.join("schema.graphql").exists(),
"schema.graphql should exist"
);
assert!(
subgraph_dir.join("package.json").exists(),
"package.json should exist"
);
// Verify it's actually the ethereum-gravatar example (contains Gravatar entity)
let schema = fs::read_to_string(subgraph_dir.join("schema.graphql")).unwrap();
assert!(
schema.contains("Gravatar"),
"schema.graphql should contain Gravatar entity"
);
}
#[test]
fn test_init_from_example_invalid_name() {
let temp_dir = TempDir::new().unwrap();
let output = run_gnd(
&[
"init",
"--skip-install",
"--from-example",
"nonexistent-example-12345",
"test",
],
temp_dir.path(),
);
// Command should fail
assert!(!output.status.success());
let stderr = String::from_utf8_lossy(&output.stderr);
// Skip test if it fails due to network issues (can't verify example doesn't exist)
if stderr.contains("Failed to fetch from graph-tooling") {
eprintln!("Skipping test_init_from_example_invalid_name: network unavailable");
return;
}
// Should show helpful error message
assert!(
stderr.contains("not found") || stderr.contains("graph-tooling"),
"Error should mention example not found or link to graph-tooling. Got: {}",
stderr
);
}
#[test]
fn test_init_from_example_aggregations() {
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("my-agg");
let output = run_gnd(
&[
"init",
"--skip-install",
"--from-example",
"aggregations",
"my-agg",
],
temp_dir.path(),
);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
// Example downloads from GitHub, may fail in CI without network
if stderr.contains("network")
|| stderr.contains("fetch")
|| stderr.contains("download")
|| stderr.contains("Failed to fetch")
{
eprintln!("Skipping test_init_from_example_aggregations: network unavailable");
return;
}
panic!(
"gnd init --from-example aggregations failed:\nstdout: {}\nstderr: {}",
stdout, stderr
);
}
// Verify scaffold was created
assert!(subgraph_dir.exists(), "Subgraph directory should exist");
// Verify it's actually the aggregations example, not ethereum-gravatar
let manifest = fs::read_to_string(subgraph_dir.join("subgraph.yaml")).unwrap();
// The aggregations example uses specVersion 1.1.0 and has blockHandlers,
// whereas ethereum-gravatar uses 0.0.5 and has eventHandlers
assert!(
manifest.contains("specVersion: 1.1.0") || manifest.contains("blockHandlers"),
"Manifest should be the aggregations example (expected specVersion 1.1.0 or blockHandlers)"
);
}
#[test]
fn test_init_from_contract_with_abi() {
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("simple-subgraph");
// Get the path to SimpleContract ABI
let abi_path = test_abis_path().join("SimpleContract.json");
assert!(abi_path.exists(), "SimpleContract.json ABI should exist");
// Run gnd init with --from-contract and --abi
let output = run_gnd(
&[
"init",
"--from-contract",
"0x5fbdb2315678afecb367f032d93f642f64180aa3",
"--abi",
abi_path.to_str().unwrap(),
"--network",
"mainnet",
"--contract-name",
"SimpleContract",
"simple-subgraph",
],
temp_dir.path(),
);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
panic!(
"gnd init --from-contract failed:\nstdout: {}\nstderr: {}",
stdout, stderr
);
}
// Verify scaffold was created
assert!(subgraph_dir.exists(), "Subgraph directory should exist");
assert!(
subgraph_dir.join("subgraph.yaml").exists(),
"subgraph.yaml should exist"
);
assert!(
subgraph_dir.join("schema.graphql").exists(),
"schema.graphql should exist"
);
// Verify manifest contains the contract
let manifest = fs::read_to_string(subgraph_dir.join("subgraph.yaml")).unwrap();
assert!(
manifest.contains("0x5fbdb2315678afecb367f032d93f642f64180aa3"),
"Manifest should contain contract address"
);
assert!(
manifest.contains("SimpleContract"),
"Manifest should contain contract name"
);
// Verify networks.json was created with contract config
let networks_path = subgraph_dir.join("networks.json");
assert!(networks_path.exists(), "networks.json should exist");
let networks: serde_json::Value =
serde_json::from_str(&fs::read_to_string(&networks_path).unwrap()).unwrap();
assert!(
networks["mainnet"]["SimpleContract"]["address"]
.as_str()
.unwrap()
.contains("0x5fbdb2315678afecb367f032d93f642f64180aa3"),
"networks.json should contain contract address"
);
}
#[test]
fn test_init_creates_mapping_file() {
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("mapping-test");
// Get the path to SimpleContract ABI
let abi_path = test_abis_path().join("SimpleContract.json");
// Run gnd init
let output = run_gnd(
&[
"init",
"--from-contract",
"0x1234567890123456789012345678901234567890",
"--abi",
abi_path.to_str().unwrap(),
"--network",
"mainnet",
"--contract-name",
"TestContract",
"--index-events",
"mapping-test",
],
temp_dir.path(),
);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
panic!("gnd init failed:\nstdout: {}\nstderr: {}", stdout, stderr);
}
// Verify mapping file was created
let mapping_path = subgraph_dir.join("src").join("test-contract.ts");
assert!(
mapping_path.exists() || subgraph_dir.join("src").join("mapping.ts").exists(),
"Mapping file should exist"
);
}
// ============================================================================
// gnd add tests
// ============================================================================
#[test]
fn test_add_datasource() {
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("add-test");
// First create a subgraph with init
let abi_path = test_abis_path().join("SimpleContract.json");
run_gnd_success(
&[
"init",
"--from-contract",
"0x1111111111111111111111111111111111111111",
"--abi",
abi_path.to_str().unwrap(),
"--network",
"mainnet",
"--contract-name",
"FirstContract",
"add-test",
],
temp_dir.path(),
);
// Verify initial manifest
let manifest_before = fs::read_to_string(subgraph_dir.join("subgraph.yaml")).unwrap();
assert!(
manifest_before.contains("FirstContract"),
"Initial manifest should have FirstContract"
);
assert!(
!manifest_before.contains("SecondContract"),
"Initial manifest should not have SecondContract"
);
// Now add another datasource
let second_abi_path = test_abis_path().join("LimitedContract.json");
let output = run_gnd(
&[
"add",
"0x2222222222222222222222222222222222222222",
"--abi",
second_abi_path.to_str().unwrap(),
"--contract-name",
"SecondContract",
],
&subgraph_dir,
);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
panic!("gnd add failed:\nstdout: {}\nstderr: {}", stdout, stderr);
}
// Verify manifest was updated
let manifest_after = fs::read_to_string(subgraph_dir.join("subgraph.yaml")).unwrap();
assert!(
manifest_after.contains("FirstContract"),
"Updated manifest should still have FirstContract"
);
assert!(
manifest_after.contains("SecondContract"),
"Updated manifest should have SecondContract"
);
assert!(
manifest_after.contains("0x2222222222222222222222222222222222222222"),
"Updated manifest should have second contract address"
);
}
// ============================================================================
// gnd codegen tests
// ============================================================================
#[test]
fn test_codegen_generates_types() {
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("codegen-test");
// Create a subgraph
let abi_path = test_abis_path().join("SimpleContract.json");
run_gnd_success(
&[
"init",
"--from-contract",
"0x1234567890123456789012345678901234567890",
"--abi",
abi_path.to_str().unwrap(),
"--network",
"mainnet",
"--contract-name",
"SimpleContract",
"--index-events",
"codegen-test",
],
temp_dir.path(),
);
// Run codegen
let output = run_gnd(&["codegen", "--skip-migrations"], &subgraph_dir);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
panic!(
"gnd codegen failed:\nstdout: {}\nstderr: {}",
stdout, stderr
);
}
// Verify generated directory exists
let generated_dir = subgraph_dir.join("generated");
assert!(generated_dir.exists(), "generated/ directory should exist");
// Verify schema.ts was generated
assert!(
generated_dir.join("schema.ts").exists(),
"generated/schema.ts should exist"
);
}
// ============================================================================
// gnd build tests
// ============================================================================
#[test]
fn test_build_after_codegen() {
// Skip this test if asc is not installed
if Command::new("asc").arg("--version").output().is_err() {
eprintln!("Skipping test_build_after_codegen: asc not installed");
return;
}
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("build-test");
// Create a subgraph
let abi_path = test_abis_path().join("SimpleContract.json");
run_gnd_success(
&[
"init",
"--from-contract",
"0x1234567890123456789012345678901234567890",
"--abi",
abi_path.to_str().unwrap(),
"--network",
"mainnet",
"--contract-name",
"SimpleContract",
"--index-events",
"build-test",
],
temp_dir.path(),
);
// Install dependencies (required for codegen and build)
let npm_install = Command::new("npm")
.arg("install")
.current_dir(&subgraph_dir)
.output();
if npm_install.is_err() || !npm_install.unwrap().status.success() {
eprintln!("Skipping test_build_after_codegen: npm install failed");
return;
}
// Run codegen first to generate types
let codegen_output = run_gnd(&["codegen", "--skip-migrations"], &subgraph_dir);
if !codegen_output.status.success() {
let stdout = String::from_utf8_lossy(&codegen_output.stdout);
let stderr = String::from_utf8_lossy(&codegen_output.stderr);
if stderr.contains("Cannot find module") || stderr.contains("graph-ts") {
eprintln!("Skipping test_build_after_codegen: dependencies not available");
return;
}
panic!(
"gnd codegen failed:\nstdout: {}\nstderr: {}",
stdout, stderr
);
}
// Run build (codegen already done)
let output = run_gnd(&["build", "--skip-migrations"], &subgraph_dir);
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
// Build may fail if dependencies aren't set up correctly
// This is expected in some CI environments
if stderr.contains("Cannot find module") || stderr.contains("graph-ts") {
eprintln!("Skipping test_build_after_codegen: dependencies not available");
return;
}
panic!("gnd build failed:\nstdout: {}\nstderr: {}", stdout, stderr);
}
// Verify build directory exists
let build_dir = subgraph_dir.join("build");
assert!(build_dir.exists(), "build/ directory should exist");
// Verify WASM was generated
let wasm_files: Vec<_> = fs::read_dir(&build_dir)
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| {
e.path()
.extension()
.map(|ext| ext == "wasm")
.unwrap_or(false)
})
.collect();
assert!(
!wasm_files.is_empty(),
"build/ should contain at least one .wasm file"
);
}
// ============================================================================
// gnd clean tests
// ============================================================================
#[test]
fn test_clean_removes_artifacts() {
let temp_dir = TempDir::new().unwrap();
let subgraph_dir = temp_dir.path().join("clean-test");
// Create a subgraph
let abi_path = test_abis_path().join("SimpleContract.json");
run_gnd_success(
&[
"init",
"--from-contract",
"0x1234567890123456789012345678901234567890",
"--abi",
abi_path.to_str().unwrap(),
"--network",
"mainnet",
"--contract-name",
"SimpleContract",
"clean-test",
],
temp_dir.path(),
);
// Run codegen to create generated/
run_gnd_success(&["codegen", "--skip-migrations"], &subgraph_dir);
// Verify generated/ exists
assert!(
subgraph_dir.join("generated").exists(),
"generated/ should exist after codegen"
);
// Create a fake build/ directory
fs::create_dir(subgraph_dir.join("build")).unwrap();
assert!(subgraph_dir.join("build").exists(), "build/ should exist");
// Run clean
run_gnd_success(&["clean"], &subgraph_dir);
// Verify directories were removed
assert!(
!subgraph_dir.join("generated").exists(),
"generated/ should be removed after clean"
);
assert!(
!subgraph_dir.join("build").exists(),
"build/ should be removed after clean"
);
}