Skip to content

Commit

Permalink
Cover CLI by tests
Browse files Browse the repository at this point in the history
  • Loading branch information
egorikftp committed Nov 21, 2024
1 parent 13b9f08 commit 7ea9c2f
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 75 deletions.
24 changes: 19 additions & 5 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ plugins {
val baseName = "valkyrie"
val versionName = rootProject.providers.gradleProperty("VERSION_NAME").get()

application {
mainClass = "io.github.composegears.valkyrie.cli.MainKt"
applicationName = "valkyrie"
version = versionName
}

sourceSets {
test {
resources {
srcDir("$rootDir/components/sharedTestResources")
}
}
}

buildConfig {
buildConfigField("VERSION_NAME", versionName)
packageName = "io.github.composegears.valkyrie.cli"
Expand All @@ -22,11 +36,6 @@ tasks.withType<Jar>().configureEach {
attributes["Implementation-Version"] = versionName
}
}
application {
mainClass = "io.github.composegears.valkyrie.cli.MainKt"
applicationName = "valkyrie"
version = versionName
}

val buildWithR8 by tasks.registering(JavaExec::class) {
dependsOn(tasks.installShadowDist)
Expand Down Expand Up @@ -59,6 +68,11 @@ val buildCLI by tasks.registering(Zip::class) {
destinationDirectory.set(layout.buildDirectory.dir("distributions/"))
}

tasks.test {
dependsOn(buildWithR8)
systemProperty("CLI_PATH", layout.buildDirectory.file("install/cli-shadow/bin").get().asFile.path)
}

val r8: Configuration by configurations.creating

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.context
import com.github.ajalt.clikt.output.MordantHelpFormatter
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.varargValues
import com.github.ajalt.clikt.parameters.options.split
import io.github.composegears.valkyrie.cli.ext.booleanOption
import io.github.composegears.valkyrie.cli.ext.intOption
import io.github.composegears.valkyrie.cli.ext.outputInfo
import io.github.composegears.valkyrie.cli.ext.requiredPathOption
import io.github.composegears.valkyrie.cli.ext.requiredStringOption
import io.github.composegears.valkyrie.cli.ext.stringOption
import io.github.composegears.valkyrie.extensions.writeToKt
import io.github.composegears.valkyrie.generator.iconpack.IconPackGenerator
import io.github.composegears.valkyrie.generator.iconpack.IconPackGeneratorConfig
Expand Down Expand Up @@ -41,15 +42,16 @@ internal class IconPackCommand : CliktCommand(name = "iconpack") {
help = "Package name of IconPack object",
)

private val iconPackName by requiredStringOption(
private val iconPackName by stringOption(
"--iconpack-name",
help = "IconPack object name",
default = "",
)

private val nestedPacks by option(
"--nested-packs",
help = "Nested packs (e.g. Filled, Colored)",
).varargValues()
).split(",")

private val indentSize by intOption(
"--indent-size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ internal class SvgXmlToImageVectorCommand : CliktCommand(name = "svgxml2imagevec
help = "The package name of the generated sources. Usually equal to IconPack package",
)

private val iconPackName by requiredStringOption(
private val iconPackName by stringOption(
"--iconpack-name",
help = "The name of the existing IconPack",
default = "",
)

private val nestedPackName by stringOption(
Expand Down Expand Up @@ -136,8 +137,6 @@ private fun CliktCommand.outputFormatOption() = option(
}
}.default(OutputFormat.BackingProperty)

internal const val SUCCESS_MESSAGE = "Converting completed."

/**
* Converts SVG or XML files to ImageVector files.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package io.github.composegears.valkyrie.cli

import assertk.assertThat
import assertk.assertions.isEqualTo
import io.github.composegears.valkyrie.cli.IconPackCommand.IconPackName
import io.github.composegears.valkyrie.cli.IconPackCommand.IndentSize
import io.github.composegears.valkyrie.cli.IconPackCommand.NestedPacks
import io.github.composegears.valkyrie.cli.IconPackCommand.OutputPath
import io.github.composegears.valkyrie.cli.IconPackCommand.PackageName
import io.github.composegears.valkyrie.cli.IconPackCommand.UseExplicitMode
import io.github.composegears.valkyrie.cli.common.CliTestType
import io.github.composegears.valkyrie.cli.common.CliTestType.DirectMain
import io.github.composegears.valkyrie.cli.common.CliTestType.JarTerminal
import io.github.composegears.valkyrie.cli.common.CommandLineTestRunner
import io.github.composegears.valkyrie.extensions.ResourceUtils.getResourceText
import java.nio.file.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.createTempDirectory
import kotlin.io.path.readText
import kotlin.properties.Delegates
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource

class IconPackCliTest {

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate icon pack`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.kt",
)
}

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate icon pack explicit mode`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.explicit.kt",
useExplicitMode = UseExplicitMode(true),
)
}

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate nested packs`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.nested.kt",
nestedPacks = NestedPacks(listOf("Filled", "Colored")),
)
}

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate nested packs explicit`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.nested.explicit.kt",
nestedPacks = NestedPacks(listOf("Filled", "Colored")),
useExplicitMode = UseExplicitMode(true),
)
}

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate nested indent 1 packs`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.nested.indent1.kt",
nestedPacks = NestedPacks(listOf("Filled", "Colored")),
indentSize = IndentSize(1),
)
}

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate nested indent 2 packs`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.nested.indent2.kt",
nestedPacks = NestedPacks(listOf("Filled", "Colored")),
indentSize = IndentSize(2),
)
}

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate nested indent 3 packs`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.nested.indent3.kt",
nestedPacks = NestedPacks(listOf("Filled", "Colored")),
indentSize = IndentSize(3),
)
}

@ParameterizedTest
@EnumSource(value = CliTestType::class)
fun `generate nested indent 6 packs`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.nested.indent6.kt",
nestedPacks = NestedPacks(listOf("Filled", "Colored")),
indentSize = IndentSize(6),
)
}

private fun testIconPack(
cliTestType: CliTestType,
expectedResource: String,
useExplicitMode: UseExplicitMode? = null,
nestedPacks: NestedPacks? = null,
indentSize: IndentSize? = null,
) {
generateIconPack(
cliTestType = cliTestType,
packCommands = listOfNotNull(
OutputPath(tempDir.absolutePathString()),
PackageName(name = "io.github.composegears.valkyrie.icons"),
IconPackName(name = "ValkyrieIcons"),
useExplicitMode,
nestedPacks,
indentSize,
),
)

val files = tempDir.toFile().listFiles().orEmpty()
assertThat(files.size).isEqualTo(1)

val result = tempDir.resolve("ValkyrieIcons.kt").readText()
val expected = getResourceText(expectedResource)
assertThat(result).isEqualTo(expected)
}

private fun generateIconPack(cliTestType: CliTestType, packCommands: List<IconPackCommand>) {
val commands = listOf("iconpack") + packCommands.map(IconPackCommand::command)

when (cliTestType) {
DirectMain -> main(*commands.toTypedArray())
JarTerminal -> CommandLineTestRunner(commands).run()
}
}

companion object {
// Workaround for https://github.com/junit-team/junit5/issues/2811.
@JvmStatic
private var tempDir: Path by Delegates.notNull()

@JvmStatic
@BeforeAll
fun before() {
tempDir = createTempDirectory()
}

@JvmStatic
@AfterAll
fun after() {
tempDir.toFile().delete()
}
}
}

private sealed interface IconPackCommand {
val command: String

data class OutputPath(val path: String) : IconPackCommand {
override val command: String = "--output-path=$path"
}

data class PackageName(val name: String) : IconPackCommand {
override val command: String = "--package-name=$name"
}

data class IconPackName(val name: String) : IconPackCommand {
override val command: String = "--iconpack-name=$name"
}

data class NestedPacks(val packs: List<String>) : IconPackCommand {
override val command: String = "--nested-packs=${packs.joinToString(separator = ",")}"
}

data class IndentSize(val size: Int) : IconPackCommand {
override val command: String = "--indent-size=$size"
}

data class UseExplicitMode(val use: Boolean) : IconPackCommand {
override val command: String = "--use-explicit-mode=$use"
}
}

This file was deleted.

Loading

0 comments on commit 7ea9c2f

Please sign in to comment.