Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 20, 2022
2 parents 0d91096 + 6a2b6fc commit cb26e8d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/CopyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class CopyMojo extends SafeMojo {

/**
* Directory in which .eo files are located.
*
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
Expand All @@ -76,6 +77,7 @@ public final class CopyMojo extends SafeMojo {

/**
* Target directory with resources to be packaged in JAR.
*
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
Expand All @@ -87,6 +89,7 @@ public final class CopyMojo extends SafeMojo {

/**
* The version to use for 0.0.0 replacements.
*
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(property = "eo.version", required = true, defaultValue = "${project.version}")
Expand Down
27 changes: 23 additions & 4 deletions eo-maven-plugin/src/main/java/org/eolang/maven/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.cactoos.Bytes;
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;
Expand All @@ -43,9 +45,6 @@
* Class for saving and loading files.
*
* @since 0.27
* @todo #1105:30min create load function (it has to be able read by path)
* It should be able to load data from file
* We also need to add new unit test
*/
@SuppressWarnings("PMD.TooManyMethods")
public class Home {
Expand All @@ -63,6 +62,7 @@ public class Home {

/**
* Ctor.
*
* @param path Path
*/
Home(final Path path) {
Expand All @@ -71,6 +71,7 @@ public class Home {

/**
* Saving input.
*
* @param input Input
* @param path Path to file
* @throws IOException If fails
Expand Down Expand Up @@ -109,6 +110,7 @@ public void save(final Input input, final Path path) throws IOException {

/**
* Saving string.
*
* @param str String
* @param path Path to file
* @throws IOException If fails
Expand All @@ -119,6 +121,7 @@ public void save(final String str, final Path path) throws IOException {

/**
* Saving text.
*
* @param txt Text
* @param path Path to file
* @throws IOException If fails
Expand All @@ -129,6 +132,7 @@ public void save(final Text txt, final Path path) throws IOException {

/**
* Saving stream.
*
* @param stream Input stream
* @param path Path to file
* @throws IOException If fails
Expand All @@ -139,6 +143,7 @@ public void save(final InputStream stream, final Path path) throws IOException {

/**
* Saving bytes.
*
* @param bytes Byte array
* @param path Path to file
* @throws IOException If fails
Expand All @@ -149,6 +154,7 @@ public void save(final byte[] bytes, final Path path) throws IOException {

/**
* Make relative name from path.
*
* @param file The path of the file or dir
* @return Relative name to CWD
*/
Expand All @@ -167,21 +173,34 @@ public String rel(final Path file) {

/**
* Check if exists.
*
* @param path Path
* @return True if exists
*/
public boolean exists(final Path path) {
return Files.exists(this.path(path));
}

/**
* Load bytes from file by path.
*
* @param path Path to the file
* @return Bytes of file
* @throws IOException if method can't find the file by path or
* if some exception happens during reading the file
*/
public Bytes load(final Path path) throws IOException {
return new BytesOf(Files.readAllBytes(this.path(path)));
}

/**
* Path modification.
*
* @param path Path
* @return Modified path (without bad symbols)
*/
private static Path path(final Path path) {
final byte[] bytes = path.toString().getBytes(StandardCharsets.UTF_8);
return Paths.get(new String(bytes, StandardCharsets.UTF_8));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
*/
package org.eolang.maven;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -58,7 +57,7 @@ void copiesSources(@TempDir final Path temp) throws Exception {
Matchers.is(true)
);
MatcherAssert.assertThat(
new String(Files.readAllBytes(out), StandardCharsets.UTF_8),
new TextOf(new Home().load(out)).asString(),
Matchers.allOf(
Matchers.containsString("+rt foo:"),
Matchers.containsString("0.0.0"),
Expand Down
22 changes: 22 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/HomeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.cactoos.Bytes;
import org.cactoos.text.Randomized;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -105,4 +108,23 @@ void existsInDirWithSpecialSymbolsTest(@TempDir final Path temp) throws IOExcept
Matchers.is(true)
);
}

@Test
void loadBytesFromExistingFile(@TempDir final Path temp) throws IOException {
final Home home = new Home();
final String filename = "foo";
final String content = "bar";
final Path subfolder = temp.resolve("subfolder").resolve(filename);
home.save(content, subfolder);
final Bytes bytes = home.load(subfolder);
final TextOf text = new TextOf(bytes);
MatcherAssert.assertThat(text, Matchers.equalTo(new TextOf(content)));
}

@Test
void loadFromAbsentFile(@TempDir final Path temp) {
final Home home = new Home();
final Path absent = temp.resolve("nonexistent");
Assertions.assertThrows(NoSuchFileException.class, () -> home.load(absent));
}
}

1 comment on commit cb26e8d

@0pdd
Copy link

@0pdd 0pdd commented on cb26e8d Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1105-4c34ccb4 disappeared from eo-maven-plugin/src/main/java/org/eolang/maven/Home.java), that's why I closed #1246. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.