Skip to content

Commit

Permalink
#2660: farea added
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 6, 2023
1 parent 33a3ce4 commit 6664714
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 215 deletions.
5 changes: 5 additions & 0 deletions eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ SOFTWARE.
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>farea</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
Expand Down
223 changes: 54 additions & 169 deletions eo-maven-plugin/src/test/java/org/eolang/maven/it/SnippetTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,43 @@
*/
package org.eolang.maven.it;

import com.yegor256.Jaxec;
import com.yegor256.Jhome;
import java.io.File;
import com.yegor256.farea.Farea;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.cactoos.Input;
import org.cactoos.io.InputOf;
import org.cactoos.iterable.Mapped;
import org.eolang.jucs.ClasspathSource;
import org.eolang.maven.AssembleMojo;
import org.eolang.maven.DemandMojo;
import org.eolang.maven.FakeMaven;
import org.eolang.maven.OnlineCondition;
import org.eolang.maven.RegisterMojo;
import org.eolang.maven.TranspileMojo;
import org.eolang.maven.hash.ChRemote;
import org.eolang.maven.hash.CommitHash;
import org.eolang.maven.objectionary.Objectionaries;
import org.eolang.maven.objectionary.OyFilesystem;
import org.eolang.maven.util.Walk;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.yaml.snakeyaml.Yaml;

/**
* Integration test for simple snippets.
*
* This test will/may fail if you change something in {@code to-java.xsl}
* <p>This test will/may fail if you change something in {@code to-java.xsl}
* or some other place where Java sources are generated. This happens
* because this test relies on {@code eo-runtime.jar}, which it finds in your local
* Maven repository. This file is supposed to be generated by a previous run
* of Maven, but will not exist at the first run. Thus, when changes are made
* of Maven, but will not exist at the first run. Thus, when changes are made,
* it is recommended to disable this test. Then, when new {@code eo-runtime.jar} is
* released, you enable this test again.
* released to Maven Central, you enable this test again.</p>
*
* @since 0.1
*
* @todo #2660:30min Most of the snippets are disabled now, in
* the "src/test/resources/snippets/*.yaml" because they don't work.
* Hopefully, they will work once a new eo-runtime.jar is released.
* Just wait until it's released and try to enable the tests.
*/
@ExtendWith(OnlineCondition.class)
@SuppressWarnings("JTCOP.RuleAllTestsHaveProductionClass")
Expand All @@ -85,161 +73,58 @@ final class SnippetTestCase {
public Path temp;

/**
* Runs and checks of eo snippets.
*
* @param yml Yaml test case.
* @throws Exception If fails
* Integration test.
* @param yml The YAML
* @throws IOException If fails
*/
@ParameterizedTest
@ExtendWith(OnlineCondition.class)
@SuppressWarnings("unchecked")
@ClasspathSource(value = "org/eolang/maven/snippets/", glob = "**.yaml")
@ExtendWith(RuntimeLibraryExists.class)
void runsAllSnippets(final String yml) throws Exception {
void runsAllSnippets(final String yml) throws IOException {
final Yaml yaml = new Yaml();
final Map<String, Object> map = yaml.load(yml);
final String stdout = SnippetTestCase.run(
this.temp,
new InputOf(String.format("%s\n", map.get("eo"))),
(List<String>) map.get("args"),
map.get("in").toString()
);
MatcherAssert.assertThat(
String.format("'%s' printed something wrong", yml),
stdout,
Matchers.allOf(
new Mapped<>(
ptn -> Matchers.matchesPattern(
Pattern.compile(ptn, Pattern.DOTALL | Pattern.MULTILINE)
),
(Iterable<String>) map.get("out")
)
)
);
}

/**
* Classpath.
* @return Classpath.
*/
static String classpath() {
return String.format(
".%s%s",
File.pathSeparatorChar,
Paths.get(System.getProperty("user.home"))
.resolve(
String.format(
".m2/repository/org/eolang/eo-runtime/%s/eo-runtime-%1$s.jar",
"1.0-SNAPSHOT"
)
)
);
}

/**
* Compile EO to Java and run.
* @param tmp Temp dir
* @param code EO sources
* @param args Command line arguments
* @param stdin The input
* @return Stdout
* @throws Exception If fails
* @checkstyle ParameterNumberCheck (5 lines)
*/
@SuppressWarnings({"unchecked", "PMD.ExcessiveMethodLength"})
private static String run(
final Path tmp,
final Input code,
final List<String> args,
final String stdin
) throws Exception {
final Path src = tmp.resolve("src");
final CommitHash hash = new ChRemote("master");
final FakeMaven maven = new FakeMaven(tmp)
.withProgram(code)
.with("sourcesDir", src.toFile())
.with("objects", Arrays.asList("org.eolang.bool"))
.with("hash", hash)
.with("objectionaries", new Objectionaries.Fake(new OyFilesystem()));
maven.execute(RegisterMojo.class);
maven.execute(DemandMojo.class);
maven.execute(AssembleMojo.class);
maven.execute(TranspileMojo.class);
final Path classes = maven.targetPath().resolve("classes");
SnippetTestCase.compileJava(maven.generatedPath(), classes);
return SnippetTestCase.runJava(args, stdin, classes);
}

/**
* Compile Java sources.
* @param generated Where to find Java sources
* @param classes Where to put compiled classes
*/
private static void compileJava(final Path generated, final Path classes) {
new Jaxec(
new Jhome().javac().toString(),
"-encoding", "utf-8",
new Walk(generated).stream()
.map(Path::toAbsolutePath)
.map(Path::toString)
.collect(Collectors.joining(" ")),
"-d", classes.toString(),
"-cp", SnippetTestCase.classpath()
).withHome(generated).exec();
}

/**
* Run Java.
* @param args Command line arguments
* @param stdin The input
* @param classes Where to find compiled classes
* @return The stdout
* @checkstyle ParameterNumberCheck (5 lines)
*/
private static String runJava(final List<String> args, final String stdin,
final Path classes) {
return new Jaxec()
.with(
new Jhome().java().toString(),
"-Dfile.encoding=UTF-8",
"-Dsun.stdout.encoding=UTF-8",
"-Dsun.stderr.encoding=UTF-8",
"-cp",
SnippetTestCase.classpath(),
"org.eolang.Main"
)
.with(args)
.withHome(classes)
.withStdin(stdin)
.exec();
}

/**
* Checks if runtime library exists.
*
* @since 0.30
*/
public static class RuntimeLibraryExists implements ExecutionCondition {

@Override
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext ctx) {
ConditionEvaluationResult ret;
try {
final String classpath = SnippetTestCase.classpath();
if (Files.exists(Paths.get(classpath))) {
ret = ConditionEvaluationResult.enabled(
String.format("Runtime library '%s' is found successfully", classpath)
);
} else {
ret = ConditionEvaluationResult.disabled("Runtime library is not found");
Assumptions.assumeFalse(map.containsKey("skip"));
new Farea(this.temp).together(
f -> {
final Path sources = Paths.get("../eo-runtime/src/main/eo");
for (final Path src : new Walk(sources).includes(Arrays.asList("**/*.eo"))) {
final Path target = this.temp.resolve("src/main/eo")
.resolve(sources.relativize(src));
target.toFile().getParentFile().mkdirs();
Files.copy(src, target);
}
} catch (final InvalidPathException exception) {
ret = ConditionEvaluationResult.disabled(
"Runtime library can't be found",
exception.getMessage()
f.files()
.file("src/main/eo/main.eo")
.write(String.format("%s\n", map.get("eo")));
f.build()
.plugins()
.appendItself()
.phase("generate-sources")
.goals("register", "assemble", "transpile");
f.build()
.plugins()
.append("org.codehaus.mojo", "exec-maven-plugin", "3.1.1")
.phase("test")
.goals("java")
.configuration()
.set("mainClass", "org.eolang.Main")
.set("arguments", map.get("args"));
f.exec("test");
MatcherAssert.assertThat(
String.format("'%s' printed something wrong", yml),
f.log(),
Matchers.allOf(
new Mapped<>(
ptn -> Matchers.matchesPattern(
Pattern.compile(ptn, Pattern.DOTALL | Pattern.MULTILINE)
),
(Iterable<String>) map.get("out")
)
)
);
}
return ret;
}
);
}

}
31 changes: 0 additions & 31 deletions eo-maven-plugin/src/test/resources/META-INF/maven/plugin.xml

This file was deleted.

4 changes: 3 additions & 1 deletion eo-maven-plugin/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ log4j.appender.CONSOLE.layout=com.jcabi.log.MulticolorLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss} [%p] %c: %m%n

log4j.logger.org.eolang=INFO
log4j.logger.com.jcabi.log.VerboseProcess=INFO
log4j.logger.com.yegor256.Jaxec=DEBUG
log4j.logger.com.yegor256.farea=DEBUG
log4j.logger.com.jcabi.log.VerboseProcess=DEBUG
log4j.logger.org.eolang.parser.Syntax=INFO
log4j.logger.org.eolang.parser.Program=INFO
log4j.logger.org.eolang.maven.SodgMojo=INFO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
in: ""
skip: true
out:
- ".*works!.*"
args: ["foo.main"]
eo: |
+package foo
[args...] > main
[args] > main
seq > a
42
*
42
[x] > foo
if.
if. > @
x.eq 0
error "x is zero"
42
seq > @
QQ.io.stdout
"works!"
*
QQ.io.stdout
"works!"
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
in: ""
skip: true
out:
- ".*true.*"
args: [ "main" ]
eo: |
[args...] > main
[args] > main
[n] > f
if. > @
n.lt 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
in: ""
skip: true
out:
- ".*greater.*"
args: [ "main", "17" ]
eo: |
+alias org.eolang.io.stdout
[args...] > main
[args] > main
[n] > f
if. > @
n.lt 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
in: ""
skip: true
out:
- ".*123.*"
args: [ "main", "123" ]
eo: |
+alias org.eolang.io.stdout
[args...] > main
[args] > main
"The code snippet that checks parent's args" > description
[] > take
^.args.at 0 > @
Expand Down
Loading

0 comments on commit 6664714

Please sign in to comment.