Skip to content

Commit ffff3ac

Browse files
committed
test: write unit tests for load command
1 parent 803c259 commit ffff3ac

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ assert_cmd = "2.0.16"
3535
predicates = "3.1.3"
3636
# Asserting file system
3737
assert_fs = "1.1.2"
38+
# Creating temporary directories
39+
tempfile = "3.8.1"

src/commands.rs

+103
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ pub fn delete(args: &DeleteArgs) -> Result<(), ErrorKind> {
103103
mod tests {
104104
use super::*;
105105
use std::io::Cursor;
106+
use std::io::Write;
107+
use tempfile::NamedTempFile;
106108

107109
// Helper function to capture stdout
108110
// TODO: use more efficient method
@@ -482,4 +484,105 @@ mod tests {
482484
_ => panic!("Unexpected error type"),
483485
}
484486
}
487+
488+
#[test]
489+
fn test_load_valid_env_file() {
490+
let mut temp_file = NamedTempFile::new().unwrap();
491+
writeln!(temp_file, "TEST_VAR=test_value\nOTHER_VAR=other_value").unwrap();
492+
493+
let args = LoadArgs {
494+
file: temp_file.path().to_string_lossy().to_string(),
495+
global: false,
496+
process: None,
497+
};
498+
499+
let result = load(&args);
500+
assert!(result.is_ok());
501+
assert_eq!(env::var("TEST_VAR").unwrap(), "test_value");
502+
assert_eq!(env::var("OTHER_VAR").unwrap(), "other_value");
503+
504+
env::remove_var("TEST_VAR");
505+
env::remove_var("OTHER_VAR");
506+
}
507+
508+
#[test]
509+
fn test_load_nonexistent_file() {
510+
let args = LoadArgs {
511+
file: "nonexistent.env".to_string(),
512+
global: false,
513+
process: None,
514+
};
515+
516+
let result = load(&args);
517+
assert!(result.is_err());
518+
assert!(matches!(result.unwrap_err(), ErrorKind::FileError(_)));
519+
}
520+
521+
#[test]
522+
fn test_load_invalid_env_file() {
523+
let mut temp_file = NamedTempFile::new().unwrap();
524+
// Using invalid .env format that dotenv_parser will reject
525+
writeln!(temp_file, "TEST_VAR test_value").unwrap();
526+
527+
let args = LoadArgs {
528+
file: temp_file.path().to_string_lossy().to_string(),
529+
global: false,
530+
process: None,
531+
};
532+
533+
let result = load(&args);
534+
assert!(result.is_err());
535+
assert!(matches!(result.unwrap_err(), ErrorKind::ParsingError(_)));
536+
}
537+
538+
#[test]
539+
fn test_load_with_process() {
540+
let mut temp_file = NamedTempFile::new().unwrap();
541+
writeln!(temp_file, "TEST_PROCESS_VAR=process_value").unwrap();
542+
543+
#[cfg(windows)]
544+
let cmd = "cmd /C echo test"; // Simple echo command for Windows
545+
#[cfg(not(windows))]
546+
let cmd = "echo test"; // Simple echo command for Unix
547+
548+
let args = LoadArgs {
549+
file: temp_file.path().to_string_lossy().to_string(),
550+
global: false,
551+
process: Some(cmd.to_string()),
552+
};
553+
554+
// First verify the variable is set correctly
555+
let result = load(&args);
556+
assert!(result.is_ok(), "Load operation failed: {:?}", result);
557+
}
558+
559+
#[test]
560+
fn test_load_empty_file() {
561+
let temp_file = NamedTempFile::new().unwrap();
562+
563+
let args = LoadArgs {
564+
file: temp_file.path().to_string_lossy().to_string(),
565+
global: false,
566+
process: None,
567+
};
568+
569+
let result = load(&args);
570+
assert!(result.is_ok());
571+
}
572+
573+
#[test]
574+
fn test_load_with_invalid_variable_name() {
575+
let mut temp_file = NamedTempFile::new().unwrap();
576+
writeln!(temp_file, "TEST_VAR=test_value\nINVALID NAME=value").unwrap();
577+
578+
let args = LoadArgs {
579+
file: temp_file.path().to_string_lossy().to_string(),
580+
global: false,
581+
process: None,
582+
};
583+
584+
let result = load(&args);
585+
assert!(result.is_err());
586+
assert!(matches!(result.unwrap_err(), ErrorKind::ParsingError(_)));
587+
}
485588
}

src/models.rs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub struct DeleteArgs {
112112
pub process: Option<String>,
113113
}
114114

115+
#[derive(Debug)]
115116
pub enum ErrorKind {
116117
StartingProcessError,
117118
ProcessFailed,

0 commit comments

Comments
 (0)