Testing commandline seemed to be not working #548
-
I have implemented a simple command which uses package com.soebes.kotlin.commands
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.int
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.inSet
import org.kodein.di.generic.provider
class Log : CliktCommand(printHelpOnEmptyArgs = true, help = """
Log information from the repository
""") {
val numberOfEntries by option("-n", metavar = "number", help = "Number of entries which will be shown.")
.int(acceptsValueWithoutName = true)
override fun run() {
echo("Logging with $numberOfEntries")
}
}
val logModule = Kodein.Module("log") {
bind<CliktCommand>().inSet() with provider { Log() }
} my package com.soebes.kotlin.git
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.options.versionOption
import com.soebes.kotlin.commands.cloneModule
import com.soebes.kotlin.commands.logModule
import com.soebes.kotlin.commands.statusModule
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
import org.kodein.di.generic.setBinding
class KGitter : CliktCommand(name = "KGitter", help = """
Repo is a command line tool that showcases how to build complex
command line interfaces with Clikt.
This tool is supposed to look like a distributed version control
system to show how something like this can be structured.
""") {
init {
versionOption("7.8.9")
}
override fun run() {}
}
fun main(args: Array<String>) {
val kodein = Kodein {
bind() from setBinding<CliktCommand>()
import(cloneModule)
import(statusModule)
import(logModule)
}
val commands: Set<CliktCommand> by kodein.instance()
KGitter().subcommands(commands).main(args)
} and now I want to check via test if this works as I expect it to workexpected for me: class LogTest {
@Test
fun logWithNamedOption() {
val logCommand = Log()
val result = logCommand.test(listOf("log -n2"))
println("result: $result")
assertEquals("Logging with 2 ", result.stdout)
}
@Test
fun logWithNamedOptionsSeparatedArgEntry() {
val logCommand = Log()
val result = logCommand.test(listOf("log", "-n2"))
println("result: $result")
assertEquals("Logging with 2 ", result.stdout)
}
@Test
fun logWithoutNamedOption() {
val logCommand = Log()
val result = logCommand.test(listOf("log", "-2"))
println("result: $result")
assertEquals("Logging with 2 ", result.stdout)
}
} but unfortunately every time those test fails (the print helped me) ... with:
Interestingly if I use the resulting jar file and execute it on plain command line the options are working as expected...
Also the help output looks correct:
Does someone has an idea/hint where to look for? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You're testing the |
Beta Was this translation helpful? Give feedback.
-
Outch... that makes sense... Oh my gosh... damn... Thank you... |
Beta Was this translation helpful? Give feedback.
You're testing the
Log
command directly in your tests, so you should only call it withlistOf("-2")
. On the CLI,log
is a subcommand of your rootKGitter
.