From e717bda50351b5ee3fb10be1c4c02941c0d3f5af Mon Sep 17 00:00:00 2001 From: David Danier Date: Sun, 10 Mar 2024 09:14:16 +0100 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=9A=A8=20Fix=20tempdir=20usage=20?= =?UTF-8?q?in=20newly=20added=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 + Cargo.toml | 3 +++ src/path.rs | 28 +++++++++++++++++----------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7b340b..ea843bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2571,6 +2571,7 @@ dependencies = [ "nu-protocol", "nu-std", "nu-utils", + "tempfile", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index 349623b..0987d51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/path.rs b/src/path.rs index 0fc2347..1f3c4e4 100644 --- a/src/path.rs +++ b/src/path.rs @@ -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 { @@ -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 @@ -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); @@ -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