Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Aug 15, 2023
2 parents d3caa91 + e190328 commit 6791cb5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public String hash() {
return this.delegate.get(ForeignTojos.Attribute.HASH.key());
}

/**
* The tojo probed.
* @return The probed.
*/
public String probed() {
return this.delegate.get(ForeignTojos.Attribute.PROBED.key());
}

/**
* Checks if tojo was not already optimized.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ public ForeignTojo add(final ObjectName name) {
return this.add(name.toString());
}

/**
* Find tojo by tojo id.
* @param id The id of the tojo.
* @return The tojo.
* @todo #2331:90min Add unit test for ForeignTojos.find method.
* The test should check that the method returns the tojo with the
* specified id. When the test is ready, remove that puzzle.
*/
public ForeignTojo find(final String id) {
return new ForeignTojo(
this.tojos.value()
.select(tojo -> tojo.get(Attribute.ID.key()).equals(id))
.stream()
.findFirst()
.orElseThrow(
() -> new IllegalArgumentException(
String.format("Tojo '%s' not found", id)
)
)
);
}

/**
* Get the tojos that are not discovered yet.
* @return The tojos.
Expand Down
28 changes: 27 additions & 1 deletion eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.cactoos.text.UncheckedText;
import org.eolang.maven.hash.CommitHash;
import org.eolang.maven.objectionary.Objectionaries;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.tojos.ForeignTojos;
import org.eolang.maven.tojos.PlacedTojos;
import org.eolang.maven.util.Home;
Expand Down Expand Up @@ -445,6 +446,22 @@ Map<String, Path> result() throws IOException {
);
}

/**
* Retrieve the entry of the last program in the eo-foreign.csv file.
* @return Tojo entry.
*/
ForeignTojo programTojo() {
return this.foreignTojos().find(this.tojoId(this.current.get() - 1));
}

/**
* Same as {@link FakeMaven#programTojo()} but for external tojos.
* @return Tojo entry.
*/
ForeignTojo programExternalTojo() {
return this.externalTojos().find(this.tojoId(this.current.get() - 1));
}

/**
* The version of eo-maven-plugin for tests.
* @return Version.
Expand Down Expand Up @@ -494,7 +511,7 @@ private FakeMaven withProgram(final String content) throws IOException {
String.format("foo/x/main%s.eo", FakeMaven.suffix(this.current.get()))
);
this.workspace.save(content, path);
final String object = String.format("foo.x.main%s", FakeMaven.suffix(this.current.get()));
final String object = this.tojoId(this.current.get());
final String scope = this.scope();
final String version = "0.25.0";
final Path source = this.workspace.absolute(path);
Expand Down Expand Up @@ -537,6 +554,15 @@ private String scope() {
return String.valueOf(this.params.getOrDefault("scope", "compile"));
}

/**
* The id of the program in tojos file.
* @param id Number of the program.
* @return String id.
*/
private static String tojoId(final int id) {
return String.format("foo.x.main%s", FakeMaven.suffix(id));
}

/**
* Looks for all declared fields for mojo and its parents.
*
Expand Down
109 changes: 39 additions & 70 deletions eo-maven-plugin/src/test/java/org/eolang/maven/ProbeMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
*/
package org.eolang.maven;

import com.yegor256.tojos.MnCsv;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.Map;
import org.cactoos.io.ResourceOf;
import org.cactoos.map.MapEntry;
Expand Down Expand Up @@ -57,13 +55,7 @@
* @since 0.28.11
* @todo #2302:30min Refactor tests in the class. Looks like there is a lot of
* code duplication among all tests in the class. Need to reduce it somehow.
* @todo #2302:30min Refactor firstEntity method. The "first entity of the
* foreign tojos" looks strange. It looks like we are tying to scan an
* intermediate state which we are trying to read in the middle of the process.
* It lead to a fragile implementation. Could we check the result somehow else?
* Or to skip that check at all?
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@ExtendWith(OnlineCondition.class)
final class ProbeMojoTest {
/**
Expand All @@ -75,14 +67,12 @@ final class ProbeMojoTest {
@ExtendWith(OnlineCondition.class)
void findsProbes(@TempDir final Path temp) throws Exception {
MatcherAssert.assertThat(
ProbeMojoTest.firstEntry(
new FakeMaven(temp)
.with("foreignFormat", "json")
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.foreignPath(),
"probed"
),
new FakeMaven(temp)
.with("foreignFormat", "json")
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.programTojo()
.probed(),
Matchers.equalTo("5")
);
}
Expand All @@ -93,36 +83,31 @@ void findsProbesViaOfflineHashFile(@TempDir final Path temp) throws IOException
new ResourceOf("org/eolang/maven/commits/tags.txt"),
Paths.get("tags.txt")
);
final CommitHash hash = new ChCached(
new ChText(temp.resolve("tags.txt"), "master")
);
MatcherAssert.assertThat(
ProbeMojoTest.firstEntry(
new FakeMaven(temp)
.with("hsh", hash)
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.foreignPath(),
"hash"
),
new FakeMaven(temp)
.with(
"hsh",
new ChCached(
new ChText(temp.resolve("tags.txt"), "master")
)
)
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.programTojo()
.hash(),
Matchers.equalTo("mmmmmmm")
);
}

@Test
void findsProbesViaOfflineHash(@TempDir final Path temp) throws IOException {
final CommitHash hash = new ChCached(
new ChPattern("*.*.*:abcdefg", "1.0.0")
);
MatcherAssert.assertThat(
ProbeMojoTest.firstEntry(
new FakeMaven(temp)
.with("hsh", hash)
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.foreignPath(),
"hash"
),
new FakeMaven(temp)
.with("hsh", new ChPattern("*.*.*:abcdefg", "1.0.0"))
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.programTojo()
.hash(),
Matchers.equalTo("abcdefg")
);
}
Expand All @@ -131,17 +116,17 @@ void findsProbesViaOfflineHash(@TempDir final Path temp) throws IOException {
@ExtendWith(OnlineCondition.class)
void findsProbesInOyRemote(@TempDir final Path temp) throws IOException {
final String tag = "0.28.10";
final CommitHash hash = new ChRemote(tag);
MatcherAssert.assertThat(
ProbeMojoTest.firstEntry(
new FakeMaven(temp)
.with("tag", tag)
.with("objectionaries", new Objectionaries.Fake(new OyRemote(hash)))
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.foreignPath(),
"probed"
),
new FakeMaven(temp)
.with("tag", tag)
.with(
"objectionaries",
new Objectionaries.Fake(new OyRemote(new ChRemote(tag)))
)
.withProgram(ProbeMojoTest.program())
.execute(new FakeMaven.Probe())
.programTojo()
.probed(),
Matchers.equalTo("2")
);
}
Expand All @@ -165,19 +150,13 @@ void findsProbesWithVersionsInOneObjectionary(@TempDir final Path temp) throws I
Matchers.is(true)
);
MatcherAssert.assertThat(
"First entry of tojos after probing should have contained one probed object, but it didn't",
ProbeMojoTest.firstEntry(
maven.externalPath(),
"probed"
),
"Program entry in tojos after probing should contain one probed object",
maven.programExternalTojo().probed(),
Matchers.equalTo("1")
);
MatcherAssert.assertThat(
"First entry of tojos after probing should have contained given hash, but it didn't",
ProbeMojoTest.firstEntry(
maven.externalPath(),
"hash"
),
"Program entry in tojos after probing should contain given hash",
maven.programExternalTojo().hash(),
Matchers.equalTo(hash.value())
);
}
Expand Down Expand Up @@ -221,18 +200,12 @@ void findsProbesWithVersionsInDifferentObjectionaries(@TempDir final Path temp)
);
MatcherAssert.assertThat(
"First entry of tojos after probing should have contained two probed objects, but it didn't",
ProbeMojoTest.firstEntry(
maven.externalPath(),
"probed"
),
maven.programExternalTojo().probed(),
Matchers.equalTo("2")
);
MatcherAssert.assertThat(
"First entry of tojos after probing should have contained given hash, but it didn't",
ProbeMojoTest.firstEntry(
maven.externalPath(),
"hash"
),
maven.programExternalTojo().hash(),
Matchers.equalTo(first.value())
);
}
Expand All @@ -244,8 +217,4 @@ private static String program() {
)
).asString();
}

private static String firstEntry(final Path foreign, final String field) {
return new LinkedList<>(new MnCsv(foreign.toFile()).read()).getFirst().get(field);
}
}

2 comments on commit 6791cb5

@0pdd
Copy link

@0pdd 0pdd commented on 6791cb5 Aug 15, 2023

Choose a reason for hiding this comment

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

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

@0pdd
Copy link

@0pdd 0pdd commented on 6791cb5 Aug 15, 2023

Choose a reason for hiding this comment

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

Puzzle 2331-2e1725a8 discovered in eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java) and submitted as #2392. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.