Command test context

The local and docker command contexts allow to run commands and scrips on the machine executing the Junit tests and verify the outcomes.

Run command or script

package solidblocks.test.gradle.command

import de.solidblocks.infra.test.SolidblocksTest
import de.solidblocks.infra.test.SolidblocksTestContext
import de.solidblocks.infra.test.assertions.shouldHaveExitCode
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(SolidblocksTest::class)
class LocalCommandContext {
    @Test
    fun localCommandContext(testContext: SolidblocksTestContext) {
        val result = testContext.local().command("whoami").runResult()

        result shouldHaveExitCode 0
    }
}

Run command or script inside Docker

package solidblocks.test.gradle.command

import de.solidblocks.infra.test.SolidblocksTest
import de.solidblocks.infra.test.SolidblocksTestContext
import de.solidblocks.infra.test.assertions.shouldHaveExitCode
import de.solidblocks.infra.test.docker.DockerTestImage
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(SolidblocksTest::class)
class DockerCommandContext {
    @Test
    fun dockerCommandContext(testContext: SolidblocksTestContext) {
        val result = testContext.docker(DockerTestImage.UBUNTU_22).command("whoami").runResult()

        result shouldHaveExitCode 0
    }
}

Options

Both the local and the docker command execution can be configured before the command is executed

package solidblocks.test.gradle.command

import de.solidblocks.infra.test.SolidblocksTest
import de.solidblocks.infra.test.SolidblocksTestContext
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.nio.file.Path
import kotlin.time.Duration.Companion.seconds

@ExtendWith(SolidblocksTest::class)
class LocalCommandOptions {
    @Test
    fun localCommandContext(testContext: SolidblocksTestContext) {
        val command = testContext.local().command("whoami")

        // timeout for running the command
        command.timeout(10.seconds)

        // inherit environment variables from the shell that spawned the units tests
        command.inheritEnv(true)

        // set working directory for command execution
        command.workingDir(Path.of("/tmp"))

        // set environment variable for command
        command.env("ENV_VAR1" to "foo-bar")

        // command.runResult()
    }
}

Assertions

The following assertions are available for the results of local().command(...).runResult() as well as docker(DockerTestImage.UBUNTU_22).command(...).runResult()

package solidblocks.test.gradle.command

import de.solidblocks.infra.test.SolidblocksTest
import de.solidblocks.infra.test.SolidblocksTestContext
import de.solidblocks.infra.test.assertions.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

@ExtendWith(SolidblocksTest::class)
class CommandAssertions {
    @Test
    fun commandAssertions(testContext: SolidblocksTestContext) {
        val result = testContext.local().command("whoami").runResult()

        result shouldHaveExitCode 0

        result outputShouldBe "something"
        result stderrShouldBe "something"

        result stderrShouldMatch ".*something.*"
        result stdoutShouldMatch ".*something.*"

        result.stdoutShouldBeEmpty()
        result.stderrShouldBeEmpty()

        result runtimeShouldBeGreaterThan 10.milliseconds
        result runtimeShouldBeLessThan 5.seconds
    }
}