Skip to content

Commit

Permalink
Merge pull request #8 from bambamboole/mc-provide-first-test
Browse files Browse the repository at this point in the history
Provide first tests (thx to @bambamboole)
  • Loading branch information
ddanier authored Mar 10, 2024
2 parents 4fd3133 + a338a77 commit 09861fb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.idea
/target
/_*
/nurfile.local
tarpaulin-report.html
59 changes: 59 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}
}
}

0 comments on commit 09861fb

Please sign in to comment.