envset 0.2.0

A command-line tool for setting environment variables in a .env file
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
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{Cursor, Write};
use tempfile::tempdir;

use crate::{Cli, Commands};
use envset::{
    parse_stdin_with_reader, print_env_keys_to_writer, print_env_vars, read_env_vars,
    update_env_file,
};

#[test]
fn test_write_vars_with_quotes() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");

    let mut env_vars = HashMap::new();
    env_vars.insert("KEY1".to_string(), r#"value with "quotes""#.to_string());
    env_vars.insert("KEY2".to_string(), r#"value with 'quotes'"#.to_string());
    env_vars.insert(
        "KEY3".to_string(),
        r#"value with both 'single' and "double" quotes"#.to_string(),
    );

    update_env_file(file_path.to_str().unwrap(), &env_vars).unwrap();

    // Read the file contents
    let contents = fs::read_to_string(&file_path).unwrap();

    // Print out the file contents for debugging
    println!("File contents:\n{}", contents);

    // Read the file using read_env_file and check the result
    let result = read_env_vars(file_path.to_str().unwrap()).unwrap();

    // Print out the environment variables for debugging
    println!("Environment variables:");
    for (key, value) in &result {
        println!("{}: {}", key, value);
    }

    // Print out the environment variables for debugging
    println!("Environment variables:");
    for (key, value) in &result {
        println!("{}: {}", key, value);
    }

    assert_eq!(
        result.get("KEY1"),
        Some(&r#"value with "quotes""#.to_string())
    );
    assert_eq!(
        result.get("KEY2"),
        Some(&r#"value with 'quotes'"#.to_string())
    );
    assert_eq!(
        result.get("KEY3"),
        Some(&r#"value with both 'single' and "double" quotes"#.to_string())
    );

    // Check the file contents directly
    let file_contents = fs::read_to_string(&file_path).unwrap();
    println!("File contents after writing:\n{}", file_contents);
    assert!(file_contents.contains(r#"KEY1="value with \"quotes\"""#));
    assert!(file_contents.contains(r#"KEY2="value with 'quotes'""#));
    assert!(file_contents.contains(r#"KEY3="value with both 'single' and \"double\" quotes""#));
}

#[test]
fn test_read_env_file() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "KEY1=value1\nKEY2=value2").unwrap();

    let result = read_env_vars(file_path.to_str().unwrap()).unwrap();
    assert_eq!(result.get("KEY1"), Some(&"value1".to_string()));
    assert_eq!(result.get("KEY2"), Some(&"value2".to_string()));
}

#[test]
fn test_write_env_file() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut env_vars = HashMap::new();
    env_vars.insert("KEY1".to_string(), "value1".to_string());
    env_vars.insert("KEY2".to_string(), "value2".to_string());

    update_env_file(file_path.to_str().unwrap(), &env_vars).unwrap();

    let result = read_env_vars(file_path.to_str().unwrap()).unwrap();
    assert_eq!(result.get("KEY1"), Some(&"value1".to_string()));
    assert_eq!(result.get("KEY2"), Some(&"value2".to_string()));
}

#[test]
fn test_preserve_comments() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(
        file,
        "# example comment\nFOO='bar'\n# another comment\nBAZ=qux"
    )
    .unwrap();

    let result = read_env_vars(file_path.to_str().unwrap()).unwrap();
    assert_eq!(result.get("FOO"), Some(&"bar".to_string()));
    assert_eq!(result.get("BAZ"), Some(&"qux".to_string()));

    // Re-read the file to check if comments are preserved
    let contents = fs::read_to_string(file_path).unwrap();
    assert!(contents.contains("# example comment"));
    assert!(contents.contains("# another comment"));
    assert!(contents.contains("FOO='bar'"));
    assert!(contents.contains("BAZ=qux"));
}

#[test]
fn test_parse_stdin_with_pipe() {
    let input = "KEY1=value1\nKEY2=value2\n";
    let mut cursor = Cursor::new(input);
    let result = parse_stdin_with_reader(&mut cursor);
    assert_eq!(result.get("KEY1"), Some(&"value1".to_string()));
    assert_eq!(result.get("KEY2"), Some(&"value2".to_string()));
    assert_eq!(result.len(), 2);
}

#[test]
fn test_parse_stdin_and_write_to_file() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");

    let input = "KEY1=value1\nKEY2=value2\n";
    let mut cursor = Cursor::new(input);
    let result = parse_stdin_with_reader(&mut cursor);

    // Write the result to the temporary file
    update_env_file(file_path.to_str().unwrap(), &result).unwrap();

    // Read the file contents
    let contents = fs::read_to_string(&file_path).unwrap();

    // Check if the file contains the expected content
    assert!(contents.contains("KEY1=value1"));
    assert!(contents.contains("KEY2=value2"));

    // Read the file using read_env_file and check the result
    let env_vars = read_env_vars(file_path.to_str().unwrap()).unwrap();
    assert_eq!(env_vars.get("KEY1"), Some(&"value1".to_string()));
    assert_eq!(env_vars.get("KEY2"), Some(&"value2".to_string()));
    assert_eq!(env_vars.len(), 2);
}

#[test]
fn test_multiple_var_sets() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");

    // First set ABCD=123
    let mut env_vars = HashMap::new();
    env_vars.insert("ABCD".to_string(), "123".to_string());
    update_env_file(file_path.to_str().unwrap(), &env_vars).unwrap();

    // Then set AB=12
    env_vars.insert("AB".to_string(), "12".to_string());
    let _ = read_env_vars(file_path.to_str().unwrap()).unwrap();
    update_env_file(file_path.to_str().unwrap(), &env_vars).unwrap();

    // Read the final state of the file
    let result = read_env_vars(file_path.to_str().unwrap()).unwrap();

    // Assert that both variables are set
    assert_eq!(result.get("ABCD"), Some(&"123".to_string()));
    assert_eq!(result.get("AB"), Some(&"12".to_string()));
    assert_eq!(result.len(), 2);

    // Check the final content of the file
    let final_content = fs::read_to_string(&file_path).unwrap();
    assert_eq!(final_content, "ABCD=123\nAB=12\n");
}

#[test]
fn test_last_occurence_of_duplicate_keys_updated() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");

    // Create an initial .env file with duplicate keys
    let initial_content = "A=a\nFOO=1\nB=b\nFOO=2\n";
    fs::write(&file_path, initial_content).unwrap();

    // Set FOO=3
    let mut env_vars = HashMap::new();
    env_vars.insert("FOO".to_string(), "3".to_string());
    update_env_file(file_path.to_str().unwrap(), &env_vars).unwrap();

    // Read the final state of the file
    let result = read_env_vars(file_path.to_str().unwrap()).unwrap();

    // Assert that FOO is set to 3
    assert_eq!(result.get("FOO"), Some(&"3".to_string()));
    assert_eq!(result.len(), 3); // A, B, and FOO

    // Check the final content of the file
    let final_content = fs::read_to_string(&file_path).unwrap();
    assert_eq!(
        final_content, "A=a\nFOO=1\nB=b\nFOO=3\n",
        "The last occurrence of FOO should be updated to 3"
    );

    let foo_count = final_content.matches("FOO=").count();
    assert_eq!(foo_count, 2, "There should be two occurrences of FOO");
    assert!(
        final_content.contains("FOO=1"),
        "The first occurrence of FOO should remain unchanged"
    );
    assert!(
        final_content.contains("FOO=3"),
        "The last occurrence of FOO should be updated to 3"
    );
}

#[test]
fn test_delete_env_vars() {
    // TODO test
}

#[test]
fn test_preserve_comments_when_setting_new_var() {
    // TODO
    // let dir = tempdir().unwrap();
    // let file_path = dir.path().join(".env");
    // let initial_content = "# This is a comment\nEXISTING=value\n\n# Another comment\n";
    // fs::write(&file_path, initial_content).unwrap();

    // let mut new_vars = HashMap::new();
    // new_vars.insert("NEW_VAR".to_string(), "new_value".to_string());
    // new_vars.insert("EXISTING".to_string(), "value".to_string());
    // write_env_file(file_path.to_str().unwrap(), &new_vars).unwrap();

    // let final_content = fs::read_to_string(&file_path).unwrap();
    // println!("Final content:\n{}", final_content);
    // assert!(
    //     final_content.contains("# This is a comment\n"),
    //     "First comment should be preserved"
    // );
    // assert!(
    //     final_content.contains("EXISTING=value\n"),
    //     "Existing variable should be preserved"
    // );
    // assert!(
    //     final_content.contains("\n# Another comment\n"),
    //     "Second comment should be preserved"
    // );
    // assert!(
    //     final_content.contains("\nNEW_VAR=new_value\n"),
    //     "New variable should be added on a new line"
    // );

    // let env_vars = read_env_vars(file_path.to_str().unwrap()).unwrap();
    // assert_eq!(env_vars.get("EXISTING"), Some(&"value".to_string()));
    // assert_eq!(env_vars.get("NEW_VAR"), Some(&"new_value".to_string()));
}

#[test]
fn test_get_single_env_var() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "FOO=bar\nBAZ=qux").unwrap();

    let env_vars = read_env_vars(file_path.to_str().unwrap()).unwrap();
    assert_eq!(env_vars.get("FOO"), Some(&"bar".to_string()));
    assert_eq!(env_vars.get("BAZ"), Some(&"qux".to_string()));
}

#[test]
fn test_print_all_env_vars() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "FOO=bar\nBAZ=qux\nABC=123").unwrap();

    let mut output = Vec::new();
    print_env_vars(file_path.to_str().unwrap(), &mut output, false);

    let output_str = String::from_utf8(output).unwrap();

    let plain_bytes = strip_ansi_escapes::strip(&output_str);
    let stripped_output = String::from_utf8_lossy(&plain_bytes);

    assert_eq!(
        stripped_output.trim(),
        "FOO=bar\nBAZ=qux\nABC=123",
        "Output should match the input file content"
    );
}

#[test]
fn test_no_print_when_args_provided() {
    use clap::Parser;
    use std::io::Cursor;

    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "FOO=bar\nBAZ=qux").unwrap();

    // Simulate command-line arguments with vars
    let args = vec![
        "envset",
        "--file",
        file_path.to_str().unwrap(),
        "NEW_VAR=value",
    ];
    let cli = Cli::parse_from(args);

    // Capture stdout
    let mut output = Vec::new();
    {
        let mut cursor = Cursor::new(&mut output);

        // Run the main logic
        if cli.command.is_none() && !cli.vars.is_empty() {
            // This is where we would normally set the environment variables
            // For this test, we're just ensuring it doesn't print
        } else {
            print_env_vars(file_path.to_str().unwrap(), &mut cursor, false);
        }
    }

    let output_str = String::from_utf8(output).unwrap();
    assert!(
        output_str.is_empty(),
        "Output should be empty when args are provided"
    );
}

#[test]
fn test_print_all_keys() {
    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "FOO=bar\nBAZ=qux").unwrap();

    let mut output = Vec::new();
    print_env_keys_to_writer(file_path.to_str().unwrap(), &mut output);

    let output_str = String::from_utf8(output).unwrap();
    assert!(output_str.contains("FOO"), "Output does not contain FOO");
    assert!(output_str.contains("BAZ"), "Output does not contain BAZ");
    assert!(!output_str.contains("="), "Output should not contain '='");
}

#[test]
fn test_print_when_no_args() {
    use clap::Parser;
    use std::io::Cursor;

    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "FOO=bar\nBAZ=qux").unwrap();

    // Simulate command-line arguments
    let args = vec!["envset", "--file", file_path.to_str().unwrap()];
    let cli = Cli::parse_from(args);

    // Capture stdout
    let mut output = Vec::new();
    {
        let mut cursor = Cursor::new(&mut output);

        // Run the main logic
        match &cli.command {
            Some(Commands::Print {
                parse_tree: false,
                json: false,
            })
            | None => {
                print_env_vars(file_path.to_str().unwrap(), &mut cursor, false);
            }
            Some(Commands::Print {
                parse_tree: true,
                json: _,
            }) => {
                // For this test, we don't need to implement parse tree printing
            }
            _ => panic!("Unexpected command"),
        }
    }

    let output_str = String::from_utf8(output).unwrap();
    assert!(
        output_str.contains("FOO") && output_str.contains("bar"),
        "Output does not contain FOO and bar"
    );
    assert!(
        output_str.contains("BAZ") && output_str.contains("qux"),
        "Output does not contain BAZ and qux"
    );
}

#[test]
fn test_no_print_when_vars_set_via_stdin() {
    use clap::Parser;
    use std::io::Cursor;

    let dir = tempdir().unwrap();
    let file_path = dir.path().join(".env");
    let mut file = File::create(&file_path).unwrap();
    writeln!(file, "EXISTING=value").unwrap();

    // Simulate command-line arguments
    let args = vec!["envset", "--file", file_path.to_str().unwrap()];
    let cli = Cli::parse_from(args);

    // Simulate stdin input
    let stdin_input = "NEW_VAR=new_value\n";
    let mut stdin = Cursor::new(stdin_input);

    // Capture stdout
    let mut output = Vec::new();
    {
        let mut stdout = Cursor::new(&mut output);

        // Run the main logic
        let new_vars = parse_stdin_with_reader(&mut stdin);
        if !new_vars.is_empty() {
            let mut env_vars = read_env_vars(file_path.to_str().unwrap()).unwrap();
            env_vars.extend(new_vars);
            update_env_file(file_path.to_str().unwrap(), &env_vars).unwrap();
        } else if cli.command.is_none() {
            print_env_vars(file_path.to_str().unwrap(), &mut stdout, false);
        }
    }

    // TODO test diff
}