Skip to content

Commit

Permalink
[ISSUE #255]Add test case for util
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm committed Nov 4, 2023
1 parent 57a4b0d commit 7e46d79
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions volo-build/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,57 @@ pub fn git_repo_init(path: &Path) -> anyhow::Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use std::fs;

use tempfile::{tempdir, NamedTempFile};

use super::*;

#[test]
fn test_ensure_path() {
// Test case 1: directory already exists
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("existing_dir");
fs::create_dir_all(&path).unwrap();
assert!(ensure_path(&path).is_ok());

// Test case 2: directory does not exist
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("new_dir");
assert!(ensure_path(&path).is_ok());
assert!(fs::metadata(&path).unwrap().is_dir());
}

#[test]
fn test_ensure_file() {
// Test case 1: File does not exist
let result = tempdir().unwrap();
let binding = result.path().join("non_existing_file.txt");
let filename1 = binding.as_path();
match ensure_file(filename1) {
Ok(file) => {
assert!(file.metadata().is_ok());
}
Err(err) => {
eprintln!("Error: {}", err);
assert!(false, "Failed to create new file");
}
}

// Test case 2: File already exists
let file1 = NamedTempFile::new().unwrap();
let filename2 = file1.path();
match ensure_file(filename2) {
Ok(file) => {
assert!(file.metadata().is_ok());
}
Err(err) => {
eprintln!("Error: {}", err);
assert!(false, "Failed to append to existing file");
}
}
}
}

0 comments on commit 7e46d79

Please sign in to comment.