Skip to content

Commit

Permalink
Separate main function from MainCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-goral authored and mergify-bot committed Mar 23, 2021
1 parent 1f219ba commit b43580c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 56 deletions.
53 changes: 4 additions & 49 deletions test_runner/src/main/kotlin/ftl/Main.kt
Original file line number Diff line number Diff line change
@@ -1,56 +1,11 @@
package ftl

import ftl.cli.AuthCommand
import ftl.cli.FirebaseCommand
import ftl.cli.firebase.CancelCommand
import ftl.cli.firebase.RefreshCommand
import ftl.cli.firebase.test.AndroidCommand
import ftl.cli.firebase.test.IPBlocksCommand
import ftl.cli.firebase.test.IosCommand
import ftl.cli.firebase.test.NetworkProfilesCommand
import ftl.cli.firebase.test.ProvidedSoftwareCommand
import ftl.log.setDebugLogging
import ftl.cli.MainCommand
import ftl.run.exception.withGlobalExceptionHandling
import ftl.util.printVersionInfo
import picocli.CommandLine

@CommandLine.Command(
name = "flank.jar\n",
synopsisHeading = "",
subcommands = [
FirebaseCommand::class,
IosCommand::class,
AndroidCommand::class,
RefreshCommand::class,
CancelCommand::class,
AuthCommand::class,
ProvidedSoftwareCommand::class,
NetworkProfilesCommand::class,
IPBlocksCommand::class
]
)
class Main : Runnable {
override fun run() {
if (printVersion) printVersionInfo()
else CommandLine.usage(Main::class.java, System.out)
}

@CommandLine.Option(names = ["-v", "--version"], description = ["Prints the version"])
private var printVersion = false

@CommandLine.Option(
names = ["--debug"],
description = ["Enables debug logging"],
defaultValue = "false"
)
fun debug(enabled: Boolean) = setDebugLogging(enabled)

companion object {
@JvmStatic
fun main(args: Array<String>) {
withGlobalExceptionHandling {
CommandLine(Main()).execute(*args)
}
}
fun main(args: Array<String>) {
withGlobalExceptionHandling {
CommandLine(MainCommand()).execute(*args)
}
}
48 changes: 48 additions & 0 deletions test_runner/src/main/kotlin/ftl/cli/MainCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ftl.cli

import ftl.cli.firebase.CancelCommand
import ftl.cli.firebase.RefreshCommand
import ftl.cli.firebase.test.AndroidCommand
import ftl.cli.firebase.test.IPBlocksCommand
import ftl.cli.firebase.test.IosCommand
import ftl.cli.firebase.test.NetworkProfilesCommand
import ftl.cli.firebase.test.ProvidedSoftwareCommand
import ftl.log.setDebugLogging
import ftl.util.printVersionInfo
import picocli.CommandLine

@CommandLine.Command(
name = "flank.jar\n",
synopsisHeading = "",
subcommands = [
FirebaseCommand::class,
IosCommand::class,
AndroidCommand::class,
RefreshCommand::class,
CancelCommand::class,
AuthCommand::class,
ProvidedSoftwareCommand::class,
NetworkProfilesCommand::class,
IPBlocksCommand::class
]
)
class MainCommand : Runnable {

@CommandLine.Option(
names = ["-v", "--version"],
description = ["Prints the version"]
)
private var printVersion = false

@CommandLine.Option(
names = ["--debug"],
description = ["Enables debug logging"],
defaultValue = "false"
)
fun debug(enabled: Boolean) = setDebugLogging(enabled)

override fun run() {
if (printVersion) printVersionInfo()
else CommandLine.usage(this, System.out)
}
}
4 changes: 2 additions & 2 deletions test_runner/src/test/kotlin/Debug.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:Suppress("InvalidPackageDeclaration")

import ftl.Main
import ftl.cli.MainCommand
import ftl.run.exception.withGlobalExceptionHandling
import ftl.util.disableCrashReporting
import picocli.CommandLine
Expand All @@ -18,7 +18,7 @@ fun main() {
?: "YOUR PROJECT ID"

withGlobalExceptionHandling {
CommandLine(Main()).execute(
CommandLine(MainCommand()).execute(
// "--debug",
"firebase",
"test",
Expand Down
7 changes: 4 additions & 3 deletions test_runner/src/test/kotlin/ftl/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl

import com.google.common.truth.Truth.assertThat
import flank.common.normalizeLineEnding
import ftl.cli.MainCommand
import ftl.test.util.FlankTestRunner
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -39,7 +40,7 @@ class MainTest {
private fun runCommand(vararg args: String): String {
systemErrRule.clearLog()
systemOutRule.clearLog()
CommandLine(Main()).execute(*args)
CommandLine(MainCommand()).execute(*args)
return systemOutRule.log.normalizeLineEnding() + systemErrRule.log.normalizeLineEnding()
}

Expand Down Expand Up @@ -81,14 +82,14 @@ class MainTest {
@Test
fun `should exit with status code 0 if no args provided`() {
systemExit.expectSystemExitWithStatus(0)
Main.main(emptyArray())
main(emptyArray())
assertMainHelpStrings(systemOutRule.log)
}

@Test
fun `should terminate jvm with exit status 2 if yml parsing error occurs`() {
systemExit.expectSystemExitWithStatus(2)
Main.main(
main(
arrayOf(
"firebase",
"test",
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/test/kotlin/ftl/test/util/TestHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package ftl.test.util

import ftl.Main
import ftl.cli.MainCommand
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
Expand Down Expand Up @@ -66,4 +66,4 @@ internal fun <T : Throwable> assertThrowsWithMessage(clazz: KClass<T>, message:
assertThrows(clazz.java) { block() }.also { assertTrue(it.message?.contains(message) ?: false) }
}

internal fun runMainCommand(vararg args: String) = CommandLine(Main()).execute(*args)
internal fun runMainCommand(vararg args: String) = CommandLine(MainCommand()).execute(*args)

0 comments on commit b43580c

Please sign in to comment.