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

#2896: Made the test more concrete #2917

Merged
merged 15 commits into from
May 9, 2024
30 changes: 13 additions & 17 deletions eo-maven-plugin/src/test/java/org/eolang/maven/VerifyMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Xsline;
import java.nio.file.Path;
import java.util.Arrays;
import org.cactoos.io.ResourceOf;
import org.eolang.maven.log.CaptureLogs;
import org.eolang.maven.log.Logs;
import org.eolang.maven.util.HmBase;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand All @@ -50,11 +52,6 @@
* /org/eolang/parser/warnings/mandatory-version-meta.xsl and
* /org/eolang/parser/warnings/mandatory-home-meta.xsl.
* After you need fix {@code createRegEx()}.
* @todo #2890:30min Fix this {@link VerifyMojoTest#detectsErrorsSuccessfully}
* flaky test and enable it. It failed in ci
* <a href="https://github.com/objectionary/eo/actions/runs/8041230784/job/21960239171?pr=2892">here</a>
* without providing the regex and message. Also may be it would be cleaner to fix
* error Assertion since now it is hard to get why it failed.
*/
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
final class VerifyMojoTest {
Expand All @@ -70,7 +67,6 @@ void doesNotFailWithNoErrorsAndWarnings(@TempDir final Path temp) {
}

@Test
@Disabled
@CaptureLogs
void detectsErrorsSuccessfully(
@TempDir final Path temp,
Expand All @@ -88,18 +84,18 @@ void detectsErrorsSuccessfully(
.execute(new FakeMaven.Verify()),
"Program with noname attributes should have failed or error, but it didn't"
);
final String message = this.getMessage(out, "Errors identified");
Assertions.assertTrue(
message.matches(this.createRegEx(temp, "Errors identified")),
"Errors message should have program name and error line number"
MatcherAssert.assertThat(
levBagryansky marked this conversation as resolved.
Show resolved Hide resolved
"Errors message should have program name and error line number",
this.getMessage(out, "Errors identified", temp.toString()),
Matchers.matchesPattern(this.createRegEx(temp, "Errors identified"))
);
}

@Test
@CaptureLogs
void detectsCriticalErrorsSuccessfully(
@TempDir final Path temp,
final Logs out) throws Exception {
final Logs out) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
Expand All @@ -112,7 +108,7 @@ void detectsCriticalErrorsSuccessfully(
.execute(new FakeMaven.Verify()),
"Wrong program should have failed or error, but it didn't"
);
final String message = this.getMessage(out, "Critical error identified");
final String message = this.getMessage(out, "Critical error identified", temp.toString());
Assertions.assertTrue(
message.matches(this.createRegEx(temp, "Critical error identified")),
"Critical error message should have program name and error line number"
Expand All @@ -139,7 +135,7 @@ void detectsWarningWithCorrespondingFlag(
.execute(new FakeMaven.Verify()),
"Program with sparse decorated object should have failed on warning, but it didn't"
);
final String message = this.getMessage(out, "Warnings identified");
final String message = this.getMessage(out, "Warnings identified", temp.toString());
Assertions.assertTrue(
message.matches(this.createRegEx(temp, "Warnings identified")),
"Warnings message should have program name and error line number"
Expand Down Expand Up @@ -267,12 +263,12 @@ private static void applyXsl(final String xsl, final Path xml) throws Exception
/**
* Parse the error message to program name and error line number for checking.
* @param logs Logs logs
* @param error String needed error message
* @param parts String needed error message
*/
private String getMessage(final Logs logs, final String error) {
private String getMessage(final Logs logs, final String... parts) {
return String.valueOf(logs.captured().stream()
.filter(
log -> log.contains(error)
log -> Arrays.stream(parts).allMatch(log::contains)
).findFirst()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@

/**
* Captured logs annotation for tests.
* @todo #2896:90min Make '@CaptureLogs' thread-safe.
* 'Logs' should contain only messages related to the test.
* Currently, '@CaptureLogs' appends all messages that
* were logged via 'Logger' to 'Logs', so messages from
* other tests (run in parallel) are also included, which causes
* problems when tests are run in parallel.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
Expand Down
Loading