Skip to content

Commit

Permalink
Merge pull request #3776 from ArtemGet/bug/#3708/tests-phi-to-xmir
Browse files Browse the repository at this point in the history
Bug/#3708/tests for PhiMojo cache
  • Loading branch information
yegor256 authored Jan 10, 2025
2 parents 96a594c + 48a7985 commit 6466088
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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())
);
}
}

1 comment on commit 6466088

@0pdd
Copy link

@0pdd 0pdd commented on 6466088 Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3708-dc6420f7 discovered in eo-maven-plugin/src/test/java/org/eolang/maven/PhiMojoTest.java) and submitted as #3804. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.