Skip to content

Commit

Permalink
Print formatted message instead of throwing NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelpasterz committed Nov 4, 2020
1 parent 208425c commit 8bbda51
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 40 deletions.
23 changes: 12 additions & 11 deletions test_runner/src/main/kotlin/ftl/args/CreateAndroidArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ftl.args.yml.AppTestPair
import ftl.config.AndroidConfig
import ftl.config.android.AndroidFlankConfig
import ftl.config.android.AndroidGcloudConfig
import ftl.util.require

fun createAndroidArgs(
config: AndroidConfig? = null,
Expand All @@ -16,15 +17,15 @@ fun createAndroidArgs(
// gcloud
appApk = gcloud.app?.normalizeFilePath(),
testApk = gcloud.test?.normalizeFilePath(),
useOrchestrator = gcloud.useOrchestrator!!,
testTargets = gcloud.testTargets!!.filterNotNull(),
useOrchestrator = gcloud::useOrchestrator.require(),
testTargets = gcloud::testTargets.require().filterNotNull(),
testRunnerClass = gcloud.testRunnerClass,
roboDirectives = gcloud.roboDirectives!!.parseRoboDirectives(),
performanceMetrics = gcloud.performanceMetrics!!,
roboDirectives = gcloud::roboDirectives.require().parseRoboDirectives(),
performanceMetrics = gcloud::performanceMetrics.require(),
numUniformShards = gcloud.numUniformShards,
environmentVariables = gcloud.environmentVariables!!,
autoGoogleLogin = gcloud.autoGoogleLogin!!,
additionalApks = gcloud.additionalApks!!.map { it.normalizeFilePath() },
environmentVariables = gcloud::environmentVariables.require(),
autoGoogleLogin = gcloud::autoGoogleLogin.require(),
additionalApks = gcloud::additionalApks.require().map { it.normalizeFilePath() },
roboScript = gcloud.roboScript?.normalizeFilePath(),

// flank
Expand All @@ -35,10 +36,10 @@ fun createAndroidArgs(
environmentVariables = env
)
} ?: emptyList(),
useLegacyJUnitResult = flank.useLegacyJUnitResult!!,
scenarioLabels = gcloud.scenarioLabels!!,
useLegacyJUnitResult = flank::useLegacyJUnitResult.require(),
scenarioLabels = gcloud::scenarioLabels.require(),
obfuscateDumpShards = obfuscate,
obbFiles = gcloud.obbfiles!!,
obbNames = gcloud.obbnames!!,
obbFiles = gcloud::obbfiles.require(),
obbNames = gcloud::obbnames.require(),
grantPermissions = gcloud.grantPermissions
)
57 changes: 29 additions & 28 deletions test_runner/src/main/kotlin/ftl/args/CreateCommonArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ftl.args.yml.toType
import ftl.config.CommonConfig
import ftl.run.status.OutputStyle
import ftl.run.status.asOutputStyle
import ftl.util.require
import ftl.util.uniqueObjectName

fun CommonConfig.createCommonArgs(
Expand All @@ -12,44 +13,44 @@ fun CommonConfig.createCommonArgs(
data = data,

// gcloud
devices = gcloud.devices!!,
devices = gcloud::devices.require(),
resultsBucket = ArgsHelper.createGcsBucket(
projectId = flank.project!!,
bucket = gcloud.resultsBucket!!
projectId = flank::project.require(),
bucket = gcloud::resultsBucket.require()
),
resultsDir = gcloud.resultsDir ?: uniqueObjectName(),
recordVideo = gcloud.recordVideo!!,
testTimeout = gcloud.timeout!!,
async = gcloud.async!!,
recordVideo = gcloud::recordVideo.require(),
testTimeout = gcloud::timeout.require(),
async = gcloud::async.require(),
resultsHistoryName = gcloud.resultsHistoryName,
flakyTestAttempts = gcloud.flakyTestAttempts!!,
flakyTestAttempts = gcloud::flakyTestAttempts.require(),
networkProfile = gcloud.networkProfile,
clientDetails = gcloud.clientDetails,
directoriesToPull = gcloud.directoriesToPull!!,
otherFiles = gcloud.otherFiles!!.mapValues { (_, path) -> path.normalizeFilePath() },
scenarioNumbers = gcloud.scenarioNumbers!!,
directoriesToPull = gcloud::directoriesToPull.require(),
otherFiles = gcloud::otherFiles.require().mapValues { (_, path) -> path.normalizeFilePath() },
scenarioNumbers = gcloud::scenarioNumbers.require(),
type = gcloud.type?.toType(),

// flank
maxTestShards = flank.maxTestShards!!,
shardTime = flank.shardTime!!,
repeatTests = flank.repeatTests!!,
smartFlankGcsPath = flank.smartFlankGcsPath!!,
smartFlankDisableUpload = flank.smartFlankDisableUpload!!,
testTargetsAlwaysRun = flank.testTargetsAlwaysRun!!,
runTimeout = flank.runTimeout!!,
fullJUnitResult = flank.fullJUnitResult!!,
project = flank.project!!,
maxTestShards = flank::maxTestShards.require(),
shardTime = flank::shardTime.require(),
repeatTests = flank::repeatTests.require(),
smartFlankGcsPath = flank::smartFlankGcsPath.require(),
smartFlankDisableUpload = flank::smartFlankDisableUpload.require(),
testTargetsAlwaysRun = flank::testTargetsAlwaysRun.require(),
runTimeout = flank::runTimeout.require(),
fullJUnitResult = flank::fullJUnitResult.require(),
project = flank::project.require(),
outputStyle = outputStyle,
keepFilePath = flank.keepFilePath!!,
ignoreFailedTests = flank.ignoreFailedTests!!,
filesToDownload = flank.filesToDownload!!,
disableSharding = flank.disableSharding!!,
localResultDir = flank.localResultsDir!!,
disableResultsUpload = flank.disableResultsUpload!!,
defaultTestTime = flank.defaultTestTime!!,
defaultClassTestTime = flank.defaultClassTestTime!!,
useAverageTestTimeForNewTests = flank.useAverageTestTimeForNewTests!!
keepFilePath = flank::keepFilePath.require(),
ignoreFailedTests = flank::ignoreFailedTests.require(),
filesToDownload = flank::filesToDownload.require(),
disableSharding = flank::disableSharding.require(),
localResultDir = flank::localResultsDir.require(),
disableResultsUpload = flank::disableResultsUpload.require(),
defaultTestTime = flank::defaultTestTime.require(),
defaultClassTestTime = flank::defaultClassTestTime.require(),
useAverageTestTimeForNewTests = flank::useAverageTestTimeForNewTests.require()
).apply {
ArgsHelper.createJunitBucket(project, smartFlankGcsPath)
}
Expand Down
3 changes: 2 additions & 1 deletion test_runner/src/main/kotlin/ftl/args/CreateIosArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ftl.args.yml.Type
import ftl.config.IosConfig
import ftl.config.ios.IosFlankConfig
import ftl.config.ios.IosGcloudConfig
import ftl.util.require

fun createIosArgs(
config: IosConfig,
Expand All @@ -28,7 +29,7 @@ private fun createIosArgs(
xctestrunZip = gcloud.test?.normalizeFilePath().orEmpty(),
xctestrunFile = gcloud.xctestrunFile?.normalizeFilePath().orEmpty(),
xcodeVersion = gcloud.xcodeVersion,
additionalIpas = gcloud.additionalIpas!!.map { it.normalizeFilePath() },
additionalIpas = gcloud::additionalIpas.require().map { it.normalizeFilePath() },
testTargets = flank.testTargets?.filterNotNull().orEmpty(),
obfuscateDumpShards = obfuscate
)
Expand Down
11 changes: 11 additions & 0 deletions test_runner/src/main/kotlin/ftl/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package ftl.util

import com.fasterxml.jackson.annotation.JsonProperty
import ftl.run.exception.FlankGeneralError
import java.io.InputStream
import java.io.StringWriter
Expand All @@ -12,6 +13,7 @@ import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.Random
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KMutableProperty
import kotlin.reflect.KProperty

fun String.trimStartLine(): String {
Expand Down Expand Up @@ -132,3 +134,12 @@ fun <R : MutableMap<String, Any>, T> mutableMapProperty(
override fun setValue(thisRef: R, property: KProperty<*>, value: T) =
thisRef.set(name ?: property.name, value as Any)
}

/**
* Used to validate values from yml config file.
* Should be used only on properties with [JsonProperty] annotation.
*/
fun <T> KMutableProperty<T?>.require() =
getter.call() ?: throw FlankGeneralError(
"Invalid value for [${setter.annotations.filterIsInstance<JsonProperty>().first().value}]: no argument value found"
)
32 changes: 32 additions & 0 deletions test_runner/src/test/kotlin/ftl/args/AndroidArgsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,7 @@ AndroidArgs
""".trimIndent()
AndroidArgs.load(yaml).validate()
}

@Test(expected = FlankGeneralError::class)
fun `should throw exception if incorrect type requested`() {
val yaml = """
Expand Down Expand Up @@ -2305,6 +2306,37 @@ AndroidArgs
""".trimIndent()
AndroidArgs.load(yaml).validate()
}

@Test
fun `should throw exception if incorrect (empty) value used in yml config file`() {
assertThrowsWithMessage(
clazz = FlankGeneralError::class,
message = "Invalid value for [test-targets]: no argument value found"
) {
AndroidArgs.load(
"""
gcloud:
app: any/path.apk
test-targets:
""".trimIndent()
)
}
}

@Test
fun `should not throw exception if incorrect (empty) value used in yml config file is overwriten with command line`() {
val cli = AndroidRunCommand()
CommandLine(cli).parseArgs("--legacy-junit-result")

AndroidArgs.load(
"""
gcloud:
app: any/path.apk
flank:
legacy-junit-result:
""".trimIndent(), cli
)
}
}

private fun AndroidArgs.Companion.load(yamlData: String, cli: AndroidRunCommand? = null): AndroidArgs =
Expand Down

0 comments on commit 8bbda51

Please sign in to comment.