|
| 1 | +package fr.ariouz.gkit.test.build.artifact; |
| 2 | + |
| 3 | +import fr.ariouz.gkit.build.BuildException; |
| 4 | +import fr.ariouz.gkit.build.ProcessRunner; |
| 5 | +import fr.ariouz.gkit.build.artifact.ArtifactBuilder; |
| 6 | +import fr.ariouz.gkit.config.models.BuildArtifact; |
| 7 | +import fr.ariouz.gkit.config.models.BuildConfig; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.io.TempDir; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 18 | + |
| 19 | +public class ArtifactBuilderTest { |
| 20 | + |
| 21 | + static class TestArtifactBuilder extends ArtifactBuilder { |
| 22 | + @Override |
| 23 | + protected ProcessRunner getProcessRunner() { |
| 24 | + return new ProcessRunner() { |
| 25 | + @Override |
| 26 | + public void run(String name, List<String> command, File workdir, boolean dryRun) { |
| 27 | + // no-op |
| 28 | + } |
| 29 | + }; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private BuildConfig baseConfig(Path projectDir, String artifactPath) { |
| 34 | + BuildConfig config = new BuildConfig(); |
| 35 | + config.setProjectDir(projectDir.toString()); |
| 36 | + |
| 37 | + BuildArtifact artifact = new BuildArtifact(); |
| 38 | + artifact.setCommand("echo build"); |
| 39 | + artifact.setPath(artifactPath); |
| 40 | + |
| 41 | + config.setArtifact(artifact); |
| 42 | + return config; |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void build_dryRun_doesNotCheckArtifactExists(@TempDir Path tempDir) { |
| 47 | + ArtifactBuilder builder = new TestArtifactBuilder(); |
| 48 | + BuildConfig config = baseConfig(tempDir, "missing.jar"); |
| 49 | + |
| 50 | + File result = builder.build(config, true); |
| 51 | + |
| 52 | + assertThat(result.getPath()).endsWith("missing.jar"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void build_artifactExists_isValid(@TempDir Path tempDir) throws Exception { |
| 57 | + Path artifact = tempDir.resolve("app.jar"); |
| 58 | + Files.createFile(artifact); |
| 59 | + |
| 60 | + ArtifactBuilder builder = new TestArtifactBuilder(); |
| 61 | + BuildConfig config = baseConfig(tempDir, "app.jar"); |
| 62 | + |
| 63 | + File result = builder.build(config, false); |
| 64 | + |
| 65 | + assertThat(result).exists(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void build_artifactMissing_throwsException(@TempDir Path tempDir) { |
| 70 | + ArtifactBuilder builder = new TestArtifactBuilder(); |
| 71 | + BuildConfig config = baseConfig(tempDir, "missing.jar"); |
| 72 | + |
| 73 | + assertThatThrownBy(() -> builder.build(config, false)) |
| 74 | + .isInstanceOf(BuildException.class) |
| 75 | + .hasMessageContaining("artifact not found"); |
| 76 | + } |
| 77 | +} |
0 commit comments