auto-env-generator 0.1.0

Fast, parallel Rust tool for automatically scanning codebases and generating .env files based on detected environment variable usage
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
//! Integration tests for auto-env-generator
//!
//! Tests the complete functionality including CLI, library API, configuration,
//! and edge cases for environment variable detection and .env file generation.

use auto_env_generator::{generate_env_file, generate_env_file_with_config, Config, EnvScanner};
use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

fn create_test_file(dir: &Path, path: &str, content: &str) -> std::io::Result<()> {
    let file_path = dir.join(path);
    if let Some(parent) = file_path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(file_path, content)
}

fn read_env_file(path: &Path) -> std::collections::HashMap<String, String> {
    let mut env_vars = std::collections::HashMap::new();

    if let Ok(content) = fs::read_to_string(path) {
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if let Some(eq_pos) = line.find('=') {
                let key = line[..eq_pos].trim().to_string();
                let value = line[eq_pos + 1..].trim().to_string();
                env_vars.insert(key, value);
            }
        }
    }

    env_vars
}

#[test]
fn test_detection_correctness_comprehensive() {
    let temp_dir = TempDir::new().unwrap();

    // Create a complex Rust file with various patterns
    let complex_content = r#"
use std::env;
use dotenv;

pub struct Config {
    database_url: String,
    redis_url: Option<String>,
}

impl Config {
    pub fn new() -> Self {
        Self {
            database_url: std::env::var("DATABASE_URL")
                .expect("DATABASE_URL must be set"),
            redis_url: env::var("REDIS_URL").ok(),
        }
    }

    pub fn load_secrets() {
        let api_key = dotenv::var("API_KEY").unwrap();
        let secret_key = std::env::var_os("SECRET_KEY").unwrap();
        let jwt_secret = env::var_os("JWT_SECRET").unwrap();
        let oauth_secret = dotenv::var_os("OAUTH_SECRET").unwrap();
    }
}

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

    #[test]
    fn test_config() {
        let test_var = std::env::var("TEST_VARIABLE").unwrap();
        let debug_mode = env::var("DEBUG_MODE").unwrap_or_default();

        // This should not be detected (not a direct env::var call)
        let other_var = format!("{}_{}", "PREFIX", std::env::var("SUFFIX_VAR").unwrap());
    }
}

fn main() {
    // Various spacing and formatting
    let var1 = std::env::var("VAR_WITH_SPACES").unwrap();
    let var2=env::var("VAR_NO_SPACES").unwrap();
    let var3 = dotenv::var(  "VAR_EXTRA_SPACES"  ).unwrap();

    // Multi-line (should still work)
    let var4 = std::env::var(
        "MULTILINE_VAR"
    ).unwrap();

    // With comments
    let var5 = env::var("COMMENTED_VAR").unwrap(); // This has a comment

    // In conditionals
    if let Ok(optional_var) = std::env::var("OPTIONAL_VAR") {
        println!("Optional var: {}", optional_var);
    }

    // In match expressions
    match env::var("MATCH_VAR") {
        Ok(val) => println!("Got: {}", val),
        Err(_) => println!("Not found"),
    }
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", complex_content).unwrap();

    let scanner = EnvScanner::new().unwrap();
    let variables = scanner.scan_directory(temp_dir.path()).unwrap();

    // Expected variables (should be detected)
    let expected = vec![
        "DATABASE_URL",
        "REDIS_URL",
        "API_KEY",
        "SECRET_KEY",
        "JWT_SECRET",
        "OAUTH_SECRET",
        "TEST_VARIABLE",
        "DEBUG_MODE",
        "SUFFIX_VAR", // This should be detected as it's a direct call
        "VAR_WITH_SPACES",
        "VAR_NO_SPACES",
        "VAR_EXTRA_SPACES",
        "MULTILINE_VAR",
        "COMMENTED_VAR",
        "OPTIONAL_VAR",
        "MATCH_VAR",
    ];

    assert_eq!(variables.len(), expected.len());

    for var in expected {
        assert!(
            variables.contains(var),
            "Variable '{}' should be detected",
            var
        );
    }
}

#[test]
fn test_detection_edge_cases() {
    let temp_dir = TempDir::new().unwrap();

    let edge_cases_content = r#"
use std::env;

fn main() {
    // These should be detected
    let var1 = std::env::var("SIMPLE_VAR").unwrap();
    let var2 = env::var("ANOTHER_VAR").ok();
    let var3 = dotenv::var("DOTENV_VAR").unwrap_or_default();

    // These should NOT be detected (not literal strings)
    let dynamic_key = "DYNAMIC_KEY";
    let var4 = std::env::var(dynamic_key).unwrap(); // Variable key
    let var5 = std::env::var(format!("PREFIX_{}", "SUFFIX")).unwrap(); // Computed key

    // These should NOT be detected (not env::var calls)
    let not_env = some_other_function("NOT_ENV_VAR");
    let comment_var = "This contains std::env::var(\"FAKE_VAR\") in a string";

    // Edge case: Special characters in variable names
    let special_var = env::var("VAR_WITH-DASH").unwrap();
    let underscore_var = env::var("VAR_WITH_UNDERSCORE").unwrap();
    let number_var = env::var("VAR123").unwrap();
}

fn some_other_function(key: &str) -> String {
    format!("Not env: {}", key)
}
"#;

    create_test_file(temp_dir.path(), "src/edge_cases.rs", edge_cases_content).unwrap();

    let scanner = EnvScanner::new().unwrap();
    let variables = scanner.scan_directory(temp_dir.path()).unwrap();

    // Should detect these
    assert!(variables.contains("SIMPLE_VAR"));
    assert!(variables.contains("ANOTHER_VAR"));
    assert!(variables.contains("DOTENV_VAR"));
    assert!(variables.contains("VAR_WITH-DASH"));
    assert!(variables.contains("VAR_WITH_UNDERSCORE"));
    assert!(variables.contains("VAR123"));

    // Should NOT detect these
    assert!(!variables.contains("DYNAMIC_KEY"));
    assert!(!variables.contains("NOT_ENV_VAR"));
    assert!(!variables.contains("FAKE_VAR"));

    // Should be exactly 6 variables
    assert_eq!(variables.len(), 6);
}

#[test]
fn test_merge_logic_comprehensive() {
    let temp_dir = TempDir::new().unwrap();

    // Create existing .env file with some variables
    let existing_env = r#"# Existing environment file
# This is a comment

EXISTING_VAR=existing_value
SHARED_VAR=original_value
EMPTY_EXISTING=

# Another comment
DATABASE_URL=postgres://localhost/mydb
"#;

    fs::write(temp_dir.path().join(".env"), existing_env).unwrap();

    // Create Rust file with some overlapping and new variables
    let rust_content = r#"
fn main() {
    let shared = std::env::var("SHARED_VAR").unwrap();
    let new_var = std::env::var("NEW_VARIABLE").unwrap();
    let db_url = std::env::var("DATABASE_URL").unwrap();
    let api_key = std::env::var("API_KEY").unwrap();
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    // Test with merge enabled (default)
    let config = Config {
        output: Some(".env".to_string()),
        merge_existing: Some(true),
        ignore: None,
    };

    generate_env_file_with_config(temp_dir.path(), config).unwrap();

    let result_vars = read_env_file(&temp_dir.path().join(".env"));

    // Should preserve existing values
    assert_eq!(
        result_vars.get("EXISTING_VAR"),
        Some(&"existing_value".to_string())
    );
    assert_eq!(
        result_vars.get("SHARED_VAR"),
        Some(&"original_value".to_string())
    );
    assert_eq!(
        result_vars.get("DATABASE_URL"),
        Some(&"postgres://localhost/mydb".to_string())
    );
    assert_eq!(result_vars.get("EMPTY_EXISTING"), Some(&"".to_string()));

    // Should add new variables with empty values
    assert_eq!(result_vars.get("NEW_VARIABLE"), Some(&"".to_string()));
    assert_eq!(result_vars.get("API_KEY"), Some(&"".to_string()));

    // Should have all variables
    assert_eq!(result_vars.len(), 6);
}

#[test]
fn test_no_merge_behavior() {
    let temp_dir = TempDir::new().unwrap();

    // Create existing .env file
    let existing_env = "EXISTING_VAR=existing_value\nSHARED_VAR=original_value\n";
    fs::write(temp_dir.path().join(".env"), existing_env).unwrap();

    // Create Rust file
    let rust_content = r#"
fn main() {
    let shared = std::env::var("SHARED_VAR").unwrap();
    let new_var = std::env::var("NEW_VARIABLE").unwrap();
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    // Test with merge disabled
    let config = Config {
        output: Some(".env".to_string()),
        merge_existing: Some(false),
        ignore: None,
    };

    generate_env_file_with_config(temp_dir.path(), config).unwrap();

    let result_vars = read_env_file(&temp_dir.path().join(".env"));

    // Should only have detected variables with empty values
    assert_eq!(result_vars.get("SHARED_VAR"), Some(&"".to_string()));
    assert_eq!(result_vars.get("NEW_VARIABLE"), Some(&"".to_string()));

    // Should NOT have the existing variable that wasn't detected
    assert!(!result_vars.contains_key("EXISTING_VAR"));

    assert_eq!(result_vars.len(), 2);
}

#[test]
fn test_ignore_list_behavior() {
    let temp_dir = TempDir::new().unwrap();

    let rust_content = r#"
fn main() {
    let db_url = std::env::var("DATABASE_URL").unwrap();
    let api_key = std::env::var("API_KEY").unwrap();
    let debug = std::env::var("DEBUG_MODE").unwrap();
    let secret = std::env::var("SECRET_KEY").unwrap();
    let port = std::env::var("PORT").unwrap();
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    // Test with ignore list
    let config = Config {
        output: Some(".env".to_string()),
        merge_existing: Some(false),
        ignore: Some(vec!["DEBUG_MODE".to_string(), "SECRET_KEY".to_string()]),
    };

    generate_env_file_with_config(temp_dir.path(), config).unwrap();

    let result_vars = read_env_file(&temp_dir.path().join(".env"));

    // Should have variables that are not ignored
    assert!(result_vars.contains_key("DATABASE_URL"));
    assert!(result_vars.contains_key("API_KEY"));
    assert!(result_vars.contains_key("PORT"));

    // Should NOT have ignored variables
    assert!(!result_vars.contains_key("DEBUG_MODE"));
    assert!(!result_vars.contains_key("SECRET_KEY"));

    assert_eq!(result_vars.len(), 3);
}

#[test]
fn test_custom_output_file() {
    let temp_dir = TempDir::new().unwrap();

    let rust_content = r#"
fn main() {
    let var = std::env::var("TEST_VAR").unwrap();
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    // Test with custom output file
    let config = Config {
        output: Some(".env.example".to_string()),
        merge_existing: Some(false),
        ignore: None,
    };

    generate_env_file_with_config(temp_dir.path(), config).unwrap();

    // Should create .env.example, not .env
    assert!(temp_dir.path().join(".env.example").exists());
    assert!(!temp_dir.path().join(".env").exists());

    let result_vars = read_env_file(&temp_dir.path().join(".env.example"));
    assert!(result_vars.contains_key("TEST_VAR"));
}

#[test]
fn test_nested_directory_scanning() {
    let temp_dir = TempDir::new().unwrap();

    // Create files in various nested directories
    create_test_file(
        temp_dir.path(),
        "src/main.rs",
        r#"fn main() { let var1 = std::env::var("VAR_1").unwrap(); }"#,
    )
    .unwrap();

    create_test_file(
        temp_dir.path(),
        "src/lib.rs",
        r#"pub fn lib_fn() { let var2 = std::env::var("VAR_2").unwrap(); }"#,
    )
    .unwrap();

    create_test_file(
        temp_dir.path(),
        "src/modules/auth.rs",
        r#"pub fn auth() { let var3 = std::env::var("VAR_3").unwrap(); }"#,
    )
    .unwrap();

    create_test_file(
        temp_dir.path(),
        "tests/integration.rs",
        r#"#[test] fn test() { let var4 = std::env::var("VAR_4").unwrap(); }"#,
    )
    .unwrap();

    create_test_file(
        temp_dir.path(),
        "examples/example.rs",
        r#"fn main() { let var5 = std::env::var("VAR_5").unwrap(); }"#,
    )
    .unwrap();

    // Create file in target directory (should be ignored)
    create_test_file(
        temp_dir.path(),
        "target/debug/main.rs",
        r#"fn main() { let ignored = std::env::var("IGNORED_VAR").unwrap(); }"#,
    )
    .unwrap();

    generate_env_file(temp_dir.path()).unwrap();

    let result_vars = read_env_file(&temp_dir.path().join(".env"));

    // Should find variables from all non-target directories
    assert!(result_vars.contains_key("VAR_1"));
    assert!(result_vars.contains_key("VAR_2"));
    assert!(result_vars.contains_key("VAR_3"));
    assert!(result_vars.contains_key("VAR_4"));
    assert!(result_vars.contains_key("VAR_5"));

    // Should ignore target directory
    assert!(!result_vars.contains_key("IGNORED_VAR"));

    assert_eq!(result_vars.len(), 5);
}

#[test]
fn test_library_api_functions() {
    let temp_dir = TempDir::new().unwrap();

    let rust_content = r#"
fn main() {
    let var1 = std::env::var("LIB_VAR_1").unwrap();
    let var2 = std::env::var("LIB_VAR_2").unwrap();
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    // Test basic generate_env_file function
    generate_env_file(temp_dir.path()).unwrap();
    assert!(temp_dir.path().join(".env").exists());

    // Test generate_env_file_to with custom output path
    let custom_output = temp_dir.path().join("custom.env");
    auto_env_generator::generate_env_file_to(temp_dir.path(), &custom_output).unwrap();
    assert!(custom_output.exists());

    // Test scan_for_env_vars function
    let variables = auto_env_generator::scan_for_env_vars(temp_dir.path()).unwrap();
    assert_eq!(variables.len(), 2);
    assert!(variables.contains("LIB_VAR_1"));
    assert!(variables.contains("LIB_VAR_2"));
}

#[test]
fn test_config_loading() {
    let temp_dir = TempDir::new().unwrap();

    // Create a TOML config file with correct format:
    let correct_config_content = r#"
output = ".env.development"
merge_existing = false
ignore = ["DEBUG", "TEST_MODE", "DEVELOPMENT"]
"#;

    fs::write(temp_dir.path().join("autoenv.toml"), correct_config_content).unwrap();

    let config = EnvScanner::load_config(temp_dir.path().join("autoenv.toml")).unwrap();

    assert_eq!(config.output, Some(".env.development".to_string()));
    assert_eq!(config.merge_existing, Some(false));
    assert_eq!(
        config.ignore,
        Some(vec![
            "DEBUG".to_string(),
            "TEST_MODE".to_string(),
            "DEVELOPMENT".to_string()
        ])
    );
}

#[test]
fn test_empty_project() {
    let temp_dir = TempDir::new().unwrap();

    // Create empty src directory
    fs::create_dir_all(temp_dir.path().join("src")).unwrap();
    fs::write(
        temp_dir.path().join("src/main.rs"),
        "fn main() { println!(\"Hello, world!\"); }",
    )
    .unwrap();

    generate_env_file(temp_dir.path()).unwrap();

    let result_vars = read_env_file(&temp_dir.path().join(".env"));
    assert_eq!(result_vars.len(), 0);

    // File should still be created with header
    let content = fs::read_to_string(temp_dir.path().join(".env")).unwrap();
    assert!(content.contains("Auto-generated environment variables"));
}

#[test]
fn test_file_with_no_env_vars() {
    let temp_dir = TempDir::new().unwrap();

    let rust_content = r#"
use std::collections::HashMap;

fn main() {
    let mut map = HashMap::new();
    map.insert("key", "value");

    println!("Hello, world!");

    let x = 42;
    let y = x * 2;

    // This contains the word "var" but is not an env var call
    let my_var = "not an env var";

    // This contains env::var in a comment but should not be detected
    // std::env::var("COMMENT_VAR") - this is just a comment
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_something() {
        assert_eq!(2 + 2, 4);
    }
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    let scanner = EnvScanner::new().unwrap();
    let variables = scanner.scan_directory(temp_dir.path()).unwrap();

    // Should not detect any variables since none are actual env::var calls
    assert_eq!(variables.len(), 0);
}

#[test]
fn test_malformed_env_calls() {
    let temp_dir = TempDir::new().unwrap();

    let rust_content = r#"
fn main() {
    // Valid calls
    let var1 = std::env::var("VALID_VAR").unwrap();

    // Invalid/malformed calls that should not be detected
    let var2 = std::env::var(; // Syntax error, but we should handle gracefully
    let var3 = std::env::var("UNCLOSED_QUOTE;
    let var4 = std::env::var('SINGLE_QUOTES').unwrap(); // Wrong quote type
    let var5 = std::env::var().unwrap(); // No argument

    // These should be detected despite formatting
    let var6 = std::env::var("VALID_VAR_2").unwrap();
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    let scanner = EnvScanner::new().unwrap();
    let variables = scanner.scan_directory(temp_dir.path()).unwrap();

    // Should only detect the valid calls
    assert_eq!(variables.len(), 2);
    assert!(variables.contains("VALID_VAR"));
    assert!(variables.contains("VALID_VAR_2"));
}

#[test]
fn test_cli_integration() {
    let temp_dir = TempDir::new().unwrap();

    let rust_content = r#"
fn main() {
    let var = std::env::var("CLI_TEST_VAR").unwrap();
}
"#;

    create_test_file(temp_dir.path(), "src/main.rs", rust_content).unwrap();

    // Build the CLI binary first
    let output = Command::new("cargo")
        .args(&["build", "--bin", "autoenv"])
        .current_dir(".")
        .output()
        .expect("Failed to build CLI");

    if !output.status.success() {
        panic!(
            "Failed to build CLI: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    // Test the CLI generate command
    let output = Command::new("./target/debug/autoenv")
        .args(&["generate", temp_dir.path().to_str().unwrap()])
        .output()
        .expect("Failed to execute CLI");

    if !output.status.success() {
        panic!(
            "CLI command failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    // Check that .env file was created
    assert!(temp_dir.path().join(".env").exists());

    let result_vars = read_env_file(&temp_dir.path().join(".env"));
    assert!(result_vars.contains_key("CLI_TEST_VAR"));
}

#[test]
fn test_large_file_performance() {
    let temp_dir = TempDir::new().unwrap();

    // Create a large file with many environment variables
    let mut large_content = String::new();
    large_content.push_str("fn main() {\n");

    for i in 0..1000 {
        large_content.push_str(&format!(
            "    let var_{} = std::env::var(\"LARGE_VAR_{}\").unwrap();\n",
            i, i
        ));
    }

    large_content.push_str("}\n");

    create_test_file(temp_dir.path(), "src/main.rs", &large_content).unwrap();

    let start = std::time::Instant::now();

    let scanner = EnvScanner::new().unwrap();
    let variables = scanner.scan_directory(temp_dir.path()).unwrap();

    let duration = start.elapsed();

    // Should complete quickly (under 1 second for 1000 variables)
    assert!(duration.as_secs() < 1);
    assert_eq!(variables.len(), 1000);

    // Verify some variables were detected correctly
    assert!(variables.contains("LARGE_VAR_0"));
    assert!(variables.contains("LARGE_VAR_500"));
    assert!(variables.contains("LARGE_VAR_999"));
}