2021-07-30 22:02:16 +02:00
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use pretty_assertions::assert_eq;
|
2022-04-11 20:18:46 +02:00
|
|
|
use std::collections::HashMap;
|
2021-07-30 22:02:16 +02:00
|
|
|
use std::io::Write;
|
|
|
|
use std::process::Command;
|
|
|
|
use tempfile::NamedTempFile;
|
2021-07-17 20:52:50 +02:00
|
|
|
|
2021-12-25 20:39:42 +01:00
|
|
|
pub type TestResult = Result<(), Box<dyn std::error::Error>>;
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2022-04-11 20:18:46 +02:00
|
|
|
pub fn run_test_with_env(input: &str, expected: &str, env: &HashMap<&str, &str>) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2024-04-23 13:37:50 +02:00
|
|
|
cmd.arg("--no-config-file");
|
2023-01-09 06:53:52 +01:00
|
|
|
cmd.arg(name).envs(env);
|
2022-04-11 20:18:46 +02:00
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2022-04-11 20:18:46 +02:00
|
|
|
|
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
2021-07-30 22:02:16 +02:00
|
|
|
#[cfg(test)]
|
2021-12-25 20:39:42 +01:00
|
|
|
pub fn run_test(input: &str, expected: &str) -> TestResult {
|
2021-07-30 22:02:16 +02:00
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2023-04-17 00:24:56 +02:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
|
|
|
cmd.arg("--no-std-lib");
|
2024-04-23 13:37:50 +02:00
|
|
|
cmd.arg("--no-config-file");
|
2023-04-17 00:24:56 +02:00
|
|
|
cmd.arg(name);
|
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
|
|
|
|
|
|
|
writeln!(file, "{input}")?;
|
|
|
|
|
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn run_test_std(input: &str, expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2024-04-23 13:37:50 +02:00
|
|
|
cmd.arg("--no-config-file");
|
2023-01-09 06:53:52 +01:00
|
|
|
cmd.arg(name);
|
2022-04-17 12:39:56 +02:00
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2022-04-11 20:18:46 +02:00
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn run_cmd_and_assert(mut cmd: Command, expected: &str) -> TestResult {
|
2021-07-30 22:02:16 +02:00
|
|
|
let output = cmd.output()?;
|
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
assert!(output.status.success());
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
assert_eq!(stdout.trim(), expected);
|
2021-07-30 22:02:16 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-22 19:24:47 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn run_test_contains(input: &str, expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-04-17 00:24:56 +02:00
|
|
|
cmd.arg("--no-std-lib");
|
2024-04-23 13:37:50 +02:00
|
|
|
cmd.arg("--no-config-file");
|
2022-01-22 19:24:47 +01:00
|
|
|
cmd.arg(name);
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2023-04-07 21:01:12 +02:00
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
|
|
|
|
2023-09-21 20:12:36 +02:00
|
|
|
println!("Expected output to contain: {expected}");
|
2023-04-07 21:01:12 +02:00
|
|
|
assert!(output.status.success());
|
|
|
|
|
|
|
|
assert!(stdout.contains(expected));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn test_ide_contains(input: &str, ide_commands: &[&str], expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-04-17 00:24:56 +02:00
|
|
|
cmd.arg("--no-std-lib");
|
2024-04-23 13:37:50 +02:00
|
|
|
cmd.arg("--no-config-file");
|
2023-04-07 21:01:12 +02:00
|
|
|
for ide_command in ide_commands {
|
|
|
|
cmd.arg(ide_command);
|
|
|
|
}
|
|
|
|
cmd.arg(name);
|
|
|
|
|
|
|
|
writeln!(file, "{input}")?;
|
2022-01-22 19:24:47 +01:00
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
2023-09-21 20:12:36 +02:00
|
|
|
println!("Expected output to contain: {expected}");
|
2022-01-22 19:24:47 +01:00
|
|
|
|
|
|
|
assert!(output.status.success());
|
|
|
|
|
|
|
|
assert!(stdout.contains(expected));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-30 22:02:16 +02:00
|
|
|
#[cfg(test)]
|
2021-12-25 20:39:42 +01:00
|
|
|
pub fn fail_test(input: &str, expected: &str) -> TestResult {
|
2021-07-30 22:02:16 +02:00
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-04-17 00:24:56 +02:00
|
|
|
cmd.arg("--no-std-lib");
|
2024-04-23 13:37:50 +02:00
|
|
|
cmd.arg("--no-config-file");
|
2021-07-30 22:02:16 +02:00
|
|
|
cmd.arg(name);
|
2022-01-05 01:26:01 +01:00
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2021-07-30 22:02:16 +02:00
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
2021-08-10 07:08:10 +02:00
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
2023-09-21 20:12:36 +02:00
|
|
|
println!("Expected error to contain: {expected}");
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2022-01-31 18:42:12 +01:00
|
|
|
assert!(!stderr.is_empty() && stderr.contains(expected));
|
2021-07-30 22:02:16 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|