Skip to content

Commit

Permalink
feat(objectionary#2331): Remove firstEntity method and add utility me…
Browse files Browse the repository at this point in the history
…thods to FakeMaven and ForeignTojos
  • Loading branch information
volodya-lombrozo committed Aug 14, 2023
1 parent 1493413 commit 02bf288
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 63 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,25 @@ 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.
*/
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
23 changes: 22 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 All @@ -512,6 +529,10 @@ private FakeMaven withProgram(final String content) throws IOException {
return this;
}

private String tojoId(final int id) {
return String.format("foo.x.main%s", FakeMaven.suffix(id));
}

/**
* Ensures the map of allowed params for the Mojo.
*
Expand Down
100 changes: 38 additions & 62 deletions eo-maven-plugin/src/test/java/org/eolang/maven/ProbeMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,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 @@ -94,36 +92,30 @@ 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 @@ -132,17 +124,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 @@ -166,19 +158,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 @@ -222,18 +208,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 @@ -245,8 +225,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);
}
}

0 comments on commit 02bf288

Please sign in to comment.