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

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

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,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")
Expand Down Expand Up @@ -243,22 +242,45 @@ pub fn run() {
})
.collect::<Vec<&str>>();

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);

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
),
}
}

justfile.pop();

working_directory = Some(justfile);
}

let text;
if let (Some(file), Some(directory)) = (justfile_option, working_directory_option) {
if let (Some(justfile), Some(directory)) = (justfile, working_directory) {
if matches.is_present("EDIT") {
edit(file);
edit(justfile);
}

text = fs::File::open(file)
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;
Expand Down
35 changes: 35 additions & 0 deletions tests/working_directory.rs
Original file line number Diff line number Diff line change
@@ -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(())
}