Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LogRecorder utility #897

Merged
merged 4 commits into from
Jan 17, 2025
Merged

Conversation

strangelookingnerd
Copy link
Contributor

@strangelookingnerd strangelookingnerd commented Jan 17, 2025

When working on jenkinsci/configuration-as-code-plugin#2623 I was looking for a way to make use of LoggerRule without depending on JUnit 4.
My workaround jenkinsci/configuration-as-code-plugin#2623 (comment) does the trick but I think it should be optimized.

This PR extracts the logic from LoggerRule and provides a JUnit-version-agnostic implementation that is more versatile.

I was thinking of adding a JUnit5 extension that is similar to LoggerRule however I do not really see the point.
The only thing that @Rule public LoggerRule rule = new LoggerRule(); does is to have an automated clean-up in the TestCase#after. The way to do this way sleeker in my opinion is to make use of AutoClosable like so:

@Test
void myTestCase() {
  // do stuff that is required for my test...
  try (LogRecorder recorder = new LogRecorder().record("Foo", Level.INFO).capture(100)) {
    // do the stuff I want to record...
    assertThat(recorder, LogRecorder.recorded(equalTo("MyMessage")));
  }
}

See LogRecorderTest for more examples of this.

Ultimately this new utility enables more tests to migrate away from JUnit4.

Testing done

Clean build with mvn clean verify

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

* extract logic from LoggerRule
strangelookingnerd and others added 2 commits January 17, 2025 16:57
Co-authored-by: Tim Jacomb <21194782+timja@users.noreply.github.com>
Co-authored-by: Tim Jacomb <21194782+timja@users.noreply.github.com>
@timja timja merged commit c51eaac into jenkinsci:master Jan 17, 2025
15 checks passed
@strangelookingnerd strangelookingnerd deleted the log_recorder branch January 17, 2025 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants