Skip to content

Commit

Permalink
Merge pull request #3295 from maxonfjvipon/fix/#3294/phi-fail-on-error
Browse files Browse the repository at this point in the history
fix(#3294): added `PhiMojo` fails on error flag
  • Loading branch information
yegor256 authored Jul 29, 2024
2 parents e7572b2 + 5d0b2fc commit f565073
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 83 deletions.
33 changes: 20 additions & 13 deletions eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;
import org.cactoos.number.SumOf;
import org.cactoos.text.TextOf;
import org.eolang.maven.util.HmBase;
Expand Down Expand Up @@ -96,6 +97,14 @@ public final class PhiMojo extends SafeMojo {
@SuppressWarnings({"PMD.ImmutableField", "PMD.LongVariable"})
private boolean phiFailOnCritical = true;

/**
* Whether {@link PhiMojo} should fail on errors or not.
* @checkstyle MemberNameCheck (10 lines)
*/
@Parameter(property = "eo.phiFailOnError", required = true, defaultValue = "true")
@SuppressWarnings("PMD.ImmutableField")
private boolean phiFailOnError = true;

/**
* Whether {@link PhiMojo} should skip XMIRs that failed on critical errors.
* @checkstyle MemberNameCheck (10 lines)
Expand Down Expand Up @@ -203,22 +212,20 @@ private Train<Shift> train() {
} else {
train = new TrDefault<>();
}
final Train.Temporary<Shift> dependent;
final List<String> dependent = new ListOf<>(
"/org/eolang/parser/critical-errors/duplicate-names.xsl",
"/org/eolang/maven/phi/incorrect-inners.xsl"
);
if (this.phiFailOnError) {
dependent.add("/org/eolang/parser/fail-on-errors.xsl");
}
if (this.phiFailOnCritical) {
dependent = new TrClasspath<>(
"/org/eolang/parser/fail-on-critical.xsl",
"/org/eolang/maven/phi/to-phi.xsl"
);
} else {
dependent = new TrClasspath<>("/org/eolang/maven/phi/to-phi.xsl");
dependent.add("/org/eolang/parser/fail-on-critical.xsl");
}
dependent.add("/org/eolang/maven/phi/to-phi.xsl");
return new TrJoined<>(
train,
new TrClasspath<>(
"/org/eolang/parser/critical-errors/duplicate-names.xsl",
"/org/eolang/maven/phi/incorrect-inners.xsl"
).back(),
dependent.back()
new TrClasspath<>(dependent.toArray(new String[0])).back()
);
}

Expand All @@ -233,7 +240,7 @@ private static String translated(final Train<Shift> train, final XML xmir)
throws ImpossibleToPhiTranslationException {
final XML translated = new Xsline(train).pass(xmir);
Logger.debug(PhiMojo.class, "XML after translation to phi:\n%s", translated);
final List<String> phi = translated.xpath("phi/text()");
final List<String> phi = translated.xpath("program/phi/text()");
if (phi.isEmpty()) {
throw new ImpossibleToPhiTranslationException(
"Xpath 'phi/text()' is not found in translated XMIR"
Expand Down
98 changes: 51 additions & 47 deletions eo-maven-plugin/src/main/resources/org/eolang/maven/phi/to-phi.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -163,53 +163,57 @@ SOFTWARE.
</xsl:function>
<!-- Program -->
<xsl:template match="program">
<phi>
<xsl:text>{</xsl:text>
<xsl:variable name="tabs" select="2"/>
<xsl:value-of select="eo:eol(1)"/>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol(2)"/>
<xsl:variable name="has-package" select="metas/meta/head[text()='package']"/>
<xsl:variable name="package" select="metas/meta[head[text()='package']]/tail[1]"/>
<xsl:variable name="parts" select="tokenize($package,'\.')"/>
<xsl:variable name="length" select="string-length($package)-string-length(replace($package,'\.',''))"/>
<xsl:choose>
<xsl:when test="$has-package">
<xsl:for-each select="$parts">
<xsl:value-of select="."/>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol($tabs+position())"/>
</xsl:for-each>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs + $length + 1"/>
<xsl:with-param name="package">
<xsl:value-of select="$program"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="$package"/>
</xsl:with-param>
</xsl:apply-templates>
<xsl:for-each select="$parts">
<xsl:value-of select="eo:comma(2, $tabs + $length + 2 - position())"/>
<xsl:value-of select="$lambda"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:text>Package</xsl:text>
<xsl:value-of select="eo:eol($tabs + $length + 1 - position())"/>
<xsl:value-of select="$rb"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs"/>
<xsl:with-param name="package" select="$program"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="eo:eol(1)"/>
<xsl:value-of select="$rb"/>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>}</xsl:text>
</phi>
<program>
<xsl:copy-of select="./sheets"/>
<xsl:copy-of select="./errors"/>
<phi>
<xsl:text>{</xsl:text>
<xsl:variable name="tabs" select="2"/>
<xsl:value-of select="eo:eol(1)"/>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol(2)"/>
<xsl:variable name="has-package" select="metas/meta/head[text()='package']"/>
<xsl:variable name="package" select="metas/meta[head[text()='package']]/tail[1]"/>
<xsl:variable name="parts" select="tokenize($package,'\.')"/>
<xsl:variable name="length" select="string-length($package)-string-length(replace($package,'\.',''))"/>
<xsl:choose>
<xsl:when test="$has-package">
<xsl:for-each select="$parts">
<xsl:value-of select="."/>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol($tabs+position())"/>
</xsl:for-each>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs + $length + 1"/>
<xsl:with-param name="package">
<xsl:value-of select="$program"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="$package"/>
</xsl:with-param>
</xsl:apply-templates>
<xsl:for-each select="$parts">
<xsl:value-of select="eo:comma(2, $tabs + $length + 2 - position())"/>
<xsl:value-of select="$lambda"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:text>Package</xsl:text>
<xsl:value-of select="eo:eol($tabs + $length + 1 - position())"/>
<xsl:value-of select="$rb"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs"/>
<xsl:with-param name="package" select="$program"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="eo:eol(1)"/>
<xsl:value-of select="$rb"/>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>}</xsl:text>
</phi>
</program>
</xsl:template>
<!-- Objects -->
<xsl:template match="objects">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
this.params.putIfAbsent("offline", false);
this.params.putIfAbsent("phiOptimize", false);
this.params.putIfAbsent("phiFailOnCritical", true);
this.params.putIfAbsent("phiFailOnError", true);
this.params.putIfAbsent("phiSkipFailed", false);
this.params.putIfAbsent(
"eoPortalDir",
Expand Down
97 changes: 77 additions & 20 deletions eo-maven-plugin/src/test/java/org/eolang/maven/PhiMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
* @since 0.34.0
*/
final class PhiMojoTest {
/**
* Comment.
*/
private static final String COMMENT =
"# This is the default 64+ symbols comment in front of named abstract object.";

@Test
void createsFiles(@TempDir final Path temp) throws Exception {
MatcherAssert.assertThat(
Expand All @@ -53,7 +59,7 @@ void createsFiles(@TempDir final Path temp) throws Exception {
),
new FakeMaven(temp)
.withProgram(
"# This is the default 64+ symbols comment in front of named abstract object.",
PhiMojoTest.COMMENT,
"[] > cart",
" memory 0 > total",
" [i] > add",
Expand All @@ -68,11 +74,29 @@ void createsFiles(@TempDir final Path temp) throws Exception {

@ParameterizedTest
@ClasspathSource(value = "org/eolang/maven/phi/xmir", glob = "**.xmir")
void convertsXmirsToPhiWithoutErrorsWithoutOptimizations(
void convertsXmirsToPhiWithoutCriticalErrorsWithoutOptimizations(
final String xmir,
@TempDir final Path temp
) throws IOException {
final FakeMaven maven = new FakeMaven(temp)
.with("phiFailOnError", false)
.with("phiOptimize", false);
new HmBase(temp).save(xmir, Paths.get("target/2-optimize/test.xmir"));
Assertions.assertDoesNotThrow(
() -> maven.execute(PhiMojo.class),
BinarizeParseTest.TO_ADD_MESSAGE
);
}

@ParameterizedTest
@ClasspathSource(value = "org/eolang/maven/phi/xmir", glob = "**.xmir")
void convertsXmirsToPhiWithoutCriticalErrorsWithOptimizations(
final String xmir,
@TempDir final Path temp
) throws IOException {
final FakeMaven maven = new FakeMaven(temp).with("phiOptimize", false);
final FakeMaven maven = new FakeMaven(temp)
.with("phiFailOnError", false)
.with("phiOptimize", true);
new HmBase(temp).save(xmir, Paths.get("target/2-optimize/test.xmir"));
Assertions.assertDoesNotThrow(
() -> maven.execute(PhiMojo.class),
Expand All @@ -86,7 +110,7 @@ void doesNotFailOnCritical(@TempDir final Path temp) {
() -> new FakeMaven(temp)
.with("phiFailOnCritical", false)
.withProgram(
"# This is the default 64+ symbols comment in front of named abstract object.",
PhiMojoTest.COMMENT,
"[] > with-duplicates",
" true > x",
" false > x"
Expand All @@ -103,7 +127,7 @@ void skipsFailedOnCriticalError(@TempDir final Path temp) {
.with("phiFailOnCritical", true)
.with("phiSkipFailed", true)
.withProgram(
"# This is the default 64+ symbols comment in front of named abstract object.",
PhiMojoTest.COMMENT,
"[] > with-duplicates",
" true > x",
" false > x"
Expand All @@ -113,6 +137,53 @@ void skipsFailedOnCriticalError(@TempDir final Path temp) {
);
}

@Test
void failsOnError(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(
PhiMojoTest.COMMENT,
"[] > without-name",
" true"
)
.execute(new FakeMaven.Phi()),
"PhiMojo should fail on errors with 'phiFailOnError' = true"
);
}

@Test
void doesNotFailOnError(@TempDir final Path temp) {
Assertions.assertDoesNotThrow(
() -> new FakeMaven(temp)
.with("phiFailOnError", false)
.withProgram(
PhiMojoTest.COMMENT,
"[] > without-name",
" true"
)
.execute(new FakeMaven.Phi()),
"PhiMojo should not fail on errors with 'phiFailOnError' = false"
);
}

@Test
void skipsFailedOnError(@TempDir final Path temp) {
Assertions.assertDoesNotThrow(
() -> new FakeMaven(temp)
.with("phiFailOnCritical", false)
.with("phiFailOnError", true)
.with("phiSkipFailed", true)
.withProgram(
PhiMojoTest.COMMENT,
"[] > without-name",
" true"
)
.execute(new FakeMaven.Phi()),
"PhiMojo should not fail on errors with 'phiSkipFailed' = true"
);
}

@Test
void doesNotSaveSkippedFile(@TempDir final Path temp) throws IOException {
MatcherAssert.assertThat(
Expand All @@ -121,7 +192,7 @@ void doesNotSaveSkippedFile(@TempDir final Path temp) throws IOException {
.with("phiFailOnCritical", true)
.with("phiSkipFailed", true)
.withProgram(
"# This is the default 64+ symbols comment in front of named abstract object.",
PhiMojoTest.COMMENT,
"[] > with-duplicates",
" true > x",
" false > x"
Expand All @@ -136,20 +207,6 @@ void doesNotSaveSkippedFile(@TempDir final Path temp) throws IOException {
);
}

@ParameterizedTest
@ClasspathSource(value = "org/eolang/maven/phi/xmir", glob = "**.xmir")
void convertsXmirsToPhiWithoutErrorsWithOptimizations(
final String xmir,
@TempDir final Path temp
) throws IOException {
final FakeMaven maven = new FakeMaven(temp).with("phiOptimize", true);
new HmBase(temp).save(xmir, Paths.get("target/2-optimize/test.xmir"));
Assertions.assertDoesNotThrow(
() -> maven.execute(PhiMojo.class),
BinarizeParseTest.TO_ADD_MESSAGE
);
}

@ParameterizedTest
@ClasspathSource(value = "org/eolang/maven/phi/yaml", glob = "**.yaml")
void checksPhiPacks(final String pack, @TempDir final Path temp) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ eo: |
# This is the default 64+ symbols comment in front of named abstract object.
[] > main
^.x > x
$.a > a
$.a > b
@.@ > phi
phi: |-
{
main ↦ ⟦
x ↦ ξ.ρ.x,
a ↦ ξ.a,
b ↦ ξ.a,
phi ↦ ξ.φ.φ
Expand Down
2 changes: 1 addition & 1 deletion eo-runtime/src/test/java/org/eolang/PhPackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void findsAttributesConcurrently() throws InterruptedException {
() -> (Runnable) () -> {
try {
latch.await();
basket.add(System.identityHashCode(pckg.take("goto")));
basket.add(System.identityHashCode(pckg.take("go")));
} catch (final InterruptedException exception) {
Thread.currentThread().interrupt();
throw new IllegalStateException(
Expand Down

0 comments on commit f565073

Please sign in to comment.