Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(#2227): Refactor ForeignTojos Attribute #2233

Merged
merged 6 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private Collection<ForeignTojo> select(final Predicate<? super Tojo> filter) {
/**
* Foreign tojo attributes.
*/
public enum Attribute {
enum Attribute {

/**
* Tojo id.
Expand Down Expand Up @@ -293,11 +293,6 @@ public enum Attribute {
*/
SCOPE("scope"),

/**
* Transpiled.
*/
TRANSPILED("transpiled"),

/**
* Hash.
*/
Expand Down Expand Up @@ -325,7 +320,7 @@ public enum Attribute {
* Get the attribute name.
* @return The attribute name.
*/
public String key() {
String key() {
return this.key;
}
}
Expand Down
39 changes: 5 additions & 34 deletions eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.eolang.maven;

import com.yegor256.tojos.TjSmart;
import com.yegor256.tojos.Tojo;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -54,6 +53,7 @@
import org.cactoos.Input;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.eolang.maven.hash.CommitHash;
import org.eolang.maven.tojos.ForeignTojos;
import org.eolang.maven.tojos.PlacedTojos;
import org.eolang.maven.util.Home;
Expand Down Expand Up @@ -82,11 +82,6 @@ public final class FakeMaven {
*/
private final Map<String, Object> params;

/**
* Attributes for eo.foreign.* and eo.external.*.
*/
private final Map<ForeignTojos.Attribute, Object> attributes;

/**
* Current program number.
* We can save several programs in workspace and each program has it's own number
Expand All @@ -102,7 +97,6 @@ public final class FakeMaven {
public FakeMaven(final Path workspace) {
this.workspace = new Home(workspace);
this.params = new HashMap<>();
this.attributes = new HashMap<>();
this.current = new AtomicInteger(0);
}

Expand Down Expand Up @@ -156,8 +150,6 @@ public TjSmart foreign() {
* @throws java.io.IOException If some problem with filesystem have happened.
*/
public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IOException {
this.fillUp(this.foreign().select(all -> true));
this.fillUp(this.external().select(all -> true));
this.params.putIfAbsent("targetDir", this.targetPath().toFile());
this.params.putIfAbsent("foreign", this.foreignPath().toFile());
this.params.putIfAbsent("external", this.externalPath().toFile());
Expand Down Expand Up @@ -301,14 +293,12 @@ FakeMaven withProgram(final Path path) throws IOException {
}

/**
* Sets tojo attribute.
*
* @param attribute Tojo attribute.
* @param value Attribute value.
* Specify hash for all foreign tojos.
* @param hash Commit hash
* @return The same maven instance.
*/
FakeMaven withTojoAttribute(final ForeignTojos.Attribute attribute, final Object value) {
this.attributes.put(attribute, value);
FakeMaven allTojosWithHash(final CommitHash hash) {
this.foreignTojos().all().forEach(tojo -> tojo.withHash(hash));
return this;
}

Expand Down Expand Up @@ -467,25 +457,6 @@ private Path externalPath() {
return this.workspace.absolute(Paths.get("eo-external.csv"));
}

/**
* Fill up given tojos by the attributes.
* @param tojos Tojos to fill up.
* @todo #1602:30min Move the method to ForeignTojos if possible.
* Let's treat ForeignTojos as an object (not as a collection of data)
* and give it a chance to fulfill itself. It knows better how to do so.
* ForeignTojo in current implementation does not have method set() so
* we either need to implement it or just stay with the method here in
* FakeMaven class.
*/
private void fillUp(final List<Tojo> tojos) {
for (final Tojo tojo : tojos) {
for (final Map.Entry<ForeignTojos.Attribute, Object> entry
: this.attributes.entrySet()) {
tojo.set(entry.getKey().key(), entry.getValue());
}
}
}

/**
* Parse full pipeline.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.eolang.jucs.ClasspathSource;
import org.eolang.maven.tojos.ForeignTojos;
import org.eolang.maven.util.Home;
import org.eolang.parser.CheckPack;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -138,7 +137,7 @@ void getsAlreadyOptimizedResultsFromCache(@TempDir final Path temp) throws Excep
new FakeMaven(temp)
.withHelloWorld()
.with("cache", cache)
.withTojoAttribute(ForeignTojos.Attribute.HASH, hash)
.allTojosWithHash(() -> hash)
.execute(new FakeMaven.Optimize());
MatcherAssert.assertThat(
new XMLDocument(
Expand All @@ -163,7 +162,7 @@ void savesOptimizedResultsToCache(@TempDir final Path temp) throws IOException {
new FakeMaven(temp)
.withHelloWorld()
.with("cache", cache)
.withTojoAttribute(ForeignTojos.Attribute.HASH, hash)
.allTojosWithHash(() -> hash)
.execute(new FakeMaven.Optimize());
MatcherAssert.assertThat(
cache.resolve(OptimizeMojo.OPTIMIZED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.eolang.maven.footprint.FtDefault;
import org.eolang.maven.hash.ChNarrow;
import org.eolang.maven.hash.ChRemote;
import org.eolang.maven.tojos.ForeignTojos;
import org.eolang.maven.hash.CommitHash;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -84,22 +84,22 @@ void failsOnTimeout(@TempDir final Path temp) {
@Test
@ExtendWith(OnlineCondition.class)
void parsesWithCache(@TempDir final Path temp) throws Exception {
final FakeMaven maven = new FakeMaven(temp);
final Path cache = temp.resolve("cache");
final FakeMaven maven = new FakeMaven(temp)
.withProgram("invalid content")
.with("cache", cache);
final String expected = new UncheckedText(
new TextOf(new ResourceOf("org/eolang/maven/main.xmir"))
).asString();
final String hash = new ChNarrow(new ChRemote("0.25.0")).value();
final CommitHash hash = new ChNarrow(new ChRemote("0.25.0"));
volodya-lombrozo marked this conversation as resolved.
Show resolved Hide resolved
new FtCached(
new CacheVersion(FakeMaven.pluginVersion(), hash),
new CacheVersion(FakeMaven.pluginVersion(), hash.value()),
cache.resolve(ParseMojo.PARSED),
new FtDefault(maven.targetPath())
).save("foo.x.main", "xmir", () -> expected);
MatcherAssert.assertThat(
volodya-lombrozo marked this conversation as resolved.
Show resolved Hide resolved
new TextOf(
maven.withProgram("invalid content")
.withTojoAttribute(ForeignTojos.Attribute.HASH, hash)
.with("cache", cache)
maven.allTojosWithHash(hash)
.execute(new FakeMaven.Parse())
.result()
.get(String.format("target/%s/foo/x/main.%s", ParseMojo.DIR, TranspileMojo.EXT))
Expand Down
Loading