Allow --justfile without --working-directory (#392)

This commit is contained in:
Casey Rodarmor 2019-04-08 00:54:05 -07:00 committed by GitHub
parent e4dcac1262
commit cee9ac21e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 11 deletions

View File

@ -106,8 +106,7 @@ pub fn run() {
.short("f") .short("f")
.long("justfile") .long("justfile")
.takes_value(true) .takes_value(true)
.help("Use <JUSTFILE> as justfile. --working-directory must also be set") .help("Use <JUSTFILE> as justfile."),
.requires("WORKING-DIRECTORY"),
) )
.arg( .arg(
Arg::with_name("LIST") Arg::with_name("LIST")
@ -243,22 +242,45 @@ pub fn run() {
}) })
.collect::<Vec<&str>>(); .collect::<Vec<&str>>();
let justfile_option = matches.value_of("JUSTFILE"); let justfile = matches.value_of("JUSTFILE").map(Path::new);
let working_directory_option = matches.value_of("WORKING-DIRECTORY"); let mut working_directory = matches.value_of("WORKING-DIRECTORY").map(PathBuf::from);
let text; if let (Some(justfile), None) = (justfile, working_directory.as_ref()) {
if let (Some(file), Some(directory)) = (justfile_option, working_directory_option) { let mut justfile = justfile.to_path_buf();
if matches.is_present("EDIT") {
edit(file); 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)) .unwrap_or_else(|error| die!("Error opening justfile: {}", error))
.slurp() .slurp()
.unwrap_or_else(|error| die!("Error reading justfile: {}", error)); .unwrap_or_else(|error| die!("Error reading justfile: {}", error));
if let Err(error) = env::set_current_dir(directory) { if let Err(error) = env::set_current_dir(&directory) {
die!("Error changing directory to {}: {}", directory, error); die!(
"Error changing directory to {}: {}",
directory.display(),
error
);
} }
} else { } else {
let name; let name;

View File

@ -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<Error>> {
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(())
}