Skip to content

Commit

Permalink
Added unit tests and fixed tilde replace regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
sravanmedarapu committed Nov 15, 2018
1 parent 95fba21 commit de09c9b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
7 changes: 5 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object ArgsHelper {
}

fun evaluateFilePath(filePath: String): String {
var file = filePath.trim().replaceFirst("~", System.getProperty("user.home"))
var file = filePath.trim().replaceFirst(Regex("^~"), System.getProperty("user.home"))
file = substituteEnvVars(file)
// avoid File(..).canonicalPath since that will resolve symlinks
file = Paths.get(file).toAbsolutePath().normalize().toString()
Expand Down Expand Up @@ -180,7 +180,10 @@ object ArgsHelper {
val matcher = envRegex.matcher(text)
while (matcher.find()) {
val envName = matcher.group(1)
val envValue = System.getenv(envName) ?: ""
val envValue = System.getenv(envName)
if (envValue == null) {
println("WARNING: $envName not found")
}
matcher.appendReplacement(buffer, envValue)
}
matcher.appendTail(buffer)
Expand Down
64 changes: 64 additions & 0 deletions test_runner/src/test/kotlin/ftl/args/ArgsHelperFilePathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,73 @@ class ArgsHelperFilePathTest {
Truth.assertThat(actual).isEqualTo(expected)
}

@Test
fun evaluateRelativeFilePath() {
val expected = makeTmpFile("/tmp/app-debug.apk")
val testApkPath = "~/../../../../../../../../../tmp/app-debug.apk"
val actual = ArgsHelper.evaluateFilePath(testApkPath)

Truth.assertThat(actual).isEqualTo(expected)
}

@Test
fun evaluateRelativeAndWildCardsInFilePath() {
makeTmpFile("/tmp/tmp1/tmp2/tmp3/tmp4/tmp5/tmp6/tmp7/tmp8/tmp9/app-debug.apk")
val expected = makeTmpFile("/tmp/tmp1/tmp2/tmp3/tmp4/tmp5/tmp6/tmp7/tmp8/tmp9/tmp10/app-debug.apk")
val inputPath = "~/../../../../../../../../../tmp/tmp1/**/tmp4/**/tmp7/*/tmp9/*/app*debug.apk"
val actual = ArgsHelper.evaluateFilePath(inputPath)

Truth.assertThat(actual).isEqualTo(expected)
}

@Test
fun evaluateWildCardsInFilePath() {
val expected = makeTmpFile("/tmp/tmp1/tmp2/tmp3/tmp4/tmp5/tmp6/tmp7/tmp8/tmp9/app-debug.apk")
makeTmpFile("/tmp/tmp1/tmp2/tmp3/tmp4/tmp5/tmp6/tmp7/tmp8/tmp9/tmp10/app-debug.apk")
val inputPath = "/tmp/**/tmp4/**/tmp8/*/app*debug.apk"
val actual = ArgsHelper.evaluateFilePath(inputPath)

Truth.assertThat(actual).isEqualTo(expected)
}


@Test(expected = RuntimeException::class)
fun wildCardsInFileNameWithMultipleMatches() {
makeTmpFile("/tmp/tmp1/app-debug.apk")
makeTmpFile("/tmp/tmp1/app---debug.apk")
val inputPath = "/tmp/tmp1/app*debug.apk"
ArgsHelper.evaluateFilePath(inputPath)
}


@Test(expected = RuntimeException::class)
fun wildCardsInFilePathWithMultipleMatches() {
makeTmpFile("/tmp/tmp1/tmp2/tmp3/app-debug.apk")
makeTmpFile("/tmp/tmp1/tmp2/tmp3/tmp4/app-debug.apk")
val inputPath = "~/../../../../../../../../../tmp/**/tmp2/**/app*debug.apk"
ArgsHelper.evaluateFilePath(inputPath)
}

@Test
fun evaluateMultipleEnvVarsInFilePath() {
val expected = makeTmpFile("/tmp/tmp/random.xctestrun")
environmentVariables.set("TMP_DIR", "tmp")
environmentVariables.set("RANDOM_FILE", "random")
val inputPath = "/\$TMP_DIR/\$TMP_DIR/\$RANDOM_FILE.xctestrun"
val actual = ArgsHelper.evaluateFilePath(inputPath)

Truth.assertThat(actual).isEqualTo(expected)
}

@Test(expected = java.nio.file.NoSuchFileException::class)
fun evaluateInvalidFilePath() {
val testApkPath = "~/flank_test_app/invalid_path/app-debug-*.xctestrun"
ArgsHelper.evaluateFilePath(testApkPath)
}

@Test(expected = java.nio.file.NoSuchFileException::class)
fun evaluateInvalidFilePathWithTilde() {
val testApkPath = "~/flank_test_app/~/invalid_path/app-debug-*.xctestrun"
ArgsHelper.evaluateFilePath(testApkPath)
}
}

0 comments on commit de09c9b

Please sign in to comment.