2022-12-23 19:19:10 +00:00
|
|
|
mod test_bits;
|
2022-12-21 22:54:39 +00:00
|
|
|
mod test_cell_path;
|
2021-12-25 19:39:42 +00:00
|
|
|
mod test_conditionals;
|
2022-08-23 07:18:14 +00:00
|
|
|
mod test_config_path;
|
2021-12-25 19:39:42 +00:00
|
|
|
mod test_converters;
|
|
|
|
mod test_custom_commands;
|
|
|
|
mod test_engine;
|
|
|
|
mod test_env;
|
|
|
|
mod test_hiding;
|
|
|
|
mod test_iteration;
|
2022-04-17 10:39:56 +00:00
|
|
|
mod test_known_external;
|
2021-12-25 19:39:42 +00:00
|
|
|
mod test_math;
|
|
|
|
mod test_modules;
|
|
|
|
mod test_parser;
|
|
|
|
mod test_ranges;
|
2022-04-07 06:23:14 +00:00
|
|
|
mod test_regex;
|
2021-12-25 19:39:42 +00:00
|
|
|
mod test_strings;
|
|
|
|
mod test_table_operations;
|
|
|
|
mod test_type_check;
|
|
|
|
|
2021-07-30 20:02:16 +00:00
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use pretty_assertions::assert_eq;
|
2022-04-11 18:18:46 +00:00
|
|
|
use std::collections::HashMap;
|
2021-07-30 20:02:16 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::process::Command;
|
|
|
|
use tempfile::NamedTempFile;
|
2021-07-17 18:52:50 +00:00
|
|
|
|
2021-12-25 19:39:42 +00:00
|
|
|
pub type TestResult = Result<(), Box<dyn std::error::Error>>;
|
2021-07-30 20:02:16 +00:00
|
|
|
|
2022-04-11 18:18:46 +00: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")?;
|
2023-01-04 07:59:10 +00:00
|
|
|
// Use this minimal config in most tests.
|
|
|
|
// Notably, this disables color_config to allow string output to be more easily compared.
|
|
|
|
cmd.arg("--config")
|
|
|
|
.arg(
|
|
|
|
nu_test_support::fs::fixtures()
|
|
|
|
.join("playground/config/minimal.nu")
|
|
|
|
.display()
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
.arg(name)
|
|
|
|
.envs(env);
|
2022-04-11 18:18:46 +00:00
|
|
|
|
|
|
|
writeln!(file, "{}", input)?;
|
|
|
|
|
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:02:16 +00:00
|
|
|
#[cfg(test)]
|
2021-12-25 19:39:42 +00:00
|
|
|
pub fn run_test(input: &str, expected: &str) -> TestResult {
|
2021-07-30 20:02:16 +00:00
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-01-04 07:59:10 +00:00
|
|
|
// Use this minimal config in most tests.
|
|
|
|
// Notably, this disables color_config to allow string output to be more easily compared.
|
|
|
|
cmd.arg("--config")
|
|
|
|
.arg(
|
|
|
|
nu_test_support::fs::fixtures()
|
|
|
|
.join("playground/config/minimal.nu")
|
|
|
|
.display()
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
.arg(name);
|
2022-04-17 10:39:56 +00:00
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
|
|
|
writeln!(file, "{}", input)?;
|
|
|
|
|
2022-04-11 18:18:46 +00:00
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn run_cmd_and_assert(mut cmd: Command, expected: &str) -> TestResult {
|
2021-07-30 20:02:16 +00:00
|
|
|
let output = cmd.output()?;
|
|
|
|
|
2021-07-30 21:57:22 +00:00
|
|
|
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);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
2021-07-30 21:57:22 +00:00
|
|
|
assert!(output.status.success());
|
2021-07-30 20:02:16 +00:00
|
|
|
|
2021-07-30 21:57:22 +00:00
|
|
|
assert_eq!(stdout.trim(), expected);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-22 18:24:47 +00: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 20:59:01 +00:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2022-01-22 18:24:47 +00:00
|
|
|
cmd.arg(name);
|
|
|
|
|
|
|
|
writeln!(file, "{}", input)?;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
assert!(output.status.success());
|
|
|
|
|
|
|
|
assert!(stdout.contains(expected));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:02:16 +00:00
|
|
|
#[cfg(test)]
|
2021-12-25 19:39:42 +00:00
|
|
|
pub fn fail_test(input: &str, expected: &str) -> TestResult {
|
2021-07-30 20:02:16 +00:00
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2021-07-30 20:02:16 +00:00
|
|
|
cmd.arg(name);
|
2022-01-05 00:26:01 +00:00
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
|
|
|
writeln!(file, "{}", input)?;
|
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
2021-08-10 05:08:10 +00:00
|
|
|
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);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
2022-01-31 17:42:12 +00:00
|
|
|
assert!(!stderr.is_empty() && stderr.contains(expected));
|
2021-07-30 20:02:16 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|