context-creator 1.5.0

High-performance CLI tool to convert codebases to Markdown for LLM context
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
700
701
702
703
704
705
706
#![cfg(test)]

//! Edge case tests for semantic analysis in markdown output

use std::fs;
use tempfile::TempDir;

/// Test circular imports are handled correctly
#[test]
fn test_circular_imports_handling() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create main.rs that declares the modules
    fs::write(
        src_dir.join("main.rs"),
        r#"
mod a;
mod b;
mod c;

fn main() {
    a::function_a();
}
"#,
    )
    .unwrap();

    // Create circular dependency: a.rs -> b.rs -> c.rs -> a.rs
    fs::write(
        src_dir.join("a.rs"),
        r#"
use crate::b::function_b;

pub fn function_a() {
    function_b();
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("b.rs"),
        r#"
use crate::c::function_c;

pub fn function_b() {
    function_c();
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("c.rs"),
        r#"
use crate::a::function_a;

pub fn function_c() {
    // This creates a circular dependency
    if false {
        function_a();
    }
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

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

    // Debug output
    eprintln!("STDOUT:\n{stdout}");
    eprintln!("STDERR:\n{stderr}");
    eprintln!("Status: {:?}", output.status);

    // Should handle circular imports without crashing
    assert!(output.status.success(), "Should handle circular imports");

    // Check that main.rs imports the modules
    assert!(stdout.contains("main.rs"));
    assert!(
        stdout.contains("Imports: a, b, c"),
        "main.rs should import a, b, c"
    );

    // Check that modules are imported by main
    assert!(stdout.contains("a.rs") && stdout.contains("Imported by: main.rs"));
    assert!(stdout.contains("b.rs") && stdout.contains("Imported by: main.rs"));
    assert!(stdout.contains("c.rs") && stdout.contains("Imported by: main.rs"));
}

/// Test files with no imports/exports
#[test]
fn test_files_with_no_semantic_info() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create files with no imports or function calls
    fs::write(
        src_dir.join("standalone.rs"),
        r#"
// A file with no imports or exports
const VALUE: i32 = 42;

fn internal_function() {
    let x = VALUE + 1;
    let _y = x * 2;
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("constants.rs"),
        r#"
// Just constants, no functions or imports
pub const MAX_SIZE: usize = 1024;
pub const DEFAULT_NAME: &str = "default";
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .arg("--include-types")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should not show semantic sections for files without semantic info
    let standalone_section = stdout
        .split("## standalone.rs")
        .nth(1)
        .unwrap_or("")
        .split("##")
        .next()
        .unwrap_or("");

    assert!(!standalone_section.contains("Imports:"));
    assert!(!standalone_section.contains("Imported by:"));
    assert!(!standalone_section.contains("Function calls:"));
}

/// Test files with many imports
#[test]
fn test_file_with_many_imports() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create a file with many imports
    fs::write(
        src_dir.join("main.rs"),
        r#"
use std::collections::HashMap;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

mod utils;
mod helpers;
mod config;
mod database;
mod api;

use utils::process;
use helpers::{format_output, validate_input};
use config::Settings;
use database::Connection;
use api::{Client, Response};

fn main() {
    process();
    format_output("test");
    validate_input("data");
}
"#,
    )
    .unwrap();

    // Create the imported modules
    for module in &["utils", "helpers", "config", "database", "api"] {
        fs::write(
            src_dir.join(format!("{module}.rs")),
            format!(
                r#"
pub fn {}() {{
    println!("{} module");
}}
"#,
                if module == &"utils" {
                    "process"
                } else {
                    "function"
                },
                module
            ),
        )
        .unwrap();
    }

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should show multiple imports
    assert!(stdout.contains("main.rs"));
    assert!(
        stdout.contains("Imports:") && stdout.contains("utils, helpers, config, database, api")
    );

    // Each module should show it's imported by main
    assert!(stdout.contains("utils.rs") && stdout.contains("Imported by: main.rs"));
    assert!(stdout.contains("helpers.rs") && stdout.contains("Imported by: main.rs"));
}

/// Test deeply nested module structure
#[test]
fn test_deeply_nested_modules() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");

    // Create nested directory structure
    let deep_path = src_dir.join("core").join("semantic").join("analyzer");
    fs::create_dir_all(&deep_path).unwrap();

    fs::write(
        src_dir.join("main.rs"),
        r#"
mod core;
use core::semantic::analyzer::analyze;

fn main() {
    analyze();
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("core").join("mod.rs"),
        r#"
pub mod semantic;
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("core").join("semantic").join("mod.rs"),
        r#"
pub mod analyzer;
"#,
    )
    .unwrap();

    fs::write(
        deep_path.join("mod.rs"),
        r#"
pub fn analyze() {
    println!("Analyzing...");
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should handle nested module imports
    assert!(output.status.success());
    assert!(stdout.contains("main.rs"));
    // The semantic analyzer should track the deep import chain
}

/// Test file with same name in different directories
#[test]
fn test_same_filename_different_dirs() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    let tests_dir = temp_dir.path().join("tests");
    fs::create_dir_all(&src_dir).unwrap();
    fs::create_dir_all(&tests_dir).unwrap();

    // Create lib.rs in both directories
    fs::write(
        src_dir.join("lib.rs"),
        r#"
pub fn library_function() {
    println!("Source library");
}
"#,
    )
    .unwrap();

    // Create a Cargo.toml to make this a proper Rust project
    fs::write(
        temp_dir.path().join("Cargo.toml"),
        r#"
[package]
name = "test_project"
version = "0.1.0"
edition = "2021"
"#,
    )
    .unwrap();

    fs::write(
        tests_dir.join("lib.rs"),
        r#"
// Test library that imports the main library
use test_project::library_function;

#[test]
fn test_library() {
    library_function();
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(temp_dir.path())
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should distinguish between files with same name
    assert!(stdout.contains("src/lib.rs") || stdout.contains("src\\lib.rs"));
    assert!(stdout.contains("tests/lib.rs") || stdout.contains("tests\\lib.rs"));
}

/// Test with type references edge cases
#[test]
fn test_complex_type_references() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("types.rs"),
        r#"
use std::collections::{HashMap, HashSet, BTreeMap};
use std::sync::{Arc, Mutex, RwLock};

pub type StringMap = HashMap<String, String>;
pub type ThreadSafeCounter = Arc<Mutex<i32>>;
pub type ConcurrentSet<T> = Arc<RwLock<HashSet<T>>>;

pub struct ComplexType<T, U> 
where 
    T: Clone + Send,
    U: Default
{
    data: HashMap<T, Vec<U>>,
    cache: BTreeMap<String, T>,
}

impl<T: Clone + Send, U: Default> ComplexType<T, U> {
    pub fn new() -> Self {
        Self {
            data: HashMap::new(),
            cache: BTreeMap::new(),
        }
    }
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--include-types")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should track type references
    assert!(output.status.success());
    if stdout.contains("Type references:") {
        // Should include standard library types used
        assert!(stdout.contains("HashMap") || stdout.contains("HashSet"));
    }
}

/// Test with function calls including method calls
#[test]
fn test_various_function_call_patterns() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("calls.rs"),
        r#"
use std::fs;
use std::path::Path;

mod utils;
use utils::{helper, process_data};

fn main() {
    // Direct function calls
    helper();
    process_data("input");
    
    // Method calls
    let path = Path::new("test.txt");
    path.exists();
    path.is_file();
    
    // Associated function calls
    fs::read_to_string("file.txt").unwrap();
    String::from("hello");
    
    // Chained calls
    vec![1, 2, 3]
        .iter()
        .map(|x| x * 2)
        .filter(|x| x > &2)
        .collect::<Vec<_>>();
    
    // Closure calls
    let closure = |x| x + 1;
    closure(5);
    
    // Macro calls (might not be tracked)
    println!("Hello");
    vec![1, 2, 3];
}
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("utils.rs"),
        r#"
pub fn helper() {
    println!("Helper");
}

pub fn process_data(input: &str) {
    println!("Processing: {}", input);
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should track various function call patterns
    assert!(output.status.success());
    if stdout.contains("Function calls:") {
        // Should at least track direct function calls
        assert!(stdout.contains("helper()") || stdout.contains("process_data()"));
    }
}

/// Test empty directory
#[test]
fn test_empty_directory() {
    let temp_dir = TempDir::new().unwrap();
    let empty_dir = temp_dir.path().join("empty");
    fs::create_dir_all(&empty_dir).unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&empty_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .arg("--include-types")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle empty directory gracefully
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Total files: 0"));
}

/// Test with invalid UTF-8 in file names (if supported by OS)
#[test]
#[cfg(unix)]
fn test_non_utf8_filenames() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create a file with valid UTF-8 content but potentially problematic name
    fs::write(
        src_dir.join("файл.rs"), // Cyrillic characters
        r#"
pub fn function() {
    println!("Non-ASCII filename");
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

    // Should handle non-ASCII filenames
    assert!(output.status.success());
}

/// Test files that import from parent directories
#[test]
fn test_parent_directory_imports() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    let sub_dir = src_dir.join("submodule");
    fs::create_dir_all(&sub_dir).unwrap();

    // Create a .git directory to ensure proper project root detection
    fs::create_dir_all(temp_dir.path().join(".git")).unwrap();

    // Create Cargo.toml to make this a proper Rust project
    fs::write(
        temp_dir.path().join("Cargo.toml"),
        r#"
[package]
name = "test_project"
version = "0.1.0"
edition = "2021"
"#,
    )
    .unwrap();

    fs::write(
        src_dir.join("lib.rs"),
        r#"
pub mod submodule;

pub fn parent_function() {
    println!("Parent");
}
"#,
    )
    .unwrap();

    fs::write(
        sub_dir.join("mod.rs"),
        r#"
use super::parent_function;

pub fn child_function() {
    parent_function();
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-callers")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should track imports from parent modules
    assert!(output.status.success());
    assert!(stdout.contains("submodule/mod.rs") || stdout.contains("submodule\\mod.rs"));
}

/// Test with external crate imports (won't be tracked but shouldn't crash)
#[test]
fn test_external_crate_imports() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    fs::write(
        src_dir.join("external.rs"),
        r#"
// These external crates won't be in our codebase
use serde::{Serialize, Deserialize};
use tokio::runtime::Runtime;
use anyhow::{Result, Context};

#[derive(Serialize, Deserialize)]
pub struct Config {
    name: String,
    value: i32,
}

pub async fn async_function() -> Result<()> {
    Ok(())
}
"#,
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .arg("--include-types")
        .output()
        .expect("Failed to execute context-creator");

    // Should not crash on external imports
    assert!(output.status.success());
}

/// Test very long import lists
#[test]
fn test_very_long_import_list() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Create many modules
    let module_count = 50;
    let mut imports = String::new();
    let mut import_list = Vec::new();

    for i in 0..module_count {
        let module_name = format!("module_{i}");
        imports.push_str(&format!("mod {module_name};\n"));
        imports.push_str(&format!("use {module_name}::function_{i};\n"));
        import_list.push(module_name.clone());

        fs::write(
            src_dir.join(format!("{module_name}.rs")),
            format!(
                r#"
pub fn function_{i}() {{
    println!("Function {{i}}", {i});
}}
"#
            ),
        )
        .unwrap();
    }

    fs::write(
        src_dir.join("main.rs"),
        format!(
            r#"
{}

fn main() {{
    // Call all functions
    {}
}}
"#,
            imports,
            (0..module_count)
                .map(|i| format!("function_{i}();"))
                .collect::<Vec<_>>()
                .join("\n    ")
        ),
    )
    .unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_context-creator"))
        .arg(&src_dir)
        .arg("--trace-imports")
        .output()
        .expect("Failed to execute context-creator");

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

    // Should handle long import lists
    assert!(output.status.success());
    assert!(stdout.contains("main.rs"));
    assert!(stdout.contains("Imports:"));

    // The import list should be reasonably formatted
    let expected_imports = import_list.join(", ");
    assert!(stdout.contains(&expected_imports) || stdout.contains(&import_list[0]));
}