From b43580cd1af06fcdfda7df31f406fa6ac2ec3b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janek=20G=C3=B3ral?= Date: Tue, 23 Mar 2021 09:03:27 +0100 Subject: [PATCH] Separate main function from MainCommand --- test_runner/src/main/kotlin/ftl/Main.kt | 53 ++----------------- .../src/main/kotlin/ftl/cli/MainCommand.kt | 48 +++++++++++++++++ test_runner/src/test/kotlin/Debug.kt | 4 +- test_runner/src/test/kotlin/ftl/MainTest.kt | 7 +-- .../test/kotlin/ftl/test/util/TestHelper.kt | 4 +- 5 files changed, 60 insertions(+), 56 deletions(-) create mode 100644 test_runner/src/main/kotlin/ftl/cli/MainCommand.kt diff --git a/test_runner/src/main/kotlin/ftl/Main.kt b/test_runner/src/main/kotlin/ftl/Main.kt index ba09f43ca1..d3c53afd57 100644 --- a/test_runner/src/main/kotlin/ftl/Main.kt +++ b/test_runner/src/main/kotlin/ftl/Main.kt @@ -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) { - withGlobalExceptionHandling { - CommandLine(Main()).execute(*args) - } - } +fun main(args: Array) { + withGlobalExceptionHandling { + CommandLine(MainCommand()).execute(*args) } } diff --git a/test_runner/src/main/kotlin/ftl/cli/MainCommand.kt b/test_runner/src/main/kotlin/ftl/cli/MainCommand.kt new file mode 100644 index 0000000000..77204883a9 --- /dev/null +++ b/test_runner/src/main/kotlin/ftl/cli/MainCommand.kt @@ -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) + } +} diff --git a/test_runner/src/test/kotlin/Debug.kt b/test_runner/src/test/kotlin/Debug.kt index d0254d61e7..00cd2d5526 100644 --- a/test_runner/src/test/kotlin/Debug.kt +++ b/test_runner/src/test/kotlin/Debug.kt @@ -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 @@ -18,7 +18,7 @@ fun main() { ?: "YOUR PROJECT ID" withGlobalExceptionHandling { - CommandLine(Main()).execute( + CommandLine(MainCommand()).execute( // "--debug", "firebase", "test", diff --git a/test_runner/src/test/kotlin/ftl/MainTest.kt b/test_runner/src/test/kotlin/ftl/MainTest.kt index 32489a5d3a..cea5ee0641 100644 --- a/test_runner/src/test/kotlin/ftl/MainTest.kt +++ b/test_runner/src/test/kotlin/ftl/MainTest.kt @@ -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 @@ -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() } @@ -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", diff --git a/test_runner/src/test/kotlin/ftl/test/util/TestHelper.kt b/test_runner/src/test/kotlin/ftl/test/util/TestHelper.kt index 8b34bfdfa5..d2a84b9f1c 100644 --- a/test_runner/src/test/kotlin/ftl/test/util/TestHelper.kt +++ b/test_runner/src/test/kotlin/ftl/test/util/TestHelper.kt @@ -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 @@ -66,4 +66,4 @@ internal fun assertThrowsWithMessage(clazz: KClass, 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)