2020-09-17 19:43:04 -07:00
|
|
|
use crate::common::*;
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
2020-09-17 19:43:04 -07:00
|
|
|
macro_rules! test {
|
2021-08-27 17:01:50 -07:00
|
|
|
{
|
|
|
|
name: $name:ident,
|
2021-07-26 01:26:06 -07:00
|
|
|
$(justfile: $justfile:expr,)?
|
2021-08-27 17:01:50 -07:00
|
|
|
$(args: ($($arg:tt),*),)?
|
|
|
|
$(env: { $($env_key:literal : $env_value:literal,)* },)?
|
|
|
|
$(stdin: $stdin:expr,)?
|
|
|
|
$(stdout: $stdout:expr,)?
|
|
|
|
$(stderr: $stderr:expr,)?
|
|
|
|
$(stderr_regex: $stderr_regex:expr,)?
|
|
|
|
$(status: $status:expr,)?
|
|
|
|
$(shell: $shell:expr,)?
|
|
|
|
} => {
|
2020-09-17 19:43:04 -07:00
|
|
|
#[test]
|
|
|
|
fn $name() {
|
2021-07-26 01:26:06 -07:00
|
|
|
let test = crate::test::Test::new();
|
|
|
|
|
|
|
|
$($(let test = test.arg($arg);)*)?
|
|
|
|
$($(let test = test.env($env_key, $env_value);)*)?
|
|
|
|
$(let test = test.justfile($justfile);)?
|
|
|
|
$(let test = test.shell($shell);)?
|
|
|
|
$(let test = test.status($status);)?
|
|
|
|
$(let test = test.stderr($stderr);)?
|
2021-08-27 17:01:50 -07:00
|
|
|
$(let test = test.stderr_regex($stderr_regex);)?
|
2021-07-26 01:26:06 -07:00
|
|
|
$(let test = test.stdin($stdin);)?
|
|
|
|
$(let test = test.stdout($stdout);)?
|
|
|
|
|
|
|
|
test.run();
|
2020-09-17 19:43:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) struct Test {
|
2021-07-28 00:33:44 -07:00
|
|
|
pub(crate) args: Vec<String>,
|
2021-07-31 12:25:49 -07:00
|
|
|
pub(crate) current_dir: PathBuf,
|
2021-07-28 00:33:44 -07:00
|
|
|
pub(crate) env: BTreeMap<String, String>,
|
2021-07-31 12:25:49 -07:00
|
|
|
pub(crate) justfile: Option<String>,
|
|
|
|
pub(crate) shell: bool,
|
|
|
|
pub(crate) status: i32,
|
2021-07-28 00:33:44 -07:00
|
|
|
pub(crate) stderr: String,
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) stderr_regex: Option<Regex>,
|
2021-07-31 12:25:49 -07:00
|
|
|
pub(crate) stdin: String,
|
|
|
|
pub(crate) stdout: String,
|
|
|
|
pub(crate) tempdir: TempDir,
|
2021-10-14 00:35:15 -07:00
|
|
|
pub(crate) unindent_stdout: bool,
|
2020-09-17 19:43:04 -07:00
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
impl Test {
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Self::with_tempdir(tempdir())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn with_tempdir(tempdir: TempDir) -> Self {
|
|
|
|
Self {
|
|
|
|
args: Vec::new(),
|
2021-07-31 12:25:49 -07:00
|
|
|
current_dir: PathBuf::new(),
|
2021-07-26 01:26:06 -07:00
|
|
|
env: BTreeMap::new(),
|
|
|
|
justfile: Some(String::new()),
|
|
|
|
shell: true,
|
|
|
|
status: EXIT_SUCCESS,
|
|
|
|
stderr: String::new(),
|
2021-07-28 00:33:44 -07:00
|
|
|
stderr_regex: None,
|
2021-07-26 01:26:06 -07:00
|
|
|
stdin: String::new(),
|
|
|
|
stdout: String::new(),
|
|
|
|
tempdir,
|
2021-10-14 00:35:15 -07:00
|
|
|
unindent_stdout: true,
|
2020-09-17 19:43:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn arg(mut self, val: &str) -> Self {
|
|
|
|
self.args.push(val.to_owned());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn args(mut self, args: &[&str]) -> Self {
|
|
|
|
for arg in args {
|
|
|
|
self = self.arg(arg);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-07-31 12:25:49 -07:00
|
|
|
pub(crate) fn current_dir(mut self, path: impl AsRef<Path>) -> Self {
|
|
|
|
self.current_dir = path.as_ref().to_owned();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn env(mut self, key: &str, val: &str) -> Self {
|
|
|
|
self.env.insert(key.to_string(), val.to_string());
|
|
|
|
self
|
|
|
|
}
|
2020-09-17 19:43:04 -07:00
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn justfile(mut self, justfile: impl Into<String>) -> Self {
|
|
|
|
self.justfile = Some(justfile.into());
|
|
|
|
self
|
|
|
|
}
|
2021-03-30 17:30:32 -07:00
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn justfile_path(&self) -> PathBuf {
|
|
|
|
self.tempdir.path().join("justfile")
|
|
|
|
}
|
2020-09-17 19:43:04 -07:00
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn no_justfile(mut self) -> Self {
|
|
|
|
self.justfile = None;
|
|
|
|
self
|
|
|
|
}
|
2020-09-17 19:43:04 -07:00
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn shell(mut self, shell: bool) -> Self {
|
|
|
|
self.shell = shell;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn status(mut self, exit_status: i32) -> Self {
|
|
|
|
self.status = exit_status;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn stderr(mut self, stderr: impl Into<String>) -> Self {
|
|
|
|
self.stderr = stderr.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn stderr_regex(mut self, stderr_regex: impl AsRef<str>) -> Self {
|
2022-01-30 12:32:38 -08:00
|
|
|
self.stderr_regex = Some(Regex::new(&format!("(?m)^{}$", stderr_regex.as_ref())).unwrap());
|
2021-07-26 01:26:06 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn stdin(mut self, stdin: impl Into<String>) -> Self {
|
|
|
|
self.stdin = stdin.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn stdout(mut self, stdout: impl Into<String>) -> Self {
|
|
|
|
self.stdout = stdout.into();
|
|
|
|
self
|
|
|
|
}
|
2021-07-28 00:33:44 -07:00
|
|
|
|
2021-07-31 12:25:49 -07:00
|
|
|
pub(crate) fn tree(self, mut tree: Tree) -> Self {
|
|
|
|
tree.map(|_name, content| unindent(content));
|
2022-01-30 12:16:10 -08:00
|
|
|
tree.instantiate(self.tempdir.path()).unwrap();
|
2021-07-31 12:25:49 -07:00
|
|
|
self
|
|
|
|
}
|
2021-10-14 00:35:15 -07:00
|
|
|
|
|
|
|
pub(crate) fn unindent_stdout(mut self, unindent_stdout: bool) -> Self {
|
|
|
|
self.unindent_stdout = unindent_stdout;
|
|
|
|
self
|
|
|
|
}
|
2021-07-26 01:26:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Test {
|
|
|
|
pub(crate) fn run(self) -> TempDir {
|
|
|
|
if let Some(justfile) = &self.justfile {
|
|
|
|
let justfile = unindent(justfile);
|
|
|
|
fs::write(self.justfile_path(), justfile).unwrap();
|
|
|
|
}
|
|
|
|
|
2021-10-14 00:35:15 -07:00
|
|
|
let stdout = if self.unindent_stdout {
|
|
|
|
unindent(&self.stdout)
|
|
|
|
} else {
|
|
|
|
self.stdout
|
|
|
|
};
|
2021-07-26 01:26:06 -07:00
|
|
|
let stderr = unindent(&self.stderr);
|
|
|
|
|
|
|
|
let mut dotenv_path = self.tempdir.path().to_path_buf();
|
2020-09-17 19:43:04 -07:00
|
|
|
dotenv_path.push(".env");
|
|
|
|
fs::write(dotenv_path, "DOTENV_KEY=dotenv-value").unwrap();
|
|
|
|
|
|
|
|
let mut command = Command::new(&executable_path("just"));
|
|
|
|
|
|
|
|
if self.shell {
|
|
|
|
command.args(&["--shell", "bash"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut child = command
|
|
|
|
.args(self.args)
|
2021-07-26 01:26:06 -07:00
|
|
|
.envs(&self.env)
|
2021-07-31 12:25:49 -07:00
|
|
|
.current_dir(self.tempdir.path().join(self.current_dir))
|
2020-09-17 19:43:04 -07:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("just invocation failed");
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut stdin_handle = child.stdin.take().expect("failed to unwrap stdin handle");
|
|
|
|
|
|
|
|
stdin_handle
|
|
|
|
.write_all(self.stdin.as_bytes())
|
|
|
|
.expect("failed to write stdin to just process");
|
|
|
|
}
|
|
|
|
|
|
|
|
let output = child
|
|
|
|
.wait_with_output()
|
|
|
|
.expect("failed to wait for just process");
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
fn compare<T: PartialEq + Debug>(name: &str, have: T, want: T) -> bool {
|
|
|
|
let equal = have == want;
|
|
|
|
if !equal {
|
|
|
|
eprintln!("Bad {}: {}", name, Comparison::new(&have, &want));
|
|
|
|
}
|
|
|
|
equal
|
|
|
|
}
|
|
|
|
|
|
|
|
let output_stderr = str::from_utf8(&output.stderr).unwrap();
|
2020-09-17 19:43:04 -07:00
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
if let Some(ref stderr_regex) = self.stderr_regex {
|
|
|
|
if !stderr_regex.is_match(output_stderr) {
|
|
|
|
panic!(
|
2022-01-30 12:32:38 -08:00
|
|
|
"Stderr regex mismatch:\n{:?}\n!~=\n/{:?}/",
|
2021-07-26 01:26:06 -07:00
|
|
|
output_stderr, stderr_regex
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 19:43:04 -07:00
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
if !compare("status", output.status.code().unwrap(), self.status)
|
|
|
|
| !compare("stdout", str::from_utf8(&output.stdout).unwrap(), &stdout)
|
|
|
|
| (self.stderr_regex.is_none() && !compare("stderr", output_stderr, &stderr))
|
|
|
|
{
|
|
|
|
panic!("Output mismatch.");
|
|
|
|
}
|
2020-09-17 19:43:04 -07:00
|
|
|
|
|
|
|
if self.status == EXIT_SUCCESS {
|
2021-07-26 01:26:06 -07:00
|
|
|
test_round_trip(self.tempdir.path());
|
2020-09-17 19:43:04 -07:00
|
|
|
}
|
2021-07-26 01:26:06 -07:00
|
|
|
|
|
|
|
self.tempdir
|
2020-09-17 19:43:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
struct Output<'a> {
|
|
|
|
stdout: &'a str,
|
|
|
|
stderr: &'a str,
|
|
|
|
status: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_round_trip(tmpdir: &Path) {
|
|
|
|
println!("Reparsing...");
|
|
|
|
|
|
|
|
let output = Command::new(&executable_path("just"))
|
|
|
|
.current_dir(tmpdir)
|
|
|
|
.arg("--dump")
|
|
|
|
.output()
|
|
|
|
.expect("just invocation failed");
|
|
|
|
|
|
|
|
if !output.status.success() {
|
|
|
|
panic!("dump failed: {}", output.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
let dumped = String::from_utf8(output.stdout).unwrap();
|
|
|
|
|
|
|
|
let reparsed_path = tmpdir.join("reparsed.just");
|
|
|
|
|
|
|
|
fs::write(&reparsed_path, &dumped).unwrap();
|
|
|
|
|
|
|
|
let output = Command::new(&executable_path("just"))
|
|
|
|
.current_dir(tmpdir)
|
|
|
|
.arg("--justfile")
|
|
|
|
.arg(&reparsed_path)
|
|
|
|
.arg("--dump")
|
|
|
|
.output()
|
|
|
|
.expect("just invocation failed");
|
|
|
|
|
|
|
|
if !output.status.success() {
|
|
|
|
panic!("reparse failed: {}", output.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
let reparsed = String::from_utf8(output.stdout).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(reparsed, dumped, "reparse mismatch");
|
|
|
|
}
|