Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#340 Allow justfile without workdir #355

Closed
wants to merge 9 commits into from
27 changes: 21 additions & 6 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ pub fn run() {
.short("f")
.long("justfile")
.takes_value(true)
.help("Use <JUSTFILE> as justfile. --working-directory must also be set")
.requires("WORKING-DIRECTORY"))
.help("Use <JUSTFILE> as justfile"))
.arg(Arg::with_name("LIST")
.short("l")
.long("list")
Expand Down Expand Up @@ -186,8 +185,24 @@ pub fn run() {
})
.collect::<Vec<&str>>();

let justfile_option = matches.value_of("JUSTFILE");
let working_directory_option = matches.value_of("WORKING-DIRECTORY");
let justfile_option = matches.value_of("JUSTFILE").map(Path::new);
let mut working_directory_option = matches.value_of("WORKING-DIRECTORY").map(PathBuf::from);

if let (Some(justfile), None) = (justfile_option, working_directory_option.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),
}
}

justfile.pop();

working_directory_option = Some(justfile);
}

let text;
if let (Some(file), Some(directory)) = (justfile_option, working_directory_option) {
Expand All @@ -200,8 +215,8 @@ pub fn run() {
.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;
Expand Down
9 changes: 9 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1834,3 +1834,12 @@ X = "\'"
"#,
status: EXIT_FAILURE,
}

integration_test! {
name: optional_working_directory,
justfile: "home:\n @echo works without --WORKING-DIRECTORY\n",
args: ("--justfile", "./justfile"),
stdout: "works without --WORKING-DIRECTORY\n",
stderr: "",
status: EXIT_SUCCESS,
}