grpctestify 1.8.6

gRPC testing utility written in Rust
Documentation
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
// Run command - execute tests

use anyhow::Result;
use futures::stream::StreamExt;
use std::path::Path;
use std::sync::Arc;
use tracing::{info, warn};

use crate::cli::Cli;
use crate::cli::args::RunArgs;
use crate::config;
use crate::execution;
use crate::parser;
use crate::parser::ast::{SectionContent, SectionType};
use crate::report;
use crate::state::{TestMeta, TestResult, TestResults};
use crate::utils::FileUtils;

fn extract_test_meta(doc: &parser::ast::GctfDocument) -> TestMeta {
    let mut meta = doc
        .sections
        .iter()
        .find_map(|s: &parser::ast::Section| {
            if let SectionContent::Meta(m) = &s.content
                && s.section_type == SectionType::Meta
            {
                return Some(TestMeta::from_file_meta(m));
            }
            None
        })
        .unwrap_or_default();

    if meta.tags.is_empty() {
        for section in &doc.sections {
            if let Some(tag_attr) = section.get_attribute("tag") {
                for t in tag_attr.value.split(',') {
                    let trimmed = t.trim();
                    if !trimmed.is_empty() && !meta.tags.contains(&trimmed.to_string()) {
                        meta.tags.push(trimmed.to_string());
                    }
                }
            }
        }
    }
    if meta.owner.is_none() {
        for section in &doc.sections {
            if let Some(owner_attr) = section.get_attribute("owner") {
                meta.owner = Some(owner_attr.value.clone());
                break;
            }
        }
    }
    if meta.summary.is_none() {
        for section in &doc.sections {
            if let Some(summary_attr) = section.get_attribute("summary") {
                meta.summary = Some(summary_attr.value.clone());
                break;
            }
        }
    }

    meta
}

fn file_matches_meta(path: &Path, tags_include: &[String], skip_tags: &[String]) -> bool {
    let parse_result = parser::parse_with_recovery(path);
    let doc = parse_result.document;

    let meta = doc.sections.iter().find_map(|s: &parser::ast::Section| {
        if let SectionContent::Meta(m) = &s.content
            && s.section_type == SectionType::Meta
        {
            Some(m)
        } else {
            None
        }
    });

    let file_tags: Vec<&str> = meta
        .map(|m| m.tags.iter().map(|s| s.as_str()).collect())
        .unwrap_or_default();

    for tag in tags_include {
        if !file_tags.iter().any(|t| t == tag) {
            return false;
        }
    }

    if !skip_tags.is_empty() && file_tags.iter().any(|t| skip_tags.iter().any(|e| t == e)) {
        return false;
    }

    true
}

fn should_retry_message(message: &str) -> bool {
    let msg = message.to_ascii_lowercase();
    msg.contains("deadline")
        || msg.contains("timeout")
        || msg.contains("timed out")
        || msg.contains("connection refused")
        || msg.contains("connection reset")
        || msg.contains("transport")
        || msg.contains("unavailable")
        || msg.contains("broken pipe")
        || msg.contains("temporarily")
        || msg.contains("network")
}

pub async fn run_tests(cli: &Cli, args: &RunArgs) -> Result<()> {
    // Get parallel job count
    let parallel_jobs = cli.parallel_jobs();
    info!("Parallel jobs: {}", parallel_jobs);

    // Handle dry-run mode
    if args.dry_run {
        info!("Dry-run mode enabled");
    }

    if args.no_assert {
        info!("No-assert mode enabled (skipping assertions)");
    }

    // Collect test files
    let mut test_files = Vec::new();
    let exclude_patterns = &args.exclude;
    for path in &args.test_paths {
        if path.is_dir() {
            test_files.extend(FileUtils::collect_test_files(path, exclude_patterns));
        } else if path.is_file() {
            test_files.push(path.clone());
        }
    }

    // Filter by META tags if provided
    let has_meta_filters = !args.tags.is_empty() || !args.skip_tags.is_empty();

    if has_meta_filters {
        let tags_inc: Vec<String> = args
            .tags
            .iter()
            .flat_map(|t| t.split(','))
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();
        let tags_exc: Vec<String> = args
            .skip_tags
            .iter()
            .flat_map(|t| t.split(','))
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();

        test_files.retain(|path| file_matches_meta(path, &tags_inc, &tags_exc));

        info!("Filtered to {} test file(s) by META", test_files.len());
    }

    info!("Found {} test file(s)", test_files.len());

    if test_files.is_empty() {
        warn!("No test files found");
        return Ok(());
    }

    // Sort files
    FileUtils::sort_files(&mut test_files, &args.sort);

    if args.stream {
        // Silent mode - streaming output only
    } else if test_files.len() == 1 {
        println!(
            "ℹ️  INFO [{}]: Running 1 test sequentially...",
            chrono::Local::now().format("%H:%M:%S")
        );
    } else if parallel_jobs <= 1 {
        println!(
            "ℹ️  INFO [{}]: Running {} test(s) sequentially...",
            chrono::Local::now().format("%H:%M:%S"),
            test_files.len()
        );
    } else {
        println!(
            "ℹ️  INFO [{}]: Running {} test(s) in parallel (jobs: {})...",
            chrono::Local::now().format("%H:%M:%S"),
            test_files.len(),
            parallel_jobs
        );
    }

    // Setup Reporters
    let mut reporters: Vec<Box<dyn report::Reporter>> = Vec::new();

    // Create environment info
    let env_info = report::console::EnvironmentInfo {
        address: std::env::var(config::ENV_GRPCTESTIFY_ADDRESS)
            .unwrap_or_else(|_| config::default_address()),
        parallel_jobs,
        sort_mode: args.sort.clone(),
        dry_run: args.dry_run,
    };

    // Add streaming JSON reporter if --stream is enabled
    if args.stream {
        reporters.push(Box::new(report::StreamingJsonReporter::new(
            test_files.len(),
        )));
    } else {
        // Always add console reporter (unless streaming)
        let mode = match cli.progress_mode() {
            crate::cli::args::ProgressMode::Dots => report::ConsoleMode::Dots,
            crate::cli::args::ProgressMode::Verbose => report::ConsoleMode::Verbose,
            crate::cli::args::ProgressMode::None => report::ConsoleMode::Silent,
        };
        reporters.push(Box::new(report::ConsoleReporter::new(
            mode,
            test_files.len() as u64,
            env_info,
        )));
    }

    // Add file reporter if configured
    if let Some(format) = cli.log_format_mode() {
        if let Some(output_path) = &args.log_output {
            match format {
                crate::cli::LogFormat::Json => {
                    reporters.push(Box::new(report::JsonReporter::new(output_path.clone())));
                }
                crate::cli::LogFormat::JUnit => {
                    reporters.push(Box::new(report::JunitReporter::new(output_path.clone())));
                }
                crate::cli::LogFormat::Allure => {
                    reporters.push(Box::new(report::AllureReporter::new(output_path.clone())));
                }
                crate::cli::LogFormat::Yaml => {
                    reporters.push(Box::new(report::YamlReporter::new(output_path.clone())));
                }
                _ => {}
            }
        } else {
            warn!(
                "--log-format specified but --log-output is missing. File report will be skipped."
            );
        }
    }

    // Initialize state
    let mut test_results = TestResults::new();

    // Initialize Coverage Collector if requested
    let coverage_collector = if args.coverage {
        Some(Arc::new(report::CoverageCollector::new()))
    } else {
        None
    };

    // Execute tests
    let start_time = std::time::Instant::now();
    let runner = Arc::new(execution::TestRunner::new(
        args.dry_run,
        args.timeout,
        args.no_assert,
        args.write,
        cli.verbose,
        coverage_collector.clone(),
    ));

    // Move reporters to Arc
    let reporters: Arc<Vec<Box<dyn report::Reporter>>> = Arc::new(reporters);

    // Use a stream for bounded parallelism
    let stream = futures::stream::iter(test_files)
        .map(|file| {
            let runner = runner.clone();
            let reporters = reporters.clone();
            let file_path_str = file.to_string_lossy().to_string();
            let file_clone = file.clone();

            async move {
                // Notify start
                for r in reporters.iter() {
                    r.on_test_start(&file_path_str);
                }

                let test_start = std::time::Instant::now();
                let mut test_result = match run_single_test(
                    &runner,
                    &file_clone,
                    args.retry,
                    args.retry_delay,
                    args.no_retry,
                )
                .await
                {
                    Ok(res) => {
                        let call_duration = res.call_duration_ms;
                        let meta = res.meta;
                        match res.status {
                            execution::TestExecutionStatus::Pass => TestResult::pass_with_meta(
                                file_path_str.clone(),
                                0,
                                call_duration,
                                meta,
                            ),
                            execution::TestExecutionStatus::Fail(msg) => {
                                TestResult::fail_with_meta(
                                    file_path_str.clone(),
                                    msg,
                                    0,
                                    call_duration,
                                    meta,
                                )
                            }
                        }
                    }
                    Err(e) => TestResult::fail(
                        file_path_str.clone(),
                        format!("Execution error: {}", e),
                        0,
                        None,
                    ),
                };

                test_result.duration_ms = test_start.elapsed().as_millis() as u64;

                // Notify end
                for r in reporters.iter() {
                    r.on_test_end(&file_path_str, &test_result);
                }

                test_result
            }
        })
        .buffer_unordered(parallel_jobs);

    let results: Vec<TestResult> = stream.collect().await;

    // Collect results
    for result in results {
        test_results.add(result);
    }

    // Update metrics
    let total_duration = start_time.elapsed().as_millis() as u64;
    test_results.metrics.total_duration_ms = total_duration;
    test_results.metrics.parallel_jobs = parallel_jobs;

    // Notify suite end
    for r in reporters.iter() {
        r.on_suite_end(&test_results)?;
    }

    // Print Coverage Report if enabled
    if let Some(collector) = coverage_collector {
        if args.is_json_coverage() {
            let report = collector.generate_json_report();
            println!("{}", serde_json::to_string_pretty(&report)?);
        } else {
            let report = collector.generate_text_report();
            if !args.stream {
                println!("\n{}", report);
            }
        }
    }

    if !test_results.all_passed() {
        std::process::exit(1);
    }

    Ok(())
}

async fn run_single_test(
    runner: &execution::TestRunner,
    file: &std::path::Path,
    retry: u32,
    retry_delay: f64,
    no_retry: bool,
) -> Result<execution::TestExecutionResult> {
    // Parse document
    let doc = match parser::parse_gctf(file) {
        Ok(d) => d,
        Err(e) => {
            return Ok(execution::TestExecutionResult::fail(
                format!("Parse error: {}", e),
                None,
            ));
        }
    };

    // Extract META for reports
    let test_meta = extract_test_meta(&doc);

    // Validate document
    if let Err(e) = parser::validate_document(&doc) {
        return Ok(
            execution::TestExecutionResult::fail(format!("Validation error: {}", e), None)
                .with_meta(test_meta),
        );
    }

    let effective_runtime = match execution::runner_helpers::resolve_effective_runtime_options(
        &doc,
        execution::runner_helpers::CliRuntimeDefaults {
            timeout_seconds: 30,
            retry,
            retry_delay_seconds: retry_delay,
            no_retry,
        },
    ) {
        Ok(v) => v,
        Err(e) => {
            return Ok(execution::TestExecutionResult::fail(
                format!("Validation error: {}", e),
                None,
            )
            .with_meta(test_meta));
        }
    };

    let max_retries = if effective_runtime.no_retry.value {
        0
    } else {
        effective_runtime.retry.value
    };

    let mut attempt = 0u32;
    let result = loop {
        let current = runner.run_test(&doc).await?;

        let should_retry = match &current.status {
            execution::TestExecutionStatus::Pass => false,
            execution::TestExecutionStatus::Fail(msg) => should_retry_message(msg),
        };

        if !should_retry || attempt >= max_retries {
            break current;
        }

        attempt += 1;
        if effective_runtime.retry_delay_seconds.value > 0.0 {
            tokio::time::sleep(std::time::Duration::from_secs_f64(
                effective_runtime.retry_delay_seconds.value,
            ))
            .await;
        }
    };

    // Update file if requested
    if let Some(resp) = &result.captured_response
        && let Err(e) = crate::utils::file::update_test_file(file, &doc, resp)
    {
        return Ok(execution::TestExecutionResult::fail(
            format!("Failed to update test file: {}", e),
            result.call_duration_ms,
        )
        .with_meta(test_meta));
    }

    Ok(result.with_meta(test_meta))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::ast::{GctfAttribute, GctfDocument, Section, SectionContent, SectionType};

    #[test]
    fn test_extract_test_meta_from_file_meta() {
        let mut doc = GctfDocument::new("test.gctf".to_string());
        let file_meta = crate::parser::ast::FileMeta {
            name: Some("suite name".to_string()),
            tags: vec!["smoke".to_string()],
            owner: Some("team-a".to_string()),
            summary: Some("test summary".to_string()),
            links: vec![],
        };
        doc.sections.push(Section {
            section_type: SectionType::Meta,
            content: SectionContent::Meta(file_meta),
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 1,
            end_line: 5,
            attributes: vec![],
        });

        let meta = extract_test_meta(&doc);
        assert_eq!(meta.name, Some("suite name".to_string()));
        assert_eq!(meta.tags, vec!["smoke".to_string()]);
        assert_eq!(meta.owner, Some("team-a".to_string()));
        assert_eq!(meta.summary, Some("test summary".to_string()));
    }

    #[test]
    fn test_extract_test_meta_fallback_tags_from_attributes() {
        let mut doc = GctfDocument::new("test.gctf".to_string());
        doc.sections.push(Section {
            section_type: SectionType::Request,
            content: SectionContent::Empty,
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 1,
            end_line: 2,
            attributes: vec![GctfAttribute::new("tag", "smoke,integration")],
        });

        let meta = extract_test_meta(&doc);
        assert_eq!(
            meta.tags,
            vec!["smoke".to_string(), "integration".to_string()]
        );
    }

    #[test]
    fn test_extract_test_meta_no_fallback_when_meta_has_tags() {
        let file_meta = crate::parser::ast::FileMeta {
            tags: vec!["smoke".to_string()],
            ..Default::default()
        };
        let mut doc = GctfDocument::new("test.gctf".to_string());
        doc.sections.push(Section {
            section_type: SectionType::Meta,
            content: SectionContent::Meta(file_meta),
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 1,
            end_line: 2,
            attributes: vec![],
        });
        doc.sections.push(Section {
            section_type: SectionType::Request,
            content: SectionContent::Empty,
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 3,
            end_line: 4,
            attributes: vec![GctfAttribute::new("tag", "integration")],
        });

        let meta = extract_test_meta(&doc);
        assert_eq!(meta.tags, vec!["smoke".to_string()]);
    }

    #[test]
    fn test_extract_test_meta_fallback_owner_from_attributes() {
        let mut doc = GctfDocument::new("test.gctf".to_string());
        doc.sections.push(Section {
            section_type: SectionType::Request,
            content: SectionContent::Empty,
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 1,
            end_line: 2,
            attributes: vec![GctfAttribute::new("owner", "team-b")],
        });

        let meta = extract_test_meta(&doc);
        assert_eq!(meta.owner, Some("team-b".to_string()));
    }

    #[test]
    fn test_extract_test_meta_fallback_summary_from_attributes() {
        let mut doc = GctfDocument::new("test.gctf".to_string());
        doc.sections.push(Section {
            section_type: SectionType::Request,
            content: SectionContent::Empty,
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 1,
            end_line: 2,
            attributes: vec![GctfAttribute::new("summary", "quick test")],
        });

        let meta = extract_test_meta(&doc);
        assert_eq!(meta.summary, Some("quick test".to_string()));
    }

    #[test]
    fn test_extract_test_meta_dedup_tags() {
        let mut doc = GctfDocument::new("test.gctf".to_string());
        doc.sections.push(Section {
            section_type: SectionType::Request,
            content: SectionContent::Empty,
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 1,
            end_line: 2,
            attributes: vec![GctfAttribute::new("tag", "smoke")],
        });
        doc.sections.push(Section {
            section_type: SectionType::Response,
            content: SectionContent::Empty,
            inline_options: Default::default(),
            raw_content: String::new(),
            start_line: 3,
            end_line: 4,
            attributes: vec![GctfAttribute::new("tag", "smoke")],
        });

        let meta = extract_test_meta(&doc);
        assert_eq!(meta.tags, vec!["smoke".to_string()]);
    }

    #[test]
    fn test_extract_test_meta_empty() {
        let doc = GctfDocument::new("test.gctf".to_string());
        let meta = extract_test_meta(&doc);
        assert!(meta.is_empty());
    }
}