Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #255]Add test case for util #256

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}
}
Loading