diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..84e6343 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,19 @@ +name: Rust Test with Coverage + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + + - name: Run Tests + run: cargo test diff --git a/.gitignore b/.gitignore index 92fe915..f38d2c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +/.idea /target /_* /nurfile.local +tarpaulin-report.html \ No newline at end of file diff --git a/src/path.rs b/src/path.rs index 4f96b32..0fc2347 100644 --- a/src/path.rs +++ b/src/path.rs @@ -21,3 +21,62 @@ pub fn find_project_path( } } } + +#[cfg(test)] +mod tests { + use super::*; + use std::env; + use std::fs::{create_dir, File}; + + #[test] + fn test_find_project_path() { + // Create a temporary directory and a "nurfile" inside it + let temp_dir = env::temp_dir(); + let nurfile_path = temp_dir.join("nurfile"); + File::create(&nurfile_path).unwrap(); + + // Test the function with the temporary directory as the current working directory + let expected_path = temp_dir.to_path_buf(); + let actual_path = find_project_path(&temp_dir).unwrap(); + assert_eq!(expected_path, actual_path); + + // Clean up + std::fs::remove_file(nurfile_path).unwrap(); + } + + #[test] + fn test_find_project_path_subdirectory() { + // Create a temporary directory and a subdirectory inside it + let temp_dir = env::temp_dir(); + let sub_dir = temp_dir.join("sub"); + create_dir(&sub_dir).unwrap(); + + // Create a "nurfile" inside the temporary directory + let nurfile_path = temp_dir.join("nurfile"); + File::create(&nurfile_path).unwrap(); + + // Test the function with the subdirectory as the current working directory + let expected_path = temp_dir.to_path_buf(); + let actual_path = find_project_path(&sub_dir).unwrap(); + assert_eq!(expected_path, actual_path); + + // Clean up + std::fs::remove_file(nurfile_path).unwrap(); + std::fs::remove_dir(sub_dir).unwrap(); + } + + #[test] + fn test_find_project_path_error() { + // Create a temporary directory without a "nurfile" + let temp_dir = env::temp_dir(); + + // Test the function with the temporary directory as the current working directory + match find_project_path(&temp_dir) { + Ok(_) => panic!("Expected an error, but got Ok"), + Err(e) => match e { + NurError::NurTaskfileNotFound() => (), // Test passes + _ => panic!("Expected NurTaskfileNotFound, but got a different error"), + }, + } + } +} \ No newline at end of file