From 6664714101c166c90576da6c77685dcb8776a9d4 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 6 Dec 2023 19:15:13 +0500 Subject: [PATCH] #2660: farea added --- eo-maven-plugin/pom.xml | 5 + .../org/eolang/maven/it/SnippetTestCase.java | 223 +++++------------- .../test/resources/META-INF/maven/plugin.xml | 31 --- .../src/test/resources/log4j.properties | 4 +- .../eolang/maven/snippets/all-features.yaml | 14 +- .../org/eolang/maven/snippets/fibo.yaml | 4 +- .../org/eolang/maven/snippets/ifthenelse.yaml | 4 +- .../org/eolang/maven/snippets/parenting.yaml | 4 +- .../org/eolang/maven/snippets/simple.yaml | 3 +- 9 files changed, 77 insertions(+), 215 deletions(-) delete mode 100644 eo-maven-plugin/src/test/resources/META-INF/maven/plugin.xml diff --git a/eo-maven-plugin/pom.xml b/eo-maven-plugin/pom.xml index 408fd498fd..0c085fd30b 100644 --- a/eo-maven-plugin/pom.xml +++ b/eo-maven-plugin/pom.xml @@ -211,6 +211,11 @@ SOFTWARE. commons-io 2.15.1 + + com.yegor256 + farea + 1.0-SNAPSHOT + org.slf4j slf4j-reload4j diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/it/SnippetTestCase.java b/eo-maven-plugin/src/test/java/org/eolang/maven/it/SnippetTestCase.java index 5ff5d52775..ef78402c6a 100755 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/it/SnippetTestCase.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/it/SnippetTestCase.java @@ -23,39 +23,22 @@ */ 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; @@ -63,15 +46,20 @@ /** * Integration test for simple snippets. * - * This test will/may fail if you change something in {@code to-java.xsl} + *

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.

* * @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") @@ -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 map = yaml.load(yml); - final String stdout = SnippetTestCase.run( - this.temp, - new InputOf(String.format("%s\n", map.get("eo"))), - (List) 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) 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 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 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) map.get("out") + ) + ) ); } - return ret; - } + ); } + } diff --git a/eo-maven-plugin/src/test/resources/META-INF/maven/plugin.xml b/eo-maven-plugin/src/test/resources/META-INF/maven/plugin.xml deleted file mode 100644 index 26a6847680..0000000000 --- a/eo-maven-plugin/src/test/resources/META-INF/maven/plugin.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - eo-maven-plugin - EO-to-Java Maven Plugin - org.eolang - eo-maven-plugin - 1.0-SNAPSHOT - diff --git a/eo-maven-plugin/src/test/resources/log4j.properties b/eo-maven-plugin/src/test/resources/log4j.properties index 15ae53fe0c..a350ccab23 100644 --- a/eo-maven-plugin/src/test/resources/log4j.properties +++ b/eo-maven-plugin/src/test/resources/log4j.properties @@ -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 diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/all-features.yaml b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/all-features.yaml index eff6d34c13..ce93a030dd 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/all-features.yaml +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/all-features.yaml @@ -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!" diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/fibo.yaml b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/fibo.yaml index 0851c28eeb..fea20548db 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/fibo.yaml +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/fibo.yaml @@ -1,9 +1,9 @@ -in: "" +skip: true out: - ".*true.*" args: [ "main" ] eo: | - [args...] > main + [args] > main [n] > f if. > @ n.lt 2 diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/ifthenelse.yaml b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/ifthenelse.yaml index 0792c16bdf..80169844f6 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/ifthenelse.yaml +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/ifthenelse.yaml @@ -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 diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/parenting.yaml b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/parenting.yaml index 42cdb51eaf..f5bc38ffd9 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/parenting.yaml +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/parenting.yaml @@ -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 > @ diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/simple.yaml b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/simple.yaml index fbc85b67c5..ef78191ae6 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/simple.yaml +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/snippets/simple.yaml @@ -1,4 +1,3 @@ -in: "" out: - ".*Hello, дорогой!.*" args: ["simple.main"] @@ -6,6 +5,6 @@ eo: | +package simple +alias org.eolang.io.stdout - [args...] > main + [args] > main stdout > @ "Hello, дорогой!"