Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Separate main function from MainCommand #1724

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)