diff --git a/.github/workflows/simian.yml b/.github/workflows/simian.yml index 216adc49cb..65bf23dc74 100644 --- a/.github/workflows/simian.yml +++ b/.github/workflows/simian.yml @@ -22,4 +22,4 @@ jobs: distribution: 'temurin' java-version: 17 - run: wget --quiet http://public.yegor256.com/simian.jar -O /tmp/simian.jar - - run: java -jar /tmp/simian.jar -threshold=19 "-excludes=**/gen" "-excludes=**/it" "**/*.java" + - run: java -jar /tmp/simian.jar -threshold=15 "-excludes=**/gen" "-excludes=**/it" "**/*.java" diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmBase.java b/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmBase.java index 8f717cadd8..0247154a71 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmBase.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmBase.java @@ -33,7 +33,6 @@ import org.cactoos.Input; import org.cactoos.Text; import org.cactoos.bytes.BytesOf; -import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; import org.cactoos.scalar.IoChecked; @@ -51,6 +50,11 @@ public final class HmBase implements Home { */ private final Path cwd; + /** + * Home with "save" functionality. + */ + private final Home origin; + /** * Ctor. * @@ -63,63 +67,68 @@ public HmBase(final File file) { /** * Ctor. * - * @param path Path + * @param pth Path */ - public HmBase(final Path path) { - this.cwd = path; + public HmBase(final Path pth) { + this.cwd = pth; + this.origin = new HmSave( + (input, path) -> { + final Path target = this.absolute(this.onlyRelative(path)); + if (target.toFile().getParentFile().mkdirs()) { + Logger.debug( + this, "Directory created: %s", + new Rel(target.getParent()) + ); + } + try { + final long bytes = new IoChecked<>( + new LengthOf( + new TeeInput( + input, + new OutputTo(target) + ) + ) + ).value(); + Logger.debug( + HmBase.class, "File %s saved (%d bytes)", + target, bytes + ); + } catch (final IOException ex) { + throw new IOException( + String.format( + "Failed while trying to save to %s", + target + ), + ex + ); + } + } + ); } @Override public void save(final String str, final Path path) throws IOException { - this.save(new InputOf(str), path); + this.origin.save(str, path); } @Override public void save(final Text txt, final Path path) throws IOException { - this.save(new InputOf(txt), path); + this.origin.save(txt, path); } @Override - public void save(final InputStream stream, final Path path) throws IOException { - this.save(new InputOf(stream), path); + public void save(final InputStream stream, final Path path) throws IOException { + this.origin.save(stream, path); } @Override - public void save(final byte[] bytes, final Path path) throws IOException { - this.save(new InputOf(bytes), path); + public void save(final byte[] bytes, final Path path) throws IOException { + this.origin.save(bytes, path); } @Override public void save(final Input input, final Path path) throws IOException { - final Path target = this.absolute(this.onlyRelative(path)); - if (target.toFile().getParentFile().mkdirs()) { - Logger.debug( - this, "Directory created: %s", - new Rel(target.getParent()) - ); - } - try { - final long bytes = new IoChecked<>( - new LengthOf( - new TeeInput( - input, - new OutputTo(target) - ) - ) - ).value(); - Logger.debug( - HmBase.class, "File %s saved (%d bytes)", - target, bytes - ); - } catch (final IOException ex) { - throw new IOException( - String.format( - "Failed while trying to save to %s", - target - ), - ex - ); - } + this.origin.save(input, path); } @Override diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmOptional.java b/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmOptional.java index 96ecf8b251..b1c567a706 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmOptional.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmOptional.java @@ -30,7 +30,6 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Text; -import org.cactoos.io.InputOf; /** * Location for files that saves optionally. @@ -43,6 +42,11 @@ public final class HmOptional implements Home { */ private final Home origin; + /** + * Home with "save" functionality. + */ + private final Home sve; + /** * Rewrite files or not. */ @@ -57,36 +61,41 @@ public final class HmOptional implements Home { public HmOptional(final Home home, final boolean rwte) { this.origin = home; this.rewrite = rwte; + this.sve = new HmSave( + (input, path) -> { + final Path target = this.absolute(this.onlyRelative(path)); + if (!target.toFile().exists() || this.rewrite) { + this.origin.save(input, path); + } else { + Logger.info(this, "Rewriting of the %s file was skipped", target); + } + } + ); } @Override public void save(final String str, final Path path) throws IOException { - this.save(new InputOf(str), path); + this.sve.save(str, path); } @Override public void save(final Text txt, final Path path) throws IOException { - this.save(new InputOf(txt), path); + this.sve.save(txt, path); } @Override - public void save(final InputStream stream, final Path path) throws IOException { - this.save(new InputOf(stream), path); + public void save(final InputStream stream, final Path path) throws IOException { + this.sve.save(stream, path); } @Override - public void save(final byte[] bytes, final Path path) throws IOException { - this.save(new InputOf(bytes), path); + public void save(final byte[] bytes, final Path path) throws IOException { + this.sve.save(bytes, path); } @Override public void save(final Input input, final Path path) throws IOException { - final Path target = this.absolute(this.onlyRelative(path)); - if (!target.toFile().exists() || this.rewrite) { - this.origin.save(input, path); - } else { - Logger.info(this, "Rewriting of the %s file was skipped", target); - } + this.sve.save(input, path); } @Override diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmSave.java b/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmSave.java new file mode 100644 index 0000000000..1e576ad3c9 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/util/HmSave.java @@ -0,0 +1,100 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven.util; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import org.cactoos.BiProc; +import org.cactoos.Bytes; +import org.cactoos.Input; +import org.cactoos.Text; +import org.cactoos.io.InputOf; +import org.cactoos.proc.IoCheckedBiProc; + +/** + * Home that defines the logic of saving different types of data to files. + * + * @since 0.37.0 + */ +public final class HmSave implements Home { + /** + * BiProc with two arguments for saving {@link Input} from first argument to file from second. + */ + private final IoCheckedBiProc sve; + + /** + * Ctor. + * + * @param save BiProc for saving {@link Input} to file. + */ + public HmSave(final BiProc save) { + this.sve = new IoCheckedBiProc<>(save); + } + + @Override + public void save(final String str, final Path path) throws IOException { + this.save(new InputOf(str), path); + } + + @Override + public void save(final Text txt, final Path path) throws IOException { + this.save(new InputOf(txt), path); + } + + @Override + public void save(final InputStream stream, final Path path) throws IOException { + this.save(new InputOf(stream), path); + } + + @Override + public void save(final byte[] bytes, final Path path) throws IOException { + this.save(new InputOf(bytes), path); + } + + @Override + public void save(final Input input, final Path path) throws IOException { + this.sve.exec(input, path); + } + + @Override + public boolean exists(final Path path) { + throw new IllegalStateException("Should never happen"); + } + + @Override + public Bytes load(final Path path) throws IOException { + throw new IllegalStateException("Should never happen"); + } + + @Override + public Path absolute(final Path path) { + throw new IllegalStateException("Should never happen"); + } + + @Override + public Path onlyRelative(final Path path) { + throw new IllegalStateException("Should never happen"); + } +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceMojoTest.java index 8acefe97cb..8f22250651 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceMojoTest.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.stream.Stream; import org.cactoos.text.TextOf; import org.eolang.maven.tojos.PlacedTojo; import org.eolang.maven.tojos.PlacedTojos; @@ -41,6 +42,9 @@ import org.hamcrest.io.FileMatchers; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link UnplaceMojo}. @@ -129,74 +133,46 @@ void keepsJarBecauseItIsStillInUse(@TempDir final Path temp) throws IOException ); } - @Test - void unplacesWithKeepAndRemoveBinariesParamTogether(@TempDir final Path temp) throws Exception { - final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp)); - final Map res = new FakeMaven(temp) - .with("placed", placed.toFile()) - .with("keepBinaries", UnplaceMojoTest.GLOB_PATTERN) - .with("removeBinaries", UnplaceMojoTest.GLOB_PATTERN) - .execute(UnplaceMojo.class) - .result(); - MatcherAssert.assertThat( - BinarizeParseTest.TO_ADD_MESSAGE, - res.values().stream().noneMatch(UnplaceMojoTest::isClass), - Matchers.is(true) - ); - MatcherAssert.assertThat( - BinarizeParseTest.TO_ADD_MESSAGE, - new TextOf(res.get(placed.getFileName().toString())).asString(), - Matchers.allOf( - Matchers.not(Matchers.containsString("false")), - Matchers.containsString("true") - ) - ); - } - - @Test - void unplacesWithRemoveBinariesParam(@TempDir final Path temp) throws Exception { - final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp)); - final Map res = new FakeMaven(temp) - .with("placed", placed.toFile()) - .with("removeBinaries", UnplaceMojoTest.GLOB_PATTERN) - .execute(UnplaceMojo.class) - .result(); - MatcherAssert.assertThat( - BinarizeParseTest.TO_ADD_MESSAGE, - res.values().stream().noneMatch(UnplaceMojoTest::isClass), - Matchers.is(true) - ); - MatcherAssert.assertThat( - BinarizeParseTest.TO_ADD_MESSAGE, - new TextOf(res.get(placed.getFileName().toString())).asString(), - Matchers.allOf( - Matchers.not(Matchers.containsString("false")), - Matchers.containsString("true") - ) - ); - } - - @Test - void unplacesWithKeepBinariesParam(@TempDir final Path temp) throws Exception { + @ParameterizedTest + @MethodSource("testArgsProvider") + void unplacesWithKeepOrRemoveBinariesParam(final String[] params, @TempDir final Path temp) + throws Exception { final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp)); - final Map res = new FakeMaven(temp) - .with("placed", placed.toFile()) - .with("keepBinaries", UnplaceMojoTest.GLOB_PATTERN) - .execute(UnplaceMojo.class) - .result(); - MatcherAssert.assertThat( - BinarizeParseTest.TO_ADD_MESSAGE, - res.values().stream().anyMatch(UnplaceMojoTest::isClass), - Matchers.is(true) - ); - MatcherAssert.assertThat( - BinarizeParseTest.TO_ADD_MESSAGE, - new TextOf(res.get(placed.getFileName().toString())).asString(), - Matchers.allOf( - Matchers.containsString("false"), - Matchers.not(Matchers.containsString("true")) - ) - ); + final FakeMaven maven = new FakeMaven(temp) + .with("placed", placed.toFile()); + for (final String param : params) { + maven.with(param, UnplaceMojoTest.GLOB_PATTERN); + } + final Map res = maven.execute(UnplaceMojo.class).result(); + if (params.length == 1 && "keepBinaries".equals(params[0])) { + MatcherAssert.assertThat( + BinarizeParseTest.TO_ADD_MESSAGE, + res.values().stream().anyMatch(UnplaceMojoTest::isClass), + Matchers.is(true) + ); + MatcherAssert.assertThat( + BinarizeParseTest.TO_ADD_MESSAGE, + new TextOf(res.get(placed.getFileName().toString())).asString(), + Matchers.allOf( + Matchers.containsString("false"), + Matchers.not(Matchers.containsString("true")) + ) + ); + } else { + MatcherAssert.assertThat( + BinarizeParseTest.TO_ADD_MESSAGE, + res.values().stream().noneMatch(UnplaceMojoTest::isClass), + Matchers.is(true) + ); + MatcherAssert.assertThat( + BinarizeParseTest.TO_ADD_MESSAGE, + new TextOf(res.get(placed.getFileName().toString())).asString(), + Matchers.allOf( + Matchers.not(Matchers.containsString("false")), + Matchers.containsString("true") + ) + ); + } } @Test @@ -307,5 +283,19 @@ private static Path clazz(final Path temp) throws IOException { private static boolean isClass(final Path path) { return path.toString().endsWith(".class"); } + + /** + * Input arguments for unit tests. + * + * @return Stream of arguments. + */ + @SuppressWarnings("PMD.UnusedPrivateMethod") + private static Stream testArgsProvider() { + return Stream.of( + Arguments.of((Object) new String[]{"keepBinaries"}), + Arguments.of((Object) new String[]{"removeBinaries"}), + Arguments.of((Object) new String[]{"keepBinaries", "removeBinaries"}) + ); + } } diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java index 0134635dea..74fd0d0e7b 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java @@ -89,26 +89,6 @@ public void usesCatcherOutput() { ); } - @Test - public void printsCatcherOutput() { - final Phi body = new PhWith( - new PhWith( - new PhWith( - new EOtry(Phi.Φ), - 0, new Broken(Phi.Φ) - ), - 1, new Catcher(Phi.Φ) - ), - 2, - new Data.ToPhi(true) - ); - MatcherAssert.assertThat( - AtCompositeTest.TO_ADD_MESSAGE, - new Dataized(body).take(String.class), - Matchers.containsString("it is broken") - ); - } - @Test public void worksWithoutException() { MatcherAssert.assertThat(