|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | +package org.eolang.maven; |
| 6 | + |
| 7 | +import com.yegor256.Mktmp; |
| 8 | +import com.yegor256.MktmpResolver; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.concurrent.atomic.AtomicInteger; |
| 12 | +import org.hamcrest.MatcherAssert; |
| 13 | +import org.hamcrest.Matchers; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for {@link Cache}. |
| 19 | + * @since 0.60 |
| 20 | + */ |
| 21 | +@ExtendWith(MktmpResolver.class) |
| 22 | +final class CacheTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + void compilesSourceAndAddsToCache(@Mktmp final Path temp) throws Exception { |
| 26 | + final var base = temp.resolve("cache"); |
| 27 | + Files.createDirectories(base); |
| 28 | + final var source = temp.resolve("source.eo"); |
| 29 | + Files.writeString(source, "[] > main\n (stdout \"Hello, EO!\") > @\n"); |
| 30 | + final var target = temp.resolve("target.xmir"); |
| 31 | + final var tail = source.getFileName(); |
| 32 | + final String content = "compiled"; |
| 33 | + new Cache(base, p -> content).apply(source, target, tail); |
| 34 | + MatcherAssert.assertThat( |
| 35 | + "Target file must be created from source", |
| 36 | + Files.readString(target), |
| 37 | + Matchers.equalTo(content) |
| 38 | + ); |
| 39 | + MatcherAssert.assertThat( |
| 40 | + "Cache file must be created", |
| 41 | + Files.exists(base.resolve(tail)), |
| 42 | + Matchers.is(true) |
| 43 | + ); |
| 44 | + MatcherAssert.assertThat( |
| 45 | + "Hash file must be created", |
| 46 | + Files.exists(base.resolve(String.format("%s.sha256", tail))), |
| 47 | + Matchers.is(true) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void readsFromCacheWhenUnchanged(@Mktmp final Path temp) throws Exception { |
| 53 | + final var base = temp.resolve("cache-directory"); |
| 54 | + Files.createDirectories(base); |
| 55 | + final var source = temp.resolve("stdin.eo"); |
| 56 | + Files.writeString(source, "[] > main\n (stdout \"Hello, EO!\") > @\n"); |
| 57 | + final var target = temp.resolve("stdin.xmir"); |
| 58 | + final var counter = new AtomicInteger(0); |
| 59 | + final var cache = new Cache( |
| 60 | + base, |
| 61 | + p -> String.format("stdin %d", counter.incrementAndGet()) |
| 62 | + ); |
| 63 | + final Path tail = source.getFileName(); |
| 64 | + cache.apply(source, target, tail); |
| 65 | + MatcherAssert.assertThat( |
| 66 | + "Compilation should happen only once", |
| 67 | + counter.get(), |
| 68 | + Matchers.equalTo(1) |
| 69 | + ); |
| 70 | + cache.apply(source, target, tail); |
| 71 | + MatcherAssert.assertThat( |
| 72 | + "Compilation should not happen again", |
| 73 | + counter.get(), |
| 74 | + Matchers.equalTo(1) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void compilesAgainWhenChanged(@Mktmp final Path temp) throws Exception { |
| 80 | + final var base = temp.resolve("cache-base-dir"); |
| 81 | + Files.createDirectories(base); |
| 82 | + final var source = temp.resolve("stdout.eo"); |
| 83 | + Files.writeString(source, "[] > main\n (stdout \"Hello, EO!\") > @\n"); |
| 84 | + final var target = temp.resolve("stdout.xmir"); |
| 85 | + final var counter = new AtomicInteger(0); |
| 86 | + final var cache = new Cache( |
| 87 | + base, |
| 88 | + p -> String.format("compiled %d", counter.incrementAndGet()) |
| 89 | + ); |
| 90 | + cache.apply(source, target, source.getFileName()); |
| 91 | + MatcherAssert.assertThat( |
| 92 | + "Compilation should happen once", |
| 93 | + counter.get(), |
| 94 | + Matchers.equalTo(1) |
| 95 | + ); |
| 96 | + Files.writeString(source, "[] > main\n (stdout \"Hello, EO! Modified\") > @\n"); |
| 97 | + cache.apply(source, target, source.getFileName()); |
| 98 | + MatcherAssert.assertThat( |
| 99 | + "Compilation should happen again after source change", |
| 100 | + counter.get(), |
| 101 | + Matchers.equalTo(2) |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments