2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2018-06-19 10:04:03 -07:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
2021-02-15 01:18:31 -08:00
|
|
|
fn convert_native_path(path: &Path) -> String {
|
2018-12-08 14:29:41 -08:00
|
|
|
fs::canonicalize(path)
|
|
|
|
.expect("canonicalize failed")
|
|
|
|
.to_str()
|
|
|
|
.map(str::to_string)
|
|
|
|
.expect("unicode decode failed")
|
2018-06-19 10:04:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2021-02-15 01:18:31 -08:00
|
|
|
fn convert_native_path(path: &Path) -> String {
|
2018-06-19 10:04:03 -07:00
|
|
|
// Translate path from windows style to unix style
|
2021-07-03 14:26:59 -07:00
|
|
|
let mut cygpath = Command::new("cygpath");
|
2018-06-19 10:04:03 -07:00
|
|
|
cygpath.arg("--unix");
|
|
|
|
cygpath.arg(path);
|
2019-07-13 01:55:06 -07:00
|
|
|
|
|
|
|
let output = cygpath.output().expect("executing cygpath failed");
|
|
|
|
|
|
|
|
assert!(output.status.success());
|
|
|
|
|
|
|
|
let stdout = str::from_utf8(&output.stdout).expect("cygpath output was not utf8");
|
|
|
|
|
|
|
|
if stdout.ends_with('\n') {
|
|
|
|
&stdout[0..stdout.len() - 1]
|
|
|
|
} else if stdout.ends_with("\r\n") {
|
|
|
|
&stdout[0..stdout.len() - 2]
|
|
|
|
} else {
|
|
|
|
stdout
|
|
|
|
}
|
|
|
|
.to_owned()
|
2018-06-19 10:04:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invocation_directory() {
|
2019-07-06 20:55:46 -07:00
|
|
|
let tmp = tempdir();
|
2018-06-19 10:04:03 -07:00
|
|
|
|
|
|
|
let mut justfile_path = tmp.path().to_path_buf();
|
|
|
|
justfile_path.push("justfile");
|
2019-04-16 22:06:28 -07:00
|
|
|
fs::write(
|
2018-12-08 14:29:41 -08:00
|
|
|
justfile_path,
|
|
|
|
"default:\n @cd {{invocation_directory()}}\n @echo {{invocation_directory()}}",
|
2019-04-16 22:06:28 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-06-19 10:04:03 -07:00
|
|
|
|
|
|
|
let mut subdir = tmp.path().to_path_buf();
|
|
|
|
subdir.push("subdir");
|
2019-04-16 22:06:28 -07:00
|
|
|
fs::create_dir(&subdir).unwrap();
|
2018-06-19 10:04:03 -07:00
|
|
|
|
2022-12-15 16:53:21 -08:00
|
|
|
let output = Command::new(executable_path("just"))
|
2018-06-19 10:04:03 -07:00
|
|
|
.current_dir(&subdir)
|
2022-11-22 16:36:23 -08:00
|
|
|
.args(["--shell", "sh"])
|
2018-06-19 10:04:03 -07:00
|
|
|
.output()
|
|
|
|
.expect("just invocation failed");
|
|
|
|
|
|
|
|
let mut failure = false;
|
|
|
|
|
|
|
|
let expected_status = 0;
|
2021-02-15 01:18:31 -08:00
|
|
|
let expected_stdout = convert_native_path(&subdir) + "\n";
|
2018-06-19 10:04:03 -07:00
|
|
|
let expected_stderr = "";
|
|
|
|
|
|
|
|
let status = output.status.code().unwrap();
|
|
|
|
if status != expected_status {
|
2023-01-26 18:49:03 -08:00
|
|
|
println!("bad status: {status} != {expected_status}");
|
2018-06-19 10:04:03 -07:00
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stdout = str::from_utf8(&output.stdout).unwrap();
|
|
|
|
if stdout != expected_stdout {
|
2023-01-26 18:49:03 -08:00
|
|
|
println!("bad stdout:\ngot:\n{stdout:?}\n\nexpected:\n{expected_stdout:?}");
|
2018-06-19 10:04:03 -07:00
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stderr = str::from_utf8(&output.stderr).unwrap();
|
|
|
|
if stderr != expected_stderr {
|
2023-01-26 18:49:03 -08:00
|
|
|
println!("bad stderr:\ngot:\n{stderr:?}\n\nexpected:\n{expected_stderr:?}");
|
2018-06-19 10:04:03 -07:00
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if failure {
|
|
|
|
panic!("test failed");
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 11:03:14 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invocation_directory_native() {
|
2024-01-11 19:22:27 -08:00
|
|
|
let Output {
|
|
|
|
stdout, tempdir, ..
|
|
|
|
} = Test::new()
|
2023-01-13 11:03:14 -08:00
|
|
|
.justfile("x := invocation_directory_native()")
|
|
|
|
.args(["--evaluate", "x"])
|
|
|
|
.stdout_regex(".*")
|
|
|
|
.run();
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_eq!(Path::new(&stdout), tempdir.path());
|
|
|
|
} else {
|
|
|
|
assert_eq!(Path::new(&stdout), tempdir.path().canonicalize().unwrap());
|
|
|
|
}
|
|
|
|
}
|