Skip to content

WIP: test: Assert log warnings and errors - #8457

Draft
j-g00da wants to merge 1 commit into
mainfrom
j-g00da/log_asserts
Draft

WIP: test: Assert log warnings and errors#8457
j-g00da wants to merge 1 commit into
mainfrom
j-g00da/log_asserts

Conversation

@j-g00da

@j-g00da j-g00da commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread src/receive_imf/receive_imf_tests.rs Outdated
.unwrap()
.unwrap();

t.log_sink.assert_warn("No from in message");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage example, all logged errors and warnings will cause tests to fail unless we explicitly assert (maybe this should be renamed to expect) that error/warn should be logged.

I will probably expose these methods directly in TestContext and TestContextManager instead of exposing log_sink.

@Hocuri

Hocuri commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

After talking to @link2xt, we found some simplifications that are possible, and one bugfix that is needed:

  • The whole printing-logs-only-when-LogSink-is-dropped logic is historical (from before cargo nextest existed) and not needed anymore. Instead, print_logevent() can be called directly from the loop in fn subscribe. Then, we don't need the collected field.
  • With the PR as-is, calling assert_warn() on one TestContext will find also react to warnings on other TestContexts, because they share the same LogSink. Instead, each TestContext should get its own LogSink. This also is a simplification, because TestContextManager doesn't need a LogSink anymore, then.

@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch 4 times, most recently from 8249945 to 2d7664d Compare July 30, 2026 12:24
Comment thread src/test_utils.rs
Comment on lines +1364 to +1389
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LogAssertType {
Warn,
Error,
}

impl Display for LogAssertType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
Self::Warn => "a warning".to_string(),
Self::Error => "an error".to_string(),
};
write!(f, "{}", str)
}
}

impl Not for LogAssertType {
type Output = Self;

fn not(self) -> Self::Output {
match self {
LogAssertType::Warn => LogAssertType::Error,
LogAssertType::Error => LogAssertType::Warn,
}
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I guess it's just bloat, will change it back to "is_error" bool later.)

Signed-off-by: Jagoda Ślązak <jslazak@jslazak.com>
@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch from 2d7664d to afff615 Compare July 31, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants