Skip to content

Commit

Permalink
specify_makefile_name: receive current_dir as an argument instead of …
Browse files Browse the repository at this point in the history
…using `env::current_dir`
  • Loading branch information
kyu08 committed Nov 23, 2024
1 parent 7c20257 commit 51d3857
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 30 deletions.
6 changes: 0 additions & 6 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions src/model/histories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ pub struct History {
}

impl History {
// #[allow(dead_code)]
// fn from(histories: (PathBuf, Vec<command::Command>)) -> 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();
Expand Down
29 changes: 14 additions & 15 deletions src/model/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::{anyhow, Result};
use regex::Regex;
use std::process;
use std::{
env, fs,
fs,
path::{Path, PathBuf},
};

Expand All @@ -20,8 +20,8 @@ impl Make {
format!("make {}", command.name)
}

pub fn create_makefile() -> Result<Make> {
let Some(makefile_name) = Make::specify_makefile_name(".".to_string()) else {
pub fn create_makefile(current_dir: PathBuf) -> Result<Make> {
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())
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Make {
})
}

fn specify_makefile_name(target_path: String) -> Option<PathBuf> {
fn specify_makefile_name(current_dir: PathBuf, target_path: String) -> Option<PathBuf> {
// 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
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -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")),
Expand Down Expand Up @@ -174,11 +169,12 @@ fn line_to_including_file_paths(line: String) -> Option<Vec<PathBuf>> {

#[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]
Expand Down Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion src/usecase/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,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,
};
Expand Down

0 comments on commit 51d3857

Please sign in to comment.