raz-core 0.2.4

Universal command generator for Rust projects - Core library with stateless file analysis and cursor-aware execution
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
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
697
698
699
//! Regression test suite for RAZ command generation
//!
//! This file contains integration tests for all the edge cases and bugs we've fixed.
//! These tests ensure that we don't regress on previously fixed issues when making changes.

use raz_core::Position;
use raz_core::file_detection::FileDetector;
use raz_core::universal_command_generator::UniversalCommandGenerator;
use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// Test for build.rs special file handling
/// Issue: RAZ was generating "cargo run --bin build" for build.rs files
/// Fix: build.rs files should be treated as special BuildScript files
#[test]
fn test_build_rs_special_handling() {
    let temp_dir = TempDir::new().unwrap();

    // Test 1: build.rs in a cargo project
    let cargo_toml = r#"
[package]
name = "test-project"
version = "0.1.0"
edition = "2021"
"#;
    fs::write(temp_dir.path().join("Cargo.toml"), cargo_toml).unwrap();

    let build_rs = r#"
fn main() {
    println!("cargo:rerun-if-changed=build.rs");
}
"#;
    let build_path = temp_dir.path().join("build.rs");
    fs::write(&build_path, build_rs).unwrap();

    fs::create_dir(temp_dir.path().join("src")).unwrap();
    fs::write(temp_dir.path().join("src/main.rs"), "fn main() {}").unwrap();

    let context = FileDetector::detect_context(&build_path, None).unwrap();
    let commands = UniversalCommandGenerator::generate_commands(&context, None).unwrap();

    // Should NOT generate "cargo run --bin build"
    assert!(
        !commands.iter().any(|c| {
            c.command == "cargo"
                && c.args.contains(&"run".to_string())
                && c.args.contains(&"--bin".to_string())
                && c.args.contains(&"build".to_string())
        }),
        "Should not generate 'cargo run --bin build' for build.rs files"
    );

    // Should generate proper build commands
    assert!(
        commands
            .iter()
            .any(|c| c.id == "build-script-trigger" || c.id == "build-script-direct"),
        "Should generate build script commands"
    );

    // Test 2: Standalone build.rs file
    let temp_dir2 = TempDir::new().unwrap();
    let standalone_build = temp_dir2.path().join("build.rs");
    fs::write(&standalone_build, build_rs).unwrap();

    let context2 = FileDetector::detect_context(&standalone_build, None).unwrap();
    let commands2 = UniversalCommandGenerator::generate_commands(&context2, None).unwrap();

    assert!(
        commands2
            .iter()
            .any(|c| c.command.contains("rustc") || c.args.iter().any(|a| a.contains("rustc"))),
        "Standalone build.rs should use rustc"
    );
}

/// Test for cursor-based test selection
/// Issue: Cursor inside test function body was running all tests in module
/// Fix: Check if cursor is within test function's line range
#[test]
fn test_cursor_inside_test_function() {
    let temp_dir = TempDir::new().unwrap();
    let project_name = "cursor-test-project";

    // Create cargo project
    let cargo_toml = format!(
        r#"
[package]
name = "{project_name}"
version = "0.1.0"
edition = "2021"
"#
    );
    fs::write(temp_dir.path().join("Cargo.toml"), cargo_toml).unwrap();

    // Create lib.rs with test module
    fs::create_dir(temp_dir.path().join("src")).unwrap();
    let lib_content = r#"
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
    
    #[test]
    fn it_never_works() {
        assert!(false);  // Line 11 - cursor will be here
    }
    
    #[test]
    fn another_test() {
        assert!(true);
    }
}
"#;
    let lib_path = temp_dir.path().join("src/lib.rs");
    fs::write(&lib_path, lib_content).unwrap();

    // Test cursor inside it_never_works function body (line 11)
    let cursor_pos = Position {
        line: 11,
        column: 20,
    };
    let context = FileDetector::detect_context(&lib_path, Some(cursor_pos)).unwrap();
    let commands =
        UniversalCommandGenerator::generate_commands(&context, Some(cursor_pos)).unwrap();

    // Debug: print all commands
    println!("Generated {} commands:", commands.len());
    for cmd in &commands {
        println!(
            "  - {} ({}): category={:?}",
            cmd.label, cmd.id, cmd.category
        );
    }

    // Find the test command
    let test_cmd = commands
        .iter()
        .find(|c| c.category == raz_core::CommandCategory::Test)
        .expect("Should generate test command");

    // Should run only the specific test, not all tests in module
    assert!(
        test_cmd.args.contains(&"tests::it_never_works".to_string()),
        "Should target specific test function"
    );
    assert!(
        test_cmd.args.contains(&"--exact".to_string()),
        "Should use --exact flag for specific test"
    );

    // Should NOT run all tests in module
    assert!(
        !test_cmd
            .args
            .iter()
            .any(|arg| arg == "tests::" || arg == "tests::*"),
        "Should not run all tests in module when cursor is inside a specific test"
    );
}

/// Test for nested module path construction
/// Issue: Nested modules like src/power/over/level_900.rs were treated as standalone files
/// Fix: Properly detect module files and construct correct module paths
#[test]
fn test_nested_module_path_construction() {
    let temp_dir = TempDir::new().unwrap();
    let project_name = "nested-module-project";

    // Create cargo project
    let cargo_toml = format!(
        r#"
[package]
name = "{project_name}"
version = "0.1.0"
edition = "2021"
"#
    );
    fs::write(temp_dir.path().join("Cargo.toml"), cargo_toml).unwrap();

    // Create nested module structure
    fs::create_dir_all(temp_dir.path().join("src/power/over")).unwrap();

    // Create lib.rs that declares the module
    let lib_content = r#"
mod power;

pub fn fibonacci(n: u64) -> u64 {
    match n {
        0 => 1,
        1 => 1,
        n => fibonacci(n - 1) + fibonacci(n - 2),
    }
}
"#;
    fs::write(temp_dir.path().join("src/lib.rs"), lib_content).unwrap();

    // Create power/mod.rs
    fs::write(temp_dir.path().join("src/power/mod.rs"), "pub mod over;").unwrap();

    // Create power/over/mod.rs
    fs::write(
        temp_dir.path().join("src/power/over/mod.rs"),
        "pub mod level_900;",
    )
    .unwrap();

    // Create the nested module file
    let level_900_content = r#"
#[cfg(test)]
mod tests {
    use crate::fibonacci;
    
    #[test]
    fn it_works() {
        assert_eq!(fibonacci(10), 89);
    }
    
    #[test]
    fn it_never_works() {
        assert!(false);  // Line 12
    }
}
"#;
    let nested_path = temp_dir.path().join("src/power/over/level_900.rs");
    fs::write(&nested_path, level_900_content).unwrap();

    // Test with cursor on line 12 (inside it_never_works)
    let cursor_pos = Position {
        line: 12,
        column: 20,
    };
    let context = FileDetector::detect_context(&nested_path, Some(cursor_pos)).unwrap();
    let commands =
        UniversalCommandGenerator::generate_commands(&context, Some(cursor_pos)).unwrap();

    // Debug: print all commands
    println!("Nested module - Generated {} commands:", commands.len());
    for cmd in &commands {
        println!(
            "  - {} ({}): category={:?}",
            cmd.label, cmd.id, cmd.category
        );
    }

    // Find test command
    let test_cmd = commands
        .iter()
        .find(|c| c.category == raz_core::CommandCategory::Test)
        .expect("Should generate test command");

    // Should use cargo test, NOT rustc
    assert_eq!(
        test_cmd.command, "cargo",
        "Should use cargo for module files"
    );

    // Should have correct module path
    assert!(
        test_cmd
            .args
            .contains(&"power::over::level_900::tests::it_never_works".to_string()),
        "Should construct full module path for nested modules"
    );

    // Debug: print the actual command
    println!(
        "Test command: {} {}",
        test_cmd.command,
        test_cmd.args.join(" ")
    );

    // The important thing is that it uses cargo test with the correct module path
    // The --lib flag is not always necessary for module files
}

/// Test for binary file test commands
/// Issue: Binary files in src/bin/ had wrong test paths (bin::name::tests instead of just tests)
/// Fix: Use --bin flag and correct test path for binaries
#[test]
fn test_binary_file_test_commands() {
    let temp_dir = TempDir::new().unwrap();
    let project_name = "binary-test-project";

    // Create cargo project
    let cargo_toml = format!(
        r#"
[package]
name = "{project_name}"
version = "0.1.0"
edition = "2021"
"#
    );
    fs::write(temp_dir.path().join("Cargo.toml"), cargo_toml).unwrap();

    // Create directories
    fs::create_dir_all(temp_dir.path().join("src/bin")).unwrap();

    // Create a simple lib.rs
    fs::write(temp_dir.path().join("src/lib.rs"), "// lib").unwrap();

    // Create binary file with tests
    let bogdan_content = r#"
fn main() {
    println!("this is bogdan");
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_main() {
        assert!(true);
    }
    
    #[test]
    fn test_power() {
        assert!(false);  // Line 16
    }
}
"#;
    let binary_path = temp_dir.path().join("src/bin/bogdan.rs");
    fs::write(&binary_path, bogdan_content).unwrap();

    // Test with cursor on line 16 (inside test_power)
    let cursor_pos = Position {
        line: 16,
        column: 20,
    };
    let context = FileDetector::detect_context(&binary_path, Some(cursor_pos)).unwrap();
    let commands =
        UniversalCommandGenerator::generate_commands(&context, Some(cursor_pos)).unwrap();

    // Debug: print all commands
    println!("Binary test - Generated {} commands:", commands.len());
    for cmd in &commands {
        println!(
            "  - {} ({}): category={:?}",
            cmd.label, cmd.id, cmd.category
        );
    }

    // Find test command
    let test_cmd = commands
        .iter()
        .find(|c| c.category == raz_core::CommandCategory::Test)
        .expect("Should generate test command for binary");

    // Should use cargo test
    assert_eq!(
        test_cmd.command, "cargo",
        "Should use cargo for binary tests"
    );

    // Should have --bin flag
    assert!(
        test_cmd.args.contains(&"--bin".to_string()),
        "Should have --bin flag"
    );
    assert!(
        test_cmd.args.contains(&"bogdan".to_string()),
        "Should specify binary name"
    );

    // Test path should be just "tests::test_power", NOT "bin::bogdan::tests::test_power"
    assert!(
        test_cmd.args.contains(&"tests::test_power".to_string()),
        "Binary test path should not include bin:: prefix"
    );

    // Should have --show-output
    assert!(
        test_cmd.args.contains(&"--show-output".to_string()),
        "Should include --show-output for better test visibility"
    );
}

/// Test tree-sitter attribute detection
/// Issue: Test attributes are siblings, not children of function nodes
/// Fix: Check previous sibling for test attributes
#[test]
fn test_tree_sitter_attribute_detection() {
    #[cfg(feature = "tree-sitter-support")]
    {
        use raz_core::tree_sitter_test_detector::TreeSitterTestDetector;

        let content = r#"
#[test]
fn my_test() {
    assert!(true);
}

#[tokio::test]
async fn async_test() {
    assert!(true);
}

#[cfg(test)]
mod tests {
    #[test]
    fn nested_test() {
        assert!(true);
    }
}
"#;

        let mut detector = TreeSitterTestDetector::new().unwrap();
        let entries = detector.detect_entry_points(content, None).unwrap();

        // Should detect all test functions
        assert!(
            entries.iter().any(|e| e.name == "my_test"),
            "Should detect #[test] attribute"
        );
        assert!(
            entries.iter().any(|e| e.name == "async_test"),
            "Should detect #[tokio::test] attribute"
        );
        assert!(
            entries.iter().any(|e| e.name == "tests"),
            "Should detect test module"
        );
    }
}

/// Integration test that actually executes commands
/// This test creates real projects and runs the generated commands to ensure they work
#[test]
#[ignore] // Ignore by default as it's slower and requires cargo
fn test_command_execution_integration() {
    let temp_dir = TempDir::new().unwrap();

    // Create a test project
    let cargo_toml = r#"
[package]
name = "integration-test"
version = "0.1.0"
edition = "2021"
"#;
    fs::write(temp_dir.path().join("Cargo.toml"), cargo_toml).unwrap();

    // Create src directory and files
    fs::create_dir(temp_dir.path().join("src")).unwrap();

    // Test 1: Library with specific test
    let lib_content = r#"
#[cfg(test)]
mod tests {
    #[test]
    fn specific_test() {
        assert_eq!(1 + 1, 2);
    }
    
    #[test]
    fn another_test() {
        panic!("This should not run");
    }
}
"#;
    let lib_path = temp_dir.path().join("src/lib.rs");
    fs::write(&lib_path, lib_content).unwrap();

    // Generate command for specific test
    let cursor = Position {
        line: 5,
        column: 20,
    }; // Inside specific_test
    let context = FileDetector::detect_context(&lib_path, Some(cursor)).unwrap();
    let commands = UniversalCommandGenerator::generate_commands(&context, Some(cursor)).unwrap();

    // Debug commands
    println!("Integration test - Generated {} commands:", commands.len());
    for cmd in &commands {
        println!(
            "  - {} ({}): category={:?}",
            cmd.label, cmd.id, cmd.category
        );
    }

    let test_cmd = commands
        .iter()
        .find(|c| c.category == raz_core::CommandCategory::Test)
        .expect("Should find test command");

    // Execute the command
    let output = Command::new(&test_cmd.command)
        .args(&test_cmd.args)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to execute command");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Verify the command succeeded and ran only the specific test
    assert!(
        output.status.success(),
        "Command should succeed. stdout: {stdout}, stderr: {stderr}"
    );
    assert!(
        stdout.contains("1 passed") || stderr.contains("1 passed"),
        "Should run exactly one test"
    );
    assert!(
        !stdout.contains("another_test"),
        "Should not run the other test"
    );

    // Test 2: Binary with tests
    fs::create_dir(temp_dir.path().join("src/bin")).unwrap();
    let binary_content = r#"
fn main() {
    println!("Binary");
}

#[cfg(test)]
mod tests {
    #[test]
    fn bin_test() {
        assert!(true);
    }
}
"#;
    let bin_path = temp_dir.path().join("src/bin/mybin.rs");
    fs::write(&bin_path, binary_content).unwrap();

    let cursor2 = Position {
        line: 9,
        column: 20,
    }; // Inside bin_test
    let context2 = FileDetector::detect_context(&bin_path, Some(cursor2)).unwrap();
    let commands2 = UniversalCommandGenerator::generate_commands(&context2, Some(cursor2)).unwrap();

    let bin_test_cmd = commands2
        .iter()
        .find(|c| c.category == raz_core::CommandCategory::Test)
        .expect("Should find binary test command");

    // Execute binary test command
    let output2 = Command::new(&bin_test_cmd.command)
        .args(&bin_test_cmd.args)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to execute binary test command");

    assert!(
        output2.status.success(),
        "Binary test command should succeed. stdout: {}, stderr: {}",
        String::from_utf8_lossy(&output2.stdout),
        String::from_utf8_lossy(&output2.stderr)
    );
}

/// Test doc test cursor detection and prioritization
/// Issue: When cursor is in doc test, RAZ was running library tests instead of doc tests
/// Fix: Detect doc test cursor position and prioritize doc test commands
#[test]
fn test_doc_test_cursor_prioritization() {
    let temp_dir = TempDir::new().unwrap();

    let cargo_toml = r#"
[package]
name = "doc-test-priority-test"
version = "0.1.0"
edition = "2021"
"#;
    fs::write(temp_dir.path().join("Cargo.toml"), cargo_toml).unwrap();

    fs::create_dir(temp_dir.path().join("src")).unwrap();
    let lib_content = r#"/// This function adds two numbers
/// 
/// ```rust
/// use doc_test_priority_test::add;
/// let x = 10;  // Line 5 - cursor here
/// let y = 20;
/// let sum = add(x, y);
/// assert_eq!(sum, 30);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(add(2, 2), 4);
    }
}
"#;
    let lib_path = temp_dir.path().join("src/lib.rs");
    fs::write(&lib_path, lib_content).unwrap();

    // Cursor on line 5 (inside doc test)
    let cursor = Position {
        line: 5,
        column: 10,
    };
    let context = FileDetector::detect_context(&lib_path, Some(cursor)).unwrap();
    let commands = UniversalCommandGenerator::generate_commands(&context, Some(cursor)).unwrap();

    // Find highest priority command
    let highest_priority_cmd = commands.iter().max_by_key(|c| c.priority).unwrap();

    // Should prioritize doc test command when cursor is in doc test
    assert!(
        highest_priority_cmd.args.contains(&"--doc".to_string()),
        "Highest priority command should be doc test when cursor is in doc test"
    );
    assert!(
        highest_priority_cmd.args.contains(&"add".to_string()),
        "Should filter by function name (add)"
    );
    assert!(
        highest_priority_cmd
            .args
            .contains(&"--show-output".to_string()),
        "Should include --show-output flag"
    );

    // Verify it's not running library tests as highest priority
    assert!(
        !highest_priority_cmd.args.contains(&"--lib".to_string()),
        "Should not prioritize library tests when cursor is in doc test"
    );
}

/// Test module-level cursor positioning
/// Issue: Cursor in test module but not on specific test was running wrong tests
/// Fix: Properly detect when cursor is in module but not on specific test
#[test]
fn test_module_level_cursor_detection() {
    let temp_dir = TempDir::new().unwrap();

    let cargo_toml = r#"
[package]
name = "module-cursor-test"
version = "0.1.0"
edition = "2021"
"#;
    fs::write(temp_dir.path().join("Cargo.toml"), cargo_toml).unwrap();

    fs::create_dir(temp_dir.path().join("src")).unwrap();
    let lib_content = r#"
#[cfg(test)]
mod tests {
    // Line 3 - inside module but not in any test
    
    #[test]
    fn first_test() {
        assert!(true);
    }
    
    #[test]
    fn second_test() {
        assert!(true);
    }
}

#[cfg(test)]
mod other_tests {
    #[test]
    fn other_test() {
        assert!(true);
    }
}
"#;
    let lib_path = temp_dir.path().join("src/lib.rs");
    fs::write(&lib_path, lib_content).unwrap();

    // Cursor on line 3 - inside 'tests' module but not in any specific test
    let cursor = Position {
        line: 3,
        column: 10,
    };
    let context = FileDetector::detect_context(&lib_path, Some(cursor)).unwrap();
    let commands = UniversalCommandGenerator::generate_commands(&context, Some(cursor)).unwrap();

    let test_cmd = commands
        .iter()
        .find(|c| c.category == raz_core::CommandCategory::Test)
        .expect("Should generate test command");

    // Should run all tests in the 'tests' module, not 'other_tests'
    assert!(
        test_cmd.args.iter().any(|arg| arg.contains("tests::")),
        "Should target the 'tests' module"
    );
    assert!(
        !test_cmd.args.iter().any(|arg| arg.contains("other_tests")),
        "Should not target the 'other_tests' module when cursor is in 'tests' module"
    );
}