Skip to content

Commit

Permalink
Merge branch 'master' of github.com:objectionary/eo
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 14, 2023
2 parents 0330b1c + deae72d commit 81f6ee1
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 108 deletions.
14 changes: 0 additions & 14 deletions eo-maven-plugin/src/main/java/org/eolang/maven/AssembleMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,6 @@ public final class AssembleMojo extends SafeMojo {
@SuppressWarnings("PMD.LongVariable")
private boolean ignoreVersionConflicts;

/**
* Whether we should fail on error.
* @checkstyle MemberNameCheck (11 lines)
* @since 0.23.0
* @todo #2443:90min Remove the following failOnError flag. Now we
* have already got rid from it in {@link OptimizeMojo} and {@link VerifyMojo}.
* We need to make failOnError the behaviour default.
*/
@SuppressWarnings("PMD.ImmutableField")
@Parameter(
property = "eo.failOnError",
defaultValue = "true")
private boolean failOnError = true;

/**
* Whether we should fail on warn.
* @checkstyle MemberNameCheck (10 lines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.cactoos.map.MapOf;
import org.eolang.maven.rust.FFINode;
import org.eolang.maven.rust.Names;
import org.eolang.maven.rust.RustNode;
Expand Down Expand Up @@ -74,7 +76,7 @@ public final class BinarizeParseMojo extends SafeMojo {
* the end of the xmir file. When adding a new language for FFI inserts, you need to add the
* appropriate XSL transformation.
*/
static final Train<Shift> TRAIN = new TrBulk<>(
private static final Train<Shift> TRAIN = new TrBulk<>(
new TrClasspath<>(
new ParsingTrain()
.empty()
Expand All @@ -84,6 +86,21 @@ public final class BinarizeParseMojo extends SafeMojo {
)
).back().back();

/**
* Map that matches ffi insert xpath to building of FFINode.
*/
private static final
Map<String, BiFunction<XML, BinarizeParseMojo, FFINode>> FACTORY = new MapOf<>(
"/program/rusts/rust",
(node, mojo) -> new RustNode(
node,
mojo.names,
mojo.targetDir.toPath().resolve("Lib"),
mojo.eoPortalDir.toPath(),
mojo.generatedDir.toPath().resolve("EOrust").resolve("natives")
)
);

/**
* Target directory.
* @checkstyle MemberNameCheck (7 lines)
Expand Down Expand Up @@ -145,29 +162,16 @@ private XML addFFIs(
* Add ffi node via xsl transformation and return list of them.
* @param input Input xmir.
* @return FFI nodes.
* @todo #2649:90min This method may be more general. We need to get rid from rust dependencies
* in this method, because when adding another type of inserts it will be just copy-paste here.
* First of all, the for-loop must create all kinds of FFI nodes, not only {@link RustNode}. I
* think we can implement it, using something like {@code FFINodeFactory}, that will return
* appropriate FFI node for every XML node from {@code nodes}. Also it will be great to move
* paths to XML FFI insert nodes (such as {@code "/program/rusts/rust"}) from this method to
* a static class field.
* @checkstyle AbbreviationAsWordInNameCheck (8 lines)
*/
private Collection<FFINode> getFFIs(final XML input) {
final List<XML> nodes = this.addFFIs(input).nodes("/program/rusts/rust");
final Collection<FFINode> ret = new ArrayList<>(nodes.size());
for (final XML node : nodes) {
ret.add(
new RustNode(
node,
this.names,
this.targetDir.toPath().resolve("Lib"),
this.eoPortalDir.toPath(),
this.generatedDir.toPath().resolve("EOrust").resolve("natives")
)
);
}
final Collection<FFINode> ret = new ArrayList<>(0);
final XML injected = this.addFFIs(input);
BinarizeParseMojo.FACTORY.forEach(
(xpath, ctor) -> injected
.nodes(xpath)
.forEach(node -> ret.add(ctor.apply(node, this)))
);
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void exec() throws IOException {
* @return Optimization for all tojos.
*/
private Optimization optimization() {
Optimization opt;
final Optimization opt;
if (this.trackOptimizationSteps) {
opt = new OptSpy(
new ParsingTrain(),
Expand All @@ -131,7 +131,6 @@ private Optimization optimization() {
} else {
opt = new OptTrain(new ParsingTrain());
}
opt = new OptTrain(opt, "/org/eolang/parser/fail-on-critical.xsl");
return opt;
}
}
31 changes: 6 additions & 25 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,6 @@ public final class ParseMojo extends SafeMojo {
*/
public static final String PARSED = "parsed";

/**
* Whether we should fail on parsing error.
* @checkstyle MemberNameCheck (7 lines)
* @since 0.23.0
*/
@SuppressWarnings("PMD.ImmutableField")
@Parameter(
property = "eo.failOnError",
defaultValue = "true")
private boolean failOnError = true;

/**
* The current version of eo-maven-plugin.
* Maven 3 only.
Expand Down Expand Up @@ -176,12 +165,12 @@ private void parse(final ForeignTojo tojo) throws IOException {
);
final XML xmir = new XMLDocument(footprint.load(name, "xmir"));
final List<XML> errors = xmir.nodes("/program/errors/error");
final Path target = new Place(name).make(
this.targetDir.toPath().resolve(ParseMojo.DIR),
TranspileMojo.EXT
);
tojo.withXmir(target.toAbsolutePath());
if (errors.isEmpty()) {
final Path target = new Place(name).make(
this.targetDir.toPath().resolve(ParseMojo.DIR),
TranspileMojo.EXT
);
tojo.withXmir(target.toAbsolutePath());
Logger.debug(
this, "Parsed %s to %s",
new Rel(source), new Rel(target)
Expand All @@ -190,18 +179,10 @@ private void parse(final ForeignTojo tojo) throws IOException {
for (final XML error : errors) {
Logger.error(
this,
"Failed to parse '%s:%s': %s (just logging, because of failOnError=false)",
"Failed to parse '%s:%s': %s",
source, error.xpath("@line").get(0), error.xpath("text()").get(0)
);
}
if (this.failOnError) {
throw new IllegalArgumentException(
String.format(
"Failed to parse %s (%d parsing errors)",
source, errors.size()
)
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.eolang.maven;

import com.jcabi.log.Logger;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -103,8 +104,10 @@ void exec() throws IOException {
*/
private Optimization optimization() {
Optimization opt = new OptTrain(
new OptTrain(new TrDefault<>()),
"/org/eolang/parser/fail-on-errors.xsl"
new TrClasspath<>(new TrDefault<>())
.with("/org/eolang/parser/fail-on-errors.xsl")
.with("/org/eolang/parser/fail-on-critical.xsl")
.back()
);
if (this.failOnWarning) {
opt = new OptTrain(opt, "/org/eolang/parser/fail-on-warnings.xsl");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class AssembleMojoTest {
/**
* Invalid eo program for testing.
*/
private static final String[] INVALID_PROGRAM = {
static final String[] INVALID_PROGRAM = {
"+alias stdout org.eolang.io.stdout",
"+home https://github.com/objectionary/eo",
"+package test",
Expand Down Expand Up @@ -215,32 +215,20 @@ void assemblesTogetherWithVersions(@TempDir final Path temp) throws Exception {
}

@Test
void assemblesNotFailWithFailOnErrorFlag(@TempDir final Path temp) throws IOException {
void assemblesNotFailWithFailOnError(@TempDir final Path temp) throws IOException {
final Map<String, Path> result = new FakeMaven(temp)
.withProgram(AssembleMojoTest.INVALID_PROGRAM)
.with("failOnError", false)
.execute(AssembleMojo.class).result();
.execute(new FakeMaven.Optimize())
.result();
MatcherAssert.assertThat(
"Even if the eo program invalid we still have to parse it, but we didn't",
result.get(String.format("target/%s", ParseMojo.DIR)),
new ContainsFiles(String.format("**/main.%s", TranspileMojo.EXT))
);
MatcherAssert.assertThat(
"Since the eo program invalid we shouldn't have optimized it, but we did",
"Even if the eo program invalid we still have to optimize it, but we didn't",
result.get(String.format("target/%s", OptimizeMojo.DIR)),
Matchers.not(new ContainsFiles(String.format("**/main.%s", TranspileMojo.EXT)))
);
}

@Test
void doesNotAssembleIfFailOnErrorFlagIsTrue(@TempDir final Path temp) {
final Class<IllegalStateException> expected = IllegalStateException.class;
Assertions.assertThrows(
expected,
() -> new FakeMaven(temp)
.withProgram(AssembleMojoTest.INVALID_PROGRAM)
.execute(AssembleMojo.class),
String.format("AssembleMojo should have failed with %s, but didn't", expected)
new ContainsFiles(String.format("**/main.%s", TranspileMojo.EXT))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ void discoversDifferentUnversionedObjectsFromDifferentVersionedObjects(@TempDir
void doesNotDiscoverWithVersions(@TempDir final Path tmp) throws IOException {
final FakeMaven maven = new FakeMaven(tmp)
.with("withVersions", false)
.with("failOnError", false)
.withVersionedProgram()
.execute(new FakeMaven.Discover());
final ObjectName seq = new OnVersioned("org.eolang.seq", "6c6269d");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,9 @@ void optimizesConcurrentlyWithLotsOfPrograms(@TempDir final Path temp) throws IO
}

@Test
void failsOnCritical(@TempDir final Path temp) throws IOException {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
void doesNotCrashesOnError(@TempDir final Path temp) throws Exception {
MatcherAssert.assertThat(
new FakeMaven(temp)
.withProgram(
"+package f\n",
"[args] > main",
Expand All @@ -231,6 +230,10 @@ void failsOnCritical(@TempDir final Path temp) throws IOException {
" FALSE > x"
).with("trackOptimizationSteps", true)
.execute(new FakeMaven.Optimize())
.result(),
Matchers.hasKey(
String.format("target/%s/foo/x/main.%s", ParseMojo.DIR, TranspileMojo.EXT)
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,10 @@ void parsesWithCache(@TempDir final Path temp) throws Exception {
}

@Test
void crashesOnInvalidSyntax(@TempDir final Path temp) {
MatcherAssert.assertThat(
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram("something > is wrong here")
.with("failOnError", true)
.execute(ParseMojo.class)
).getCause().getCause().getMessage(),
Matchers.containsString("Failed to parse")
);
}

@Test
void doesNotCrashesWithFailOnError(@TempDir final Path temp) throws Exception {
void doesNotCrashesOnError(@TempDir final Path temp) throws Exception {
MatcherAssert.assertThat(
new FakeMaven(temp)
.withProgram("something < is wrong here")
.with("failOnError", false)
.execute(new FakeMaven.Parse())
.result(),
Matchers.hasKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ final class SafeMojoTest {
@Test
@CaptureLogs
void logsStackTrace(final Logs out, @TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
Assertions.assertDoesNotThrow(
() -> new FakeMaven(temp)
.withProgram("something > is definitely wrong here")
.with("failOnError", true)
.execute(ParseMojo.class)
.execute(new FakeMaven.Parse())
);
MatcherAssert.assertThat(
String.join("\n", out.captured()),
Expand Down
38 changes: 38 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/VerifyMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,44 @@ void failsOptimizationOnError(@TempDir final Path temp) {
);
}

@Test
void failsOptimizationOnCritical(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(
"+package f\n",
"[args] > main",
" seq > @",
" TRUE > x",
" FALSE > x"
).with("trackOptimizationSteps", true)
.execute(new FakeMaven.Verify())
);
}

@Test
void failsParsingOnError(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram("something > is wrong here")
.execute(new FakeMaven.Verify()),
"Program with invalid syntax should have failed, but it didn't"
);
}

@Test
void failsOnInvalidProgram(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(AssembleMojoTest.INVALID_PROGRAM)
.execute(new FakeMaven.Verify()),
"Invalid program with wrong syntax should have failed to assemble, but it didn't"
);
}

@Test
void failsOnWarning(@TempDir final Path temp) throws Exception {
final FakeMaven maven = new FakeMaven(temp)
Expand Down

0 comments on commit 81f6ee1

Please sign in to comment.