Skip to content

Commit

Permalink
Add basic argument parsing test (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Jun 21, 2023
1 parent 93171f1 commit 562d325
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main/kotlin/slack/cli/exec/ProcessedExecCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlin.io.path.deleteRecursively
import kotlin.system.exitProcess
import okio.buffer
import okio.source
import org.jetbrains.annotations.TestOnly
import slack.cli.projectDirOption

/**
Expand All @@ -44,17 +45,21 @@ import slack.cli.projectDirOption
public class ProcessedExecCli :
CliktCommand("Executes a command with Bugsnag tracing and retries as needed.") {

private val projectDir by projectDirOption()
private val verbose by option("--verbose", "-v").flag()
private val bugsnagKey by option("--bugsnag-key", envvar = "PE_BUGSNAG_KEY")
private val configurationFile by
internal val projectDir by projectDirOption()
internal val verbose by option("--verbose", "-v").flag()
internal val bugsnagKey by option("--bugsnag-key", envvar = "PE_BUGSNAG_KEY")
internal val configurationFile by
option("--config", envvar = "PE_CONFIGURATION_FILE")
.path(mustExist = true, canBeFile = true, canBeDir = false)

private val args by argument().multiple()
@get:TestOnly internal val parseOnly by option("--parse-only").flag(default = false)

internal val args by argument().multiple()

@OptIn(ExperimentalStdlibApi::class, ExperimentalPathApi::class)
override fun run() {
if (parseOnly) return

val moshi = ProcessingUtil.newMoshi()
val config =
configurationFile?.let {
Expand Down
53 changes: 53 additions & 0 deletions src/test/kotlin/slack/cli/exec/ProcessedExecCliTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 Slack Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package slack.cli.exec

import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

class ProcessedExecCliTest {

@JvmField @Rule val temporaryFolder = TemporaryFolder()

@Test
fun standardParsing() {
val configFile = temporaryFolder.newFile("config.json")
val args = "./gradlew build -Pvariant=debug"
val parsed =
ProcessedExecCli().apply {
parse(
arrayOf(
"--project-dir",
temporaryFolder.root.absolutePath,
"--bugsnag-key=1234",
"--verbose",
"--parse-only",
"--config",
configFile.absolutePath,
"--",
args
)
)
}
assertThat(parsed.projectDir).isEqualTo(temporaryFolder.root.toPath())
assertThat(parsed.verbose).isTrue()
assertThat(parsed.bugsnagKey).isEqualTo("1234")
assertThat(parsed.configurationFile).isEqualTo(configFile.toPath())
assertThat(parsed.args).isEqualTo(listOf(args))
}
}

0 comments on commit 562d325

Please sign in to comment.