Skip to content

Commit

Permalink
feat(objectionary#1857): Replace FtCached(hash, cache, origin) with F…
Browse files Browse the repository at this point in the history
…tCached(CacheVersion, cache, origin)

fix(objectionary#1857): add test for uncacheable version

feat(objectionary#1857): add plugin version for cache

feat(objectionary#1857): fix old tests
  • Loading branch information
volodya-lombrozo committed Mar 6, 2023
1 parent f5e655c commit b70420d
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.LinkedList;
import java.util.function.BiConsumer;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -235,6 +236,14 @@ public final class AssembleMojo extends SafeMojo {
@SuppressWarnings("PMD.ImmutableField")
private boolean unrollExitError = true;

/**
* The current version of eo-maven-plugin.
* Maven 3 only.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(defaultValue = "${plugin}", readonly = true)
private PluginDescriptor plugin;

@Override
public void exec() throws IOException {
if (this.central == null) {
Expand Down
17 changes: 13 additions & 4 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -43,6 +44,7 @@
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.eolang.maven.footprint.CacheVersion;
import org.eolang.maven.footprint.Footprint;
import org.eolang.maven.footprint.FtCached;
import org.eolang.maven.footprint.FtDefault;
Expand Down Expand Up @@ -100,6 +102,16 @@ public final class ParseMojo extends SafeMojo implements CompilationStep {
defaultValue = "true")
private boolean failOnError = true;

/**
* The current version of eo-maven-plugin.
* Maven 3 only.
* You can read more about that property
* <a href="https://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html#Supported_Annotations">here</a>.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(defaultValue = "${plugin}", readonly = true)
private PluginDescriptor plugin;

@Override
public void exec() throws IOException {
final int total = new SumOf(
Expand Down Expand Up @@ -157,9 +169,6 @@ private boolean isNotParsed(final Tojo tojo) {
*
* @param tojo The tojo
* @throws IOException If fails
* @todo #1226:30min new FtCached(hash, cache, origin) should be replaced with a new constructor
* that uses the current eo-maven-plugin version - new FtCached(CacheVersion, cache, origin).
* This will allow us to invalidate the cache when the plugin version changes.
*/
@SuppressWarnings({"PMD.AvoidCatchingGenericException", "PMD.ExceptionAsFlowControl"})
private void parse(final Tojo tojo) throws IOException {
Expand All @@ -171,7 +180,7 @@ private void parse(final Tojo tojo) throws IOException {
);
if (tojo.exists(AssembleMojo.ATTR_HASH)) {
footprint = new FtCached(
tojo.get(AssembleMojo.ATTR_HASH),
new CacheVersion(this.plugin.getVersion(), tojo.get(AssembleMojo.ATTR_HASH)),
this.cache.resolve(ParseMojo.PARSED),
footprint
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
* @since 0.30
*/
final class CacheVersion {
public final class CacheVersion {

/**
* Not cacheable versions.
Expand All @@ -66,7 +66,7 @@ final class CacheVersion {
* @param ver Version of the eo-maven-plugin which currently builds a program.
* @param hsh Hash of the objectionary tag.
*/
CacheVersion(final String ver, final String hsh) {
public CacheVersion(final String ver, final String hsh) {
this.version = ver;
this.hash = hsh;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public FtCached(
* @param cache Cache root
* @param origin Origin
*/
FtCached(
public FtCached(
final CacheVersion ver,
final Path cache,
final Footprint origin
Expand Down Expand Up @@ -108,11 +108,15 @@ public String load(final String program, final String ext) throws IOException {
public void save(final String program, final String ext, final Scalar<String> content)
throws IOException {
final String text;
if (this.version.cacheable() && this.isCached(program, ext)) {
text = this.load(program, ext);
if (this.version.cacheable()) {
if (this.isCached(program, ext)) {
text = this.load(program, ext);
} else {
text = new IoChecked<>(content).value();
new Home(this.cache).save(text, this.path(program, ext));
}
} else {
text = new IoChecked<>(content).value();
new Home(this.cache).save(text, this.path(program, ext));
}
this.origin.save(program, ext, () -> text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void assemblesTogether(@TempDir final Path temp) throws Exception {
.with("cache", temp.resolve("cache/parsed"))
.with("skipZeroVersions", true)
.with("central", Central.EMPTY)
.with("plugin", FakeMaven.pluginDescriptor())
.with("ignoreTransitive", true)
.with(
"objectionary",
Expand Down Expand Up @@ -134,6 +135,7 @@ void assemblesNotFailWithFailOnErrorFlag(@TempDir final Path temp) throws Except
.with("failOnError", false)
.with("central", Central.EMPTY)
.with("ignoreTransitive", true)
.with("plugin", FakeMaven.pluginDescriptor())
.with(
"objectionary",
new OyFake()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void makesFullCompilingLifecycleSuccessfully(@TempDir final Path temp) throws IO
.with("skipZeroVersions", true)
.with("central", Central.EMPTY)
.with("ignoreTransitive", true)
.with("plugin", FakeMaven.pluginDescriptor())
.with(
"objectionary",
new OyFake()
Expand Down
158 changes: 90 additions & 68 deletions eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.stream.Stream;
import javax.annotation.concurrent.NotThreadSafe;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.cactoos.Input;
import org.cactoos.text.TextOf;
Expand Down Expand Up @@ -95,46 +96,6 @@ public FakeMaven(final Path workspace) {
this.current = new AtomicInteger(0);
}

/**
* Adds correct 'Hello world' program to workspace.
* @return The same maven instance.
* @throws IOException If method can't save eo program to the workspace.
*/
public FakeMaven withHelloWorld() throws IOException {
return this.withProgram("+package f", "[args] > main", " (stdout \"Hello!\").print");
}

/**
* Adds eo program to a workspace.
* @param input Program as an input.
* @return The same maven instance.
* @throws IOException If method can't save eo program to the workspace.
*/
public FakeMaven withProgram(final Input input) throws IOException {
return this.withProgram(new UncheckedText(new TextOf(input)).asString());
}

/**
* Adds eo program to a workspace.
* @param program Program as a raw string.
* @return The same maven instance.
* @throws IOException If method can't save eo program to the workspace.
*/
public FakeMaven withProgram(final String... program) throws IOException {
return this.withProgram(String.join("\n", program));
}

/**
* Adds eo program to a workspace.
*
* @param path Path to the program
* @return The same maven instance
* @throws IOException If fails
*/
public FakeMaven withProgram(final Path path) throws IOException {
return this.withProgram(new UncheckedText(new TextOf(path)).asString());
}

/**
* Sets parameter for execution.
*
Expand All @@ -147,18 +108,6 @@ public FakeMaven with(final String param, final Object value) {
return this;
}

/**
* Sets tojo attribute.
*
* @param attribute Tojo attribute.
* @param value Attribute value.
* @return The same maven instance.
*/
public FakeMaven withTojoAttribute(final String attribute, final Object value) {
this.attributes.put(attribute, value);
return this;
}

/**
* Executes mojos in the workspace.
* You can use utility classes to run predefined maven pipelines:
Expand All @@ -177,6 +126,17 @@ public FakeMaven execute(final Iterable<Class<? extends AbstractMojo>> mojo)
return this;
}

/**
* Tojo for eo-foreign.* file.
*
* @return TjSmart of the current eo-foreign.file.
*/
public TjSmart foreign() {
return new TjSmart(
Catalogs.INSTANCE.make(this.foreignPath())
);
}

/**
* Executes Mojo in the workspace.
*
Expand Down Expand Up @@ -222,6 +182,7 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
this.params.putIfAbsent("generateDotFiles", true);
this.params.putIfAbsent("generatedDir", this.generatedPath().toFile());
this.params.putIfAbsent("placedFormat", "csv");
this.params.putIfAbsent("plugin", FakeMaven.pluginDescriptor());
final Moja<T> moja = new Moja<>(mojo);
for (final Map.Entry<String, ?> entry : this.allowedParams(mojo).entrySet()) {
moja.with(entry.getKey(), entry.getValue());
Expand All @@ -230,47 +191,88 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
return this;
}

/**
* Adds correct 'Hello world' program to workspace.
* @return The same maven instance.
* @throws IOException If method can't save eo program to the workspace.
*/
FakeMaven withHelloWorld() throws IOException {
return this.withProgram("+package f", "[args] > main", " (stdout \"Hello!\").print");
}

/**
* Adds eo program to a workspace.
* @param input Program as an input.
* @return The same maven instance.
* @throws IOException If method can't save eo program to the workspace.
*/
FakeMaven withProgram(final Input input) throws IOException {
return this.withProgram(new UncheckedText(new TextOf(input)).asString());
}

/**
* Adds eo program to a workspace.
* @param program Program as a raw string.
* @return The same maven instance.
* @throws IOException If method can't save eo program to the workspace.
*/
FakeMaven withProgram(final String... program) throws IOException {
return this.withProgram(String.join("\n", program));
}

/**
* Adds eo program to a workspace.
*
* @param path Path to the program
* @return The same maven instance
* @throws IOException If fails
*/
FakeMaven withProgram(final Path path) throws IOException {
return this.withProgram(new UncheckedText(new TextOf(path)).asString());
}

/**
* Sets tojo attribute.
*
* @param attribute Tojo attribute.
* @param value Attribute value.
* @return The same maven instance.
*/
FakeMaven withTojoAttribute(final String attribute, final Object value) {
this.attributes.put(attribute, value);
return this;
}

/**
* Path to compilation target directory.
* @return Path to target dir.
*/
public Path targetPath() {
Path targetPath() {
return this.workspace.absolute(Paths.get("target"));
}

/**
* Path to generated directory.
* @return Path to generated dir.
*/
public Path generatedPath() {
Path generatedPath() {
return this.targetPath().resolve("generated");
}

/**
* Path to 'eo-foreign.csv' or 'eo-foreign.json' file after all changes.
* @return Path to eo-foreign.* file.
*/
public Path foreignPath() {
Path foreignPath() {
return this.workspace.absolute(Paths.get("eo-foreign.csv"));
}

/**
* Tojo for eo-foreign.* file.
*
* @return TjSmart of the current eo-foreign.file.
*/
public TjSmart foreign() {
return new TjSmart(
Catalogs.INSTANCE.make(this.foreignPath())
);
}

/**
* Tojo for placed.json file.
*
* @return TjSmart of the current placed.json file.
*/
public TjSmart placed() {
TjSmart placed() {
return new TjSmart(
Catalogs.INSTANCE.make(this.workspace.absolute(Paths.get("placed.json")))
);
Expand All @@ -283,7 +285,7 @@ public TjSmart placed() {
* @return Map of "relative UNIX path" (key) - "absolute path" (value).
* @throws IOException If some problem with filesystem have happened.
*/
public Map<String, Path> result() throws IOException {
Map<String, Path> result() throws IOException {
final Path root = this.workspace.absolute(Paths.get(""));
return Files.walk(root).collect(
Collectors.toMap(
Expand All @@ -296,6 +298,14 @@ public Map<String, Path> result() throws IOException {
);
}

/**
* The version of eo-maven-plugin for tests.
* @return Version.
*/
static String pluginVersion() {
return "1.0-TEST";
}

/**
* Suffix for the program name or path.
* - main_1.eo
Expand All @@ -314,6 +324,18 @@ static String suffix(final int index) {
return suffix;
}

/**
* Plugin descriptor with test version.
* @return Plugin descriptor.
*/
static PluginDescriptor pluginDescriptor() {
final PluginDescriptor descriptor = new PluginDescriptor();
descriptor.setGroupId("org.eolang");
descriptor.setArtifactId("eo-maven-plugin");
descriptor.setVersion(FakeMaven.pluginVersion());
return descriptor;
}

/**
* Adds eo program to a workspace.
* @param content EO program content.
Expand Down

0 comments on commit b70420d

Please sign in to comment.