From d2e452a696a7eb94f3f81aa047687b984df7f11a Mon Sep 17 00:00:00 2001 From: kyu08 <49891479+kyu08@users.noreply.github.com> Date: Sun, 24 Nov 2024 01:22:38 +0900 Subject: [PATCH] specify_makefile_name: receive current_dir as an argument instead of using `env::current_dir` --- src/file/toml.rs | 6 ------ src/model/histories.rs | 8 -------- src/model/make.rs | 29 ++++++++++++++--------------- src/usecase/tui/app.rs | 2 +- 4 files changed, 15 insertions(+), 30 deletions(-) diff --git a/src/file/toml.rs b/src/file/toml.rs index 133ec69..72d9ef6 100644 --- a/src/file/toml.rs +++ b/src/file/toml.rs @@ -52,12 +52,6 @@ impl Histories { } } -// impl std::default::Default for Histories { -// fn default() -> Self { -// Self { histories: vec![] } -// } -// } - #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] struct History { path: PathBuf, diff --git a/src/model/histories.rs b/src/model/histories.rs index b8013e7..86c8938 100644 --- a/src/model/histories.rs +++ b/src/model/histories.rs @@ -55,14 +55,6 @@ pub struct History { } impl History { - // #[allow(dead_code)] - // fn from(histories: (PathBuf, Vec)) -> Self { - // Self { - // path: histories.0, - // commands: histories.1, - // } - // } - // TODO: ut fn append(&self, executed_command: command::Command) -> Self { let mut updated_commands = self.commands.clone(); diff --git a/src/model/make.rs b/src/model/make.rs index 066a81c..b1e8f1e 100644 --- a/src/model/make.rs +++ b/src/model/make.rs @@ -3,7 +3,7 @@ use anyhow::{anyhow, Result}; use regex::Regex; use std::process; use std::{ - env, fs, + fs, path::{Path, PathBuf}, }; @@ -20,8 +20,8 @@ impl Make { format!("make {}", command.name) } - pub fn create_makefile() -> Result { - let Some(makefile_name) = Make::specify_makefile_name(".".to_string()) else { + pub fn create_makefile(current_dir: PathBuf) -> Result { + let Some(makefile_name) = Make::specify_makefile_name(current_dir, ".".to_string()) else { return Err(anyhow!("makefile not found.\n")); }; Make::new(Path::new(&makefile_name).to_path_buf()) @@ -56,7 +56,7 @@ impl Make { }) } - fn specify_makefile_name(target_path: String) -> Option { + fn specify_makefile_name(current_dir: PathBuf, target_path: String) -> Option { // By default, when make looks for the makefile, it tries the following names, in order: GNUmakefile, makefile and Makefile. // https://www.gnu.org/software/make/manual/make.html#Makefile-Names // It needs to enumerate `Makefile` too not only `makefile` to make it work on case insensitive file system @@ -68,12 +68,6 @@ impl Make { let file_name = e.unwrap().file_name(); let file_name_string = file_name.to_str().unwrap(); if makefile_name_options.contains(&file_name_string) { - let current_dir = match env::current_dir() { - // TODO: use model.current_dir - Err(_) => return None, - Ok(d) => d, - }; - temp_result.push(current_dir.join(file_name)); } } @@ -104,6 +98,7 @@ impl Make { #[cfg(test)] pub fn new_for_test() -> Make { use super::runner_type; + use std::env; Make { path: env::current_dir().unwrap().join(Path::new("Test.mk")), @@ -174,11 +169,12 @@ fn line_to_including_file_paths(line: String) -> Option> { #[cfg(test)] mod test { - use crate::model::runner_type; - use super::*; - - use std::fs::{self, File}; + use crate::model::runner_type; + use std::{ + env, + fs::{self, File}, + }; use uuid::Uuid; #[test] @@ -231,7 +227,10 @@ mod test { assert_eq!( expect, - Make::specify_makefile_name(tmp_dir.to_string_lossy().to_string()), + Make::specify_makefile_name( + env::current_dir().unwrap(), + tmp_dir.to_string_lossy().to_string() + ), "\nFailed: 🚨{:?}🚨\n", case.title, ); diff --git a/src/usecase/tui/app.rs b/src/usecase/tui/app.rs index 68a1dc3..a933d2f 100644 --- a/src/usecase/tui/app.rs +++ b/src/usecase/tui/app.rs @@ -347,7 +347,7 @@ impl SelectTargetState<'_> { Ok(d) => d, Err(e) => bail!("Failed to get current directory: {}", e), }; - let makefile = match Make::create_makefile() { + let makefile = match Make::create_makefile(current_dir.clone()) { Err(e) => return Err(e), Ok(f) => f, };