Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Add basic argument parsing test #22

Merged
merged 1 commit into from
Jun 21, 2023
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
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))
}
}