From 7062da671bd0bec8e4996f0c3a115bd1130ece33 Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Sun, 23 Jan 2022 13:42:58 -0600 Subject: [PATCH] feat: added initial assert subcommand --- DEVELOPMENT.md | 8 +++ build.gradle | 7 ++- .../java/me/itzg/helpers/McImageHelper.java | 2 + .../itzg/helpers/assertcmd/AssertCommand.java | 11 ++++ .../me/itzg/helpers/assertcmd/FileExists.java | 32 +++++++++++ .../helpers/assertcmd/FileExistsTest.java | 56 +++++++++++++++++++ 6 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/main/java/me/itzg/helpers/assertcmd/AssertCommand.java create mode 100644 src/main/java/me/itzg/helpers/assertcmd/FileExists.java create mode 100644 src/test/java/me/itzg/helpers/assertcmd/FileExistsTest.java diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 569874ca..5addc70b 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -1,3 +1,11 @@ +## Ad hoc testing + +Beyond the unit tests, ad hoc "integration testing" can be done by running via Gradle passing the intended command-line via `--args`, such as: + +```shell +./gradlew run --args="assert fileExists build.gradle" +``` + ## Creating a new release The Github Actions workflow `publish-release` will take of performing the release build, but the tag needs to be created with the release tasks shown below. diff --git a/build.gradle b/build.gradle index a66ab34d..02824e17 100644 --- a/build.gradle +++ b/build.gradle @@ -137,12 +137,13 @@ dependencies { implementation 'com.jayway.jsonpath:json-path:2.6.0' implementation 'org.apache.maven:maven-artifact:3.8.4' - testImplementation 'org.assertj:assertj-core:3.21.0' + testImplementation 'org.assertj:assertj-core:3.22.0' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' - testImplementation 'org.mockito:mockito-junit-jupiter:4.1.0' + testImplementation 'org.mockito:mockito-junit-jupiter:4.2.0' testImplementation 'org.mock-server:mockserver-junit-jupiter:5.11.2' - testImplementation 'com.github.stefanbirkner:system-lambda:1.2.0' + // https://github.com/stefanbirkner/system-lambda + testImplementation 'com.github.stefanbirkner:system-lambda:1.2.1' annotationProcessor "info.picocli:picocli-codegen:${picocliVersion}" } diff --git a/src/main/java/me/itzg/helpers/McImageHelper.java b/src/main/java/me/itzg/helpers/McImageHelper.java index 7f58ba89..64293e1e 100644 --- a/src/main/java/me/itzg/helpers/McImageHelper.java +++ b/src/main/java/me/itzg/helpers/McImageHelper.java @@ -3,6 +3,7 @@ import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import me.itzg.helpers.asciify.Asciify; +import me.itzg.helpers.assertcmd.AssertCommand; import me.itzg.helpers.get.GetCommand; import me.itzg.helpers.hash.HashCommand; import me.itzg.helpers.patch.PatchCommand; @@ -19,6 +20,7 @@ @Command(name = "mc-image-helper", subcommands = { Asciify.class, + AssertCommand.class, CompareVersionsCommand.class, GetCommand.class, HashCommand.class, diff --git a/src/main/java/me/itzg/helpers/assertcmd/AssertCommand.java b/src/main/java/me/itzg/helpers/assertcmd/AssertCommand.java new file mode 100644 index 00000000..90f4081a --- /dev/null +++ b/src/main/java/me/itzg/helpers/assertcmd/AssertCommand.java @@ -0,0 +1,11 @@ +package me.itzg.helpers.assertcmd; + +import picocli.CommandLine.Command; + +@Command(name = "assert", description = "Provides assertion operators for verifying container setup", + subcommands = { + FileExists.class + } +) +public class AssertCommand { +} diff --git a/src/main/java/me/itzg/helpers/assertcmd/FileExists.java b/src/main/java/me/itzg/helpers/assertcmd/FileExists.java new file mode 100644 index 00000000..5a2ff323 --- /dev/null +++ b/src/main/java/me/itzg/helpers/assertcmd/FileExists.java @@ -0,0 +1,32 @@ +package me.itzg.helpers.assertcmd; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.concurrent.Callable; +import picocli.CommandLine.Command; +import picocli.CommandLine.ExitCode; +import picocli.CommandLine.Parameters; + +@Command(name = "fileExists") +class FileExists implements Callable { + + @Parameters + List paths; + + @Override + public Integer call() throws Exception { + boolean missing = false; + + if (paths != null) { + for (Path path : paths) { + if (!Files.exists(path)) { + System.err.printf("%s does not exist%n", path); + missing = true; + } + } + } + + return missing ? ExitCode.SOFTWARE : ExitCode.OK; + } +} diff --git a/src/test/java/me/itzg/helpers/assertcmd/FileExistsTest.java b/src/test/java/me/itzg/helpers/assertcmd/FileExistsTest.java new file mode 100644 index 00000000..67336eb7 --- /dev/null +++ b/src/test/java/me/itzg/helpers/assertcmd/FileExistsTest.java @@ -0,0 +1,56 @@ +package me.itzg.helpers.assertcmd; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr; +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import picocli.CommandLine; + +class FileExistsTest { + + @Test + void passesWhenAllExist(@TempDir Path tempDir) throws Exception { + final Path file1 = Files.createFile(tempDir.resolve("file1")); + final Path file2 = Files.createFile(tempDir.resolve("file2")); + + final String errOut = tapSystemErr(() -> { + int exitCode = new CommandLine(new FileExists()) + .execute( + file1.toString(), + file2.toString() + ); + + assertThat(exitCode).isEqualTo(0); + }); + + assertThat(errOut).isBlank(); + } + + @Test + void failsWhenSomeMissing(@TempDir Path tempDir) throws Exception { + final Path file1 = Files.createFile(tempDir.resolve("file1")); + final Path file2 = Files.createFile(tempDir.resolve("file2")); + final Path pathA = tempDir.resolve("fileA"); + final Path fileB = tempDir.resolve("fileB"); + + final String errOut = tapSystemErr(() -> { + int exitCode = new CommandLine(new FileExists()) + .execute( + file1.toString(), + pathA.toString(), + file2.toString(), + fileB.toString() + ); + + assertThat(exitCode).isEqualTo(1); + }); + + assertThat(errOut).contains( + "fileA does not exist", + "fileB does not exist" + ); + } +} \ No newline at end of file