1
1
mirror of https://github.com/casey/just.git synced 2024-11-26 23:27:26 +03:00
just/tests/choose.rs

200 lines
3.4 KiB
Rust
Raw Normal View History

use crate::common::*;
test! {
name: env,
justfile: "
foo:
echo foo
bar:
echo bar
",
args: ("--choose"),
env: {
"JUST_CHOOSER": "head -n1",
},
stdout: "bar\n",
stderr: "echo bar\n",
}
test! {
name: chooser,
justfile: "
foo:
echo foo
bar:
echo bar
",
args: ("--choose", "--chooser", "head -n1"),
stdout: "bar\n",
stderr: "echo bar\n",
}
test! {
name: override_variable,
justfile: "
baz := 'A'
foo:
echo foo
bar:
echo {{baz}}
",
args: ("--choose", "baz=B"),
env: {
"JUST_CHOOSER": "head -n1",
},
stdout: "B\n",
stderr: "echo B\n",
}
test! {
name: skip_private_recipes,
justfile: "
foo:
echo foo
_bar:
echo bar
",
args: ("--choose"),
env: {
"JUST_CHOOSER": "head -n1",
},
stdout: "foo\n",
stderr: "echo foo\n",
}
test! {
name: skip_recipes_that_require_arguments,
justfile: "
foo:
echo foo
bar BAR:
echo {{BAR}}
",
args: ("--choose"),
env: {
"JUST_CHOOSER": "head -n1",
},
stdout: "foo\n",
stderr: "echo foo\n",
}
test! {
name: no_choosable_recipes,
justfile: "
_foo:
echo foo
bar BAR:
echo {{BAR}}
",
args: ("--choose"),
stdout: "",
stderr: "error: Justfile contains no choosable recipes.\n",
status: EXIT_FAILURE,
}
test! {
name: multiple_recipes,
justfile: "
foo:
echo foo
bar:
echo bar
",
args: ("--choose", "--chooser", "echo foo bar"),
stdout: "foo\nbar\n",
stderr: "echo foo\necho bar\n",
}
#[test]
fn invoke_error_function() {
Test::new()
.justfile(
"
foo:
echo foo
bar:
echo bar
",
)
.stderr(if cfg!(windows) {
"error: Chooser `/ -cu fzf` invocation failed: Access is denied. (os error 5)\n"
} else {
"error: Chooser `/ -cu fzf` invocation failed: Permission denied (os error 13)\n"
})
.status(EXIT_FAILURE)
.shell(false)
.args(&["--shell", "/", "--choose"])
.run();
}
#[test]
#[cfg(not(windows))]
fn status_error() {
let tmp = temptree! {
justfile: "foo:\n echo foo\nbar:\n echo bar\n",
"exit-2": "#!/usr/bin/env bash\nexit 2\n",
};
cmd_unit!(%"chmod +x", tmp.path().join("exit-2"));
let path = env::join_paths(
iter::once(tmp.path().to_owned()).chain(env::split_paths(&env::var_os("PATH").unwrap())),
)
.unwrap();
let output = Command::new(executable_path("just"))
.current_dir(tmp.path())
.arg("--choose")
.arg("--chooser")
.arg("exit-2")
.env("PATH", path)
.output()
.unwrap();
assert_eq!(
String::from_utf8_lossy(&output.stderr),
"error: Chooser `exit-2` failed: exit code: 2\n",
);
assert_eq!(output.status.code().unwrap(), 2);
}
#[test]
fn default() {
2021-07-04 00:26:59 +03:00
let tmp = temptree! {
justfile: "foo:\n echo foo\n",
};
let cat = which("cat").unwrap();
let fzf = tmp.path().join(format!("fzf{}", env::consts::EXE_SUFFIX));
#[cfg(unix)]
std::os::unix::fs::symlink(cat, fzf).unwrap();
#[cfg(windows)]
std::os::windows::fs::symlink_file(cat, fzf).unwrap();
let path = env::join_paths(
iter::once(tmp.path().to_owned()).chain(env::split_paths(&env::var_os("PATH").unwrap())),
)
.unwrap();
let output = Command::new(executable_path("just"))
.arg("--choose")
.current_dir(tmp.path())
.env("PATH", path)
.output()
.unwrap();
assert_stdout(&output, "foo\n");
}