Files
PiepmatzandBahex 43bcebc28e Update parser tests (#18668)
<!--

Thanks for contributing to Nushell!



Before submitting, please read the contributing guide:

https://github.com/nushell/nushell/blob/main/CONTRIBUTING.md



This template helps reviewers understand your changes and allows us to
generate high-quality release notes.

-->



## Description

<!--

Explain what this PR does and why.



This section is intentionally flexible:

- Describe the problem

- Explain your approach

- Include technical details if relevant



Good examples:

- "In this PR, I fixed..."

- "In this PR, I added support for..."

- "This change improves X by..."



Write as much or as little as needed for reviewers to understand your
changes.

-->
This PR updates the tests in `tests/repl/test_parser.rs`. They used a
lot of helpers which I completely removed their usage in this file.
Otherwise this just modernizes these tests.


## User-facing changes (Release notes)

<!--

This section is used (mostly as-is) for https://www.nushell.sh/blog/



Describe how Nushell behavior changes from a user's perspective.

Do NOT describe internal Rust changes here.



Write in a release note style, for example:

- "Added support for..."

- "Fixed an issue where..."

- "Nushell now supports..."

- "Improved performance of..."



If your changes do NOT affect users (internal refactors, cleanup, etc.),

just write:

- "n/a"

- "nan"

- or similar



This tells us the change should not appear in the changelog.



Tips:

- Focus on observable behavior

- Include examples if helpful

- Keep it concise



You can:

- Write a short paragraph (will appear as a bullet point), OR

- Use headings (###) if your change needs more structure



Avoid writing things like:

- "In this PR, I refactored..."

- "This updates internal code..."



You may leave this blank until the PR is ready.

-->
n/a

---

<sub>Stack created with <a
href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a
href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

---------

Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com>
2026-07-26 20:08:36 +02:00

157 lines
3.8 KiB
Rust

use assert_cmd::cargo_bin;
use pretty_assertions::assert_eq;
use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;
pub type TestResult = Result<(), Box<dyn std::error::Error>>;
#[cfg(test)]
#[track_caller]
pub fn run_test(input: &str, expected: &str) -> TestResult {
let mut file = NamedTempFile::new()?;
let name = file.path();
let mut cmd = Command::new(cargo_bin!());
cmd.arg("--no-std-lib");
cmd.arg("--no-config-file");
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)]
#[track_caller]
pub fn run_test_std(input: &str, expected: &str) -> TestResult {
let mut file = NamedTempFile::new()?;
let name = file.path();
let mut cmd = Command::new(cargo_bin!());
cmd.arg("--no-config-file");
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)]
#[track_caller]
fn run_cmd_and_assert(mut cmd: Command, expected: &str) -> TestResult {
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_eq!(stdout.trim(), expected);
Ok(())
}
#[cfg(test)]
#[track_caller]
pub fn run_test_contains(input: &str, expected: &str) -> TestResult {
let mut file = NamedTempFile::new()?;
let name = file.path();
let mut cmd = Command::new(cargo_bin!());
cmd.arg("--no-std-lib");
cmd.arg("--no-config-file");
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}");
println!("Expected output to contain: {expected}");
assert!(output.status.success());
assert!(stdout.contains(expected));
Ok(())
}
#[cfg(test)]
#[track_caller]
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::new(cargo_bin!());
cmd.arg("--no-std-lib");
cmd.arg("--no-config-file");
for ide_command in ide_commands {
cmd.arg(ide_command);
}
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}");
println!("Expected output to contain: {expected}");
assert!(output.status.success());
assert!(stdout.contains(expected));
Ok(())
}
#[cfg(test)]
#[track_caller]
pub fn fail_test(input: &str, expected: &str) -> TestResult {
let mut file = NamedTempFile::new()?;
let name = file.path();
let mut cmd = Command::new(cargo_bin!());
cmd.arg("--no-std-lib");
cmd.arg("--no-config-file");
cmd.arg(name);
cmd.env(
"PWD",
std::env::current_dir().expect("Can't get current dir"),
);
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}");
println!("Expected error to contain: {expected}");
assert!(!stderr.is_empty() && stderr.contains(expected));
Ok(())
}