just/tests/working_directory.rs
Casey Rodarmor aefdcea7d0
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
  to be implicitly inherited by subprocesses, we now use
  `Command::current_dir` to set it explicitly. This feels much better,
  since we aren't dependent on the implicit state of the process's
  current directory.

- Subcommand execution is much improved.

- Added a ton of tests for config parsing, config execution, working
  dir, and search dir.

- Error messages are improved. Many more will be colored.

- The Config is now onwed, instead of borrowing from the arguments and
  the `clap::ArgMatches` object. This is a huge ergonomic improvement,
  especially in tests, and I don't think anyone will notice.

- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
  matching git, which I think is what most people will expect.

- Added a cute `tmptree!{}` macro, for creating temporary directories
  populated with directories and files for tests.

- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00

187 lines
4.2 KiB
Rust

use std::{error::Error, process::Command};
use executable_path::executable_path;
use test_utilities::tmptree;
const JUSTFILE: &str = r#"
foo := `cat data`
linewise bar=`cat data`: shebang
echo expression: {{foo}}
echo default: {{bar}}
echo linewise: `cat data`
shebang:
#!/usr/bin/env sh
echo "shebang:" `cat data`
"#;
const DATA: &str = "OK";
const WANT: &str = "shebang: OK\nexpression: OK\ndefault: OK\nlinewise: OK\n";
/// 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<dyn Error>> {
let tmp = tmptree! {
justfile: JUSTFILE,
data: DATA,
};
let output = Command::new(executable_path("just"))
.arg("--justfile")
.arg(&tmp.path().join("justfile"))
.output()?;
if !output.status.success() {
eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
panic!();
}
let stdout = String::from_utf8(output.stdout).unwrap();
assert_eq!(stdout, WANT);
Ok(())
}
/// Test that just runs with the correct working directory when invoked with
/// `--justfile` but not `--working-directory`, and justfile path has no
/// parent
#[test]
fn justfile_without_working_directory_relative() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! {
justfile: JUSTFILE,
data: DATA,
};
let output = Command::new(executable_path("just"))
.current_dir(&tmp.path())
.arg("--justfile")
.arg("justfile")
.output()?;
if !output.status.success() {
eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
panic!();
}
let stdout = String::from_utf8(output.stdout).unwrap();
assert_eq!(stdout, WANT);
Ok(())
}
/// Test that just invokes commands from the directory in which the justfile is found
#[test]
fn change_working_directory_to_search_justfile_parent() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! {
justfile: JUSTFILE,
data: DATA,
subdir: {},
};
let output = Command::new(executable_path("just"))
.current_dir(tmp.path().join("subdir"))
.output()?;
if !output.status.success() {
eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
panic!();
}
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout, WANT);
Ok(())
}
/// Test that just runs with the correct working directory when invoked with
/// `--justfile` but not `--working-directory`
#[test]
fn justfile_and_working_directory() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! {
justfile: JUSTFILE,
sub: {
data: DATA,
},
};
let output = Command::new(executable_path("just"))
.arg("--justfile")
.arg(&tmp.path().join("justfile"))
.arg("--working-directory")
.arg(&tmp.path().join("sub"))
.output()?;
if !output.status.success() {
eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
panic!();
}
let stdout = String::from_utf8(output.stdout).unwrap();
assert_eq!(stdout, WANT);
Ok(())
}
/// Test that just runs with the correct working directory when invoked with
/// `--justfile` but not `--working-directory`
#[test]
fn search_dir_child() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! {
child: {
justfile: JUSTFILE,
data: DATA,
},
};
let output = Command::new(executable_path("just"))
.current_dir(&tmp.path())
.arg("child/")
.output()?;
if !output.status.success() {
eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
panic!();
}
let stdout = String::from_utf8(output.stdout).unwrap();
assert_eq!(stdout, WANT);
Ok(())
}
/// Test that just runs with the correct working directory when invoked with
/// `--justfile` but not `--working-directory`
#[test]
fn search_dir_parent() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! {
child: {
},
justfile: JUSTFILE,
data: DATA,
};
let output = Command::new(executable_path("just"))
.current_dir(&tmp.path().join("child"))
.arg("../")
.output()?;
if !output.status.success() {
eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
panic!();
}
let stdout = String::from_utf8(output.stdout).unwrap();
assert_eq!(stdout, WANT);
Ok(())
}