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