Skip to content

Commit

Permalink
tests: Use CLI test helper from test-helper repository
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 30, 2025
1 parent 5f48404 commit 8af75b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 56 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ build-context = "0.1"
easy-ext = "1"
fs-err = "3"
tempfile = { version = "3", default-features = false }
test-helper = { features = ["cli"], git = "https://github.com/taiki-e/test-helper.git", branch = "main" }

[lints]
workspace = true
Expand Down
69 changes: 13 additions & 56 deletions tests/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
env,
ffi::OsStr,
path::{Path, PathBuf},
process::{Command, ExitStatus},
process::Command,
str,
sync::LazyLock,
};
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Command {
let (_test_project, cur_dir) = test_project(test_model);
let output =
self.current_dir(cur_dir).output().context("could not execute process").unwrap();
AssertOutput(Some(AssertOutputInner {
AssertOutput(Some(test_helper::cli::AssertOutput {
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: String::from_utf8_lossy(&output.stderr)
.lines()
Expand Down Expand Up @@ -141,13 +141,7 @@ impl Command {
}
}

pub(crate) struct AssertOutput(Option<AssertOutputInner>);

struct AssertOutputInner {
stdout: String,
stderr: String,
status: ExitStatus,
}
pub(crate) struct AssertOutput(Option<test_helper::cli::AssertOutput>);

fn replace_command(lines: &str) -> String {
if lines.contains("rustup run") {
Expand All @@ -158,75 +152,38 @@ fn replace_command(lines: &str) -> String {
lines.to_owned()
}
}
fn line_separated(lines: &str) -> impl Iterator<Item = &'_ str> {
lines.lines().map(str::trim).filter(|line| !line.is_empty())
}

impl AssertOutput {
/// Receives a line(`\n`)-separated list of patterns and asserts whether stderr contains each pattern.
/// Receives a line(`\n`)-separated list of patterns and asserts whether stdout contains each pattern.
#[track_caller]
pub(crate) fn stderr_contains(&self, pats: impl AsRef<str>) -> &Self {
pub(crate) fn stdout_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if !output.stderr.contains(pat) {
panic!(
"assertion failed: `self.stderr.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stderr
);
}
}
output.stdout_contains(replace_command(pats.as_ref()));
}
self
}

/// Receives a line(`\n`)-separated list of patterns and asserts whether stdout contains each pattern.
#[track_caller]
pub(crate) fn stderr_not_contains(&self, pats: impl AsRef<str>) -> &Self {
pub(crate) fn stdout_not_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if output.stderr.contains(pat) {
panic!(
"assertion failed: `!self.stderr.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stderr
);
}
}
output.stdout_not_contains(replace_command(pats.as_ref()));
}
self
}

/// Receives a line(`\n`)-separated list of patterns and asserts whether stdout contains each pattern.
/// Receives a line(`\n`)-separated list of patterns and asserts whether stderr contains each pattern.
#[track_caller]
pub(crate) fn stdout_contains(&self, pats: impl AsRef<str>) -> &Self {
pub(crate) fn stderr_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if !output.stdout.contains(pat) {
panic!(
"assertion failed: `self.stdout.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stdout
);
}
}
output.stderr_contains(replace_command(pats.as_ref()));
}
self
}

/// Receives a line(`\n`)-separated list of patterns and asserts whether stdout contains each pattern.
#[track_caller]
pub(crate) fn stdout_not_contains(&self, pats: impl AsRef<str>) -> &Self {
pub(crate) fn stderr_not_contains(&self, pats: impl AsRef<str>) -> &Self {
if let Some(output) = &self.0 {
for pat in line_separated(&replace_command(pats.as_ref())) {
if output.stdout.contains(pat) {
panic!(
"assertion failed: `!self.stdout.contains(..)`:\n\nEXPECTED:\n{0}\n{pat}\n{0}\n\nACTUAL:\n{0}\n{1}\n{0}\n",
"-".repeat(60),
output.stdout
);
}
}
output.stderr_not_contains(replace_command(pats.as_ref()));
}
self
}
Expand Down

0 comments on commit 8af75b8

Please sign in to comment.