From cee9ac21e0ab2ed503ffd07369b2f6afd8f0e137 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 8 Apr 2019 00:54:05 -0700 Subject: [PATCH] Allow `--justfile` without `--working-directory` (#392) --- src/run.rs | 44 ++++++++++++++++++++++++++++---------- tests/working_directory.rs | 35 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 tests/working_directory.rs diff --git a/src/run.rs b/src/run.rs index e6284100..8a4592ef 100644 --- a/src/run.rs +++ b/src/run.rs @@ -106,8 +106,7 @@ pub fn run() { .short("f") .long("justfile") .takes_value(true) - .help("Use as justfile. --working-directory must also be set") - .requires("WORKING-DIRECTORY"), + .help("Use as justfile."), ) .arg( Arg::with_name("LIST") @@ -243,22 +242,45 @@ pub fn run() { }) .collect::>(); - let justfile_option = matches.value_of("JUSTFILE"); - let working_directory_option = matches.value_of("WORKING-DIRECTORY"); + let justfile = matches.value_of("JUSTFILE").map(Path::new); + let mut working_directory = matches.value_of("WORKING-DIRECTORY").map(PathBuf::from); - let text; - if let (Some(file), Some(directory)) = (justfile_option, working_directory_option) { - if matches.is_present("EDIT") { - edit(file); + if let (Some(justfile), None) = (justfile, working_directory.as_ref()) { + let mut justfile = justfile.to_path_buf(); + + if !justfile.is_absolute() { + match justfile.canonicalize() { + Ok(canonical) => justfile = canonical, + Err(err) => die!( + "Could not canonicalize justfile path `{}`: {}", + justfile.display(), + err + ), + } } - text = fs::File::open(file) + justfile.pop(); + + working_directory = Some(justfile); + } + + let text; + if let (Some(justfile), Some(directory)) = (justfile, working_directory) { + if matches.is_present("EDIT") { + edit(justfile); + } + + text = fs::File::open(justfile) .unwrap_or_else(|error| die!("Error opening justfile: {}", error)) .slurp() .unwrap_or_else(|error| die!("Error reading justfile: {}", error)); - if let Err(error) = env::set_current_dir(directory) { - die!("Error changing directory to {}: {}", directory, error); + if let Err(error) = env::set_current_dir(&directory) { + die!( + "Error changing directory to {}: {}", + directory.display(), + error + ); } } else { let name; diff --git a/tests/working_directory.rs b/tests/working_directory.rs new file mode 100644 index 00000000..7bd37494 --- /dev/null +++ b/tests/working_directory.rs @@ -0,0 +1,35 @@ +extern crate executable_path; +extern crate tempdir; + +use executable_path::executable_path; +use std::{error::Error, fs, process::Command}; +use tempdir::TempDir; + +/// Test that just runs with the correct working directory when invoked with +/// `--justfile` but not `--working-directory` +#[test] +fn justfile_without_working_directory() -> Result<(), Box> { + let tmp = TempDir::new("just-integration")?; + let justfile = tmp.path().join("justfile"); + let data = tmp.path().join("data"); + fs::write( + &justfile, + "foo = `cat data`\ndefault:\n echo {{foo}}\n cat data", + )?; + fs::write(&data, "found it")?; + + let output = Command::new(executable_path("just")) + .arg("--justfile") + .arg(&justfile) + .output()?; + + if !output.status.success() { + panic!() + } + + let stdout = String::from_utf8(output.stdout).unwrap(); + + assert_eq!(stdout, "found it\nfound it"); + + Ok(()) +}