Skip to content

Commit

Permalink
test: 🚨 Fix tempdir usage in newly added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddanier committed Mar 10, 2024
1 parent 09861fb commit e717bda
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ thiserror = "1.0.57"
default = ["extra", "dataframe"]
extra = ["dep:nu-cmd-extra", "nu-cmd-lang/extra"]
dataframe = ["dep:nu-cmd-dataframe", "nu-cmd-dataframe/dataframe"]

[dev-dependencies]
tempfile = "3.10.1"
28 changes: 17 additions & 11 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub fn find_project_path(
}
}

println!("{:?}", taskfile_path);
println!("{:?}", path);

if let Some(parent) = path.parent() {
path = parent.to_path_buf();
} else {
Expand All @@ -25,19 +28,20 @@ pub fn find_project_path(
#[cfg(test)]
mod tests {
use super::*;
use std::env;
use tempfile::tempdir;
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");
let temp_dir = tempdir().unwrap();
let temp_dir_path = temp_dir.path().to_path_buf();
let nurfile_path = temp_dir.path().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();
let expected_path = temp_dir_path.clone();
let actual_path = find_project_path(&temp_dir_path).unwrap();
assert_eq!(expected_path, actual_path);

// Clean up
Expand All @@ -47,16 +51,17 @@ mod tests {
#[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");
let temp_dir = tempdir().unwrap();
let temp_dir_path = temp_dir.path().to_path_buf();
let sub_dir = temp_dir_path.join("sub");
create_dir(&sub_dir).unwrap();

// Create a "nurfile" inside the temporary directory
let nurfile_path = temp_dir.join("nurfile");
let nurfile_path = temp_dir_path.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 expected_path = temp_dir_path.clone();
let actual_path = find_project_path(&sub_dir).unwrap();
assert_eq!(expected_path, actual_path);

Expand All @@ -68,10 +73,11 @@ mod tests {
#[test]
fn test_find_project_path_error() {
// Create a temporary directory without a "nurfile"
let temp_dir = env::temp_dir();
let temp_dir = tempdir().unwrap();
let temp_dir_path = temp_dir.path().to_path_buf();

// Test the function with the temporary directory as the current working directory
match find_project_path(&temp_dir) {
match find_project_path(&temp_dir_path) {
Ok(_) => panic!("Expected an error, but got Ok"),
Err(e) => match e {
NurError::NurTaskfileNotFound() => (), // Test passes
Expand Down

0 comments on commit e717bda

Please sign in to comment.