Skip to content

Commit

Permalink
test(libmake): ✅ add tests for csv.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 14, 2024
1 parent d0d2b39 commit 07ce546
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/test_csv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#[cfg(test)]
mod tests {
use libmake::generators::csv::generate_from_csv;
use std::fs::File;
use std::io::Write;

#[test]
fn test_generate_from_csv_valid() {
// Create a temporary CSV file
let csv_content = "\
author,build,categories,description,documentation,edition,email,homepage,keywords,license,name,output,readme,repository,rustversion,version,website\n\
John Doe,cargo build,web,Sample project,,2021,[email protected],https://example.com,example,Rust-1.0,SampleProject,/tmp/output,README.md,https://github.com/example,1.51.0,0.1.0,https://example.com\n";

let path = "/tmp/test_valid.csv";
let mut file = File::create(path).unwrap();
file.write_all(csv_content.as_bytes()).unwrap();

// Call the function and check for success
let result = generate_from_csv(path);
assert!(result.is_ok());

// Clean up
std::fs::remove_file(path).unwrap();
}

#[test]
fn test_generate_from_csv_invalid_path() {
let result = generate_from_csv("/invalid/path/to/csv");
assert!(result.is_err());
}

#[test]
fn test_generate_from_csv_malformed_csv() {
// Create a temporary malformed CSV file
let csv_content = "author,build,categories,description,documentation,edition,email,homepage,keywords,license,name,output,readme,repository,rustversion,version,website\n\
John Doe,cargo build,web,Sample project,,2021,[email protected],https://example.com,example,Rust-1.0,SampleProject,/tmp/output,README.md,https://github.com/example,1.51.0,0.1.0";

let path = "/tmp/test_malformed.csv";
let mut file = File::create(path).unwrap();
file.write_all(csv_content.as_bytes()).unwrap();

// Call the function and check for error
let result = generate_from_csv(path);
assert!(result.is_err());

// Clean up
std::fs::remove_file(path).unwrap();
}
}

0 comments on commit 07ce546

Please sign in to comment.