From 3dd27d49b623a5c8c2a4a32256e36d2d20a7dbbc Mon Sep 17 00:00:00 2001 From: bootstraponline Date: Fri, 16 Nov 2018 09:24:23 -0500 Subject: [PATCH] Fix single glob splitting --- .../src/main/kotlin/ftl/args/ArgsFileVisitor.kt | 3 ++- test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt | 11 ++++++----- .../src/test/kotlin/ftl/args/AndroidArgsFileTest.kt | 5 +++-- .../test/kotlin/ftl/args/ArgsHelperFilePathTest.kt | 9 +++++++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/test_runner/src/main/kotlin/ftl/args/ArgsFileVisitor.kt b/test_runner/src/main/kotlin/ftl/args/ArgsFileVisitor.kt index 6cd5a229e5..24d0100ac8 100644 --- a/test_runner/src/main/kotlin/ftl/args/ArgsFileVisitor.kt +++ b/test_runner/src/main/kotlin/ftl/args/ArgsFileVisitor.kt @@ -32,13 +32,14 @@ class ArgsFileVisitor(glob: String) : SimpleFileVisitor() { companion object { private const val RECURSE = "/**" + private const val SINGLE_GLOB = "/*" } @Throws(java.nio.file.NoSuchFileException::class) fun walk(searchPath: Path): List { val searchString = searchPath.toString() // /Users/tmp/code/flank/test_app/** => /Users/tmp/code/flank/test_app/ - val beforeGlob = Paths.get(searchString.substringBefore(RECURSE)) + val beforeGlob = Paths.get(searchString.substringBefore(SINGLE_GLOB)) // must not follow links when resolving paths or /tmp turns into /private/tmp val realPath = beforeGlob.toRealPath(LinkOption.NOFOLLOW_LINKS) diff --git a/test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt b/test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt index ed968953a2..a0c2c31ea5 100644 --- a/test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt +++ b/test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt @@ -50,7 +50,7 @@ object ArgsHelper { fun evaluateFilePath(filePath: String): String { var file = filePath.trim().replaceFirst(Regex("^~"), System.getProperty("user.home")) - file = substituteEnvVars(file) + file = evaluateEnvVars(file) // avoid File(..).canonicalPath since that will resolve symlinks file = Paths.get(file).toAbsolutePath().normalize().toString() @@ -63,7 +63,8 @@ object ArgsHelper { } else if (filePaths.isEmpty()) { Utils.fatalError("'$file' not found ($filePath)") } - return filePaths[0].toAbsolutePath().toString() + + return filePaths.first().toAbsolutePath().normalize().toString() } fun assertGcsFileExists(uri: String) { @@ -175,16 +176,16 @@ object ArgsHelper { // https://stackoverflow.com/a/2821201/2450315 private val envRegex = Pattern.compile("\\$([a-zA-Z_]+[a-zA-Z0-9_]*)") - private fun substituteEnvVars(text: String): String { + private fun evaluateEnvVars(text: String): String { val buffer = StringBuffer() val matcher = envRegex.matcher(text) while (matcher.find()) { val envName = matcher.group(1) - val envValue = System.getenv(envName) + val envValue: String? = System.getenv(envName) if (envValue == null) { println("WARNING: $envName not found") } - matcher.appendReplacement(buffer, envValue) + matcher.appendReplacement(buffer, envValue ?: "") } matcher.appendTail(buffer) return buffer.toString() diff --git a/test_runner/src/test/kotlin/ftl/args/AndroidArgsFileTest.kt b/test_runner/src/test/kotlin/ftl/args/AndroidArgsFileTest.kt index e56158b6e4..35f1b63953 100644 --- a/test_runner/src/test/kotlin/ftl/args/AndroidArgsFileTest.kt +++ b/test_runner/src/test/kotlin/ftl/args/AndroidArgsFileTest.kt @@ -18,6 +18,7 @@ import org.junit.contrib.java.lang.system.SystemOutRule import org.junit.rules.ExpectedException import org.junit.runner.RunWith import java.io.File +import ftl.test.util.TestHelper.absolutePath @RunWith(FlankTestRunner::class) class AndroidArgsFileTest { @@ -43,8 +44,8 @@ class AndroidArgsFileTest { private val testName = "class com.example.app.ExampleUiTest#testPasses" private val directoryToPull = "/sdcard/screenshots" - private val appApkAbsolutePath = File(appApkLocal).absolutePath - private val testApkAbsolutePath = File(testApkLocal).absolutePath + private val appApkAbsolutePath = appApkLocal.absolutePath() + private val testApkAbsolutePath = testApkLocal.absolutePath() // NOTE: Change working dir to '%MODULE_WORKING_DIR%' in IntelliJ to match gradle for this test to pass. @Test fun localConfigLoadsSuccessfully() { diff --git a/test_runner/src/test/kotlin/ftl/args/ArgsHelperFilePathTest.kt b/test_runner/src/test/kotlin/ftl/args/ArgsHelperFilePathTest.kt index 7e45d7fef4..f5e36d07af 100644 --- a/test_runner/src/test/kotlin/ftl/args/ArgsHelperFilePathTest.kt +++ b/test_runner/src/test/kotlin/ftl/args/ArgsHelperFilePathTest.kt @@ -68,6 +68,15 @@ class ArgsHelperFilePathTest { Truth.assertThat(actual).isEqualTo(expected) } + @Test + fun evaluateSingleGlobBeforeDouble() { + val expected = makeTmpFile("/tmp/tmp1/tmp2/singleglob/app-debug.apk") + val inputPath = "/tmp/*/**/singleglob/app-debug.apk" + val actual = ArgsHelper.evaluateFilePath(inputPath) + + Truth.assertThat(actual).isEqualTo(expected) + } + @Test fun evaluateRelativeAndWildCardsInFilePath() { makeTmpFile("/tmp/tmp1/tmp2/tmp3/tmp4/tmp5/tmp6/tmp7/tmp8/tmp9/app-debug.apk")