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

Bug/#3708/tests for PhiMojo cache #3776

Merged
merged 10 commits into from
Jan 10, 2025
14 changes: 14 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
/**
* Read XMIR files and translate them to the phi-calculus expression.
* @since 0.34.0
* @todo #3708:60min implement cache.
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
* I assume that files received from dir via Walk should be synced with tojos:
* 1) File is not present in tojos ->
* add file to tojos with column XMIR equals Path of the file.
* 2) File is present in tojos and not phied
* (XMIR is younger than PHI or XMIR doesn't exist or PHI doesn't exist) ->
* add column XMIR equals Path of the file if XMIR not exists.
* When all files are synced we should pass all not phied tojos to the
* {@code FpDefault} reusing existing xmir-to-phi logic
*/
@Mojo(
name = "xmir-to-phi",
Expand All @@ -54,6 +63,11 @@ public final class PhiMojo extends SafeMojo {
*/
public static final String EXT = "phi";

/**
* Subdirectory for parsed cache.
*/
static final String CACHE = "phied";

/**
* The directory where to take xmir files for translation from.
* @checkstyle MemberNameCheck (10 lines)
Expand Down
73 changes: 73 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/PhiMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,29 @@
import com.yegor256.farea.Farea;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import org.cactoos.text.TextOf;
import org.eolang.jucs.ClasspathSource;
import org.eolang.maven.footprint.CachePath;
import org.eolang.maven.footprint.Saved;
import org.eolang.xax.XtSticky;
import org.eolang.xax.XtYaml;
import org.eolang.xax.Xtory;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;

/**
* Test cases for {@link PhiMojo}.
* @since 0.34.0
* @todo #3708:30min Remove @Disabled annotation on
* {@code PhiMojoTest.usesCache()} and {@code PhiMojoTest.invalidatesCache()}
* when cache is implemented, check that tests is valid otherwise fix them.
*/
@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"})
@ExtendWith(MktmpResolver.class)
Expand Down Expand Up @@ -223,4 +230,70 @@ void checksSaltyPhiPacks(final String pack, @Mktmp final Path temp) throws Excep
Matchers.equalTo(xtory.map().get("salty").toString())
);
}

@Test
@Disabled
void usesCache(
@Mktmp final Path temp,
@RandomProgram final String program
) throws Exception {
final Path cache = temp.resolve("cache");
final String hash = "123ZaRiFcHiK321";
final Path cached = new Saved(
"some valid phi from cache",
new CachePath(
cache.resolve(PhiMojo.CACHE),
FakeMaven.pluginVersion(),
hash,
Path.of("foo/x/main.phi")
).get()
).value();
Files.setLastModifiedTime(
cached,
FileTime.fromMillis(System.currentTimeMillis() + 50_000)
);
MatcherAssert.assertThat(
"Phi is not loaded from cache",
new TextOf(
new FakeMaven(temp)
.with("cache", cache.toFile())
.withProgram(program)
.allTojosWithHash(() -> hash)
.execute(new FakeMaven.Phi())
.result()
.get("target/phi/foo/x/main.phi")
),
Matchers.equalTo(new TextOf(Files.readString(cached)))
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
);
}

@Test
@Disabled
void invalidatesCache(
@Mktmp final Path temp,
final @RandomProgram String program
) throws Exception {
final Path cache = temp.resolve("cache");
final String hash = "123ZaRiFcHiK321";
final Path cached = new Saved(
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
"some invalid phi (old) from cache",
new CachePath(
cache.resolve(PhiMojo.CACHE),
FakeMaven.pluginVersion(),
hash,
Path.of("foo/x/main.phi")
).get()
).value();
final long old = cached.toFile().lastModified();
new FakeMaven(temp)
.with("cache", cache.toFile())
.withProgram(program)
.allTojosWithHash(() -> hash)
.execute(new FakeMaven.Phi());
MatcherAssert.assertThat(
"PHI cache not invalidated",
old,
Matchers.lessThan(cached.toFile().lastModified())
);
}
}
Loading