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
5 changes: 5 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 @@ -54,6 +54,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
74 changes: 74 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 @@ -28,24 +28,32 @@
import com.yegor256.MktmpResolver;
import com.yegor256.WeAreOnline;
import com.yegor256.farea.Farea;
import java.io.File;
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 +231,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")
).asString(),
Matchers.equalTo(new TextOf(cached).asString())
);
}

@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 File cached = new Saved(
"some invalid phi (old) from cache",
new CachePath(
cache.resolve(PhiMojo.CACHE),
FakeMaven.pluginVersion(),
hash,
Path.of("foo/x/main.phi")
).get()
).value().toFile();
final long old = cached.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.lastModified())
);
}
}
Loading