Skip to content

Commit

Permalink
Rebased from master
Browse files Browse the repository at this point in the history
- updating Xctestrun and FindTestNames methods - wip
  • Loading branch information
zuziaka committed Nov 2, 2020
1 parent a9f6144 commit 38ae7d2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
7 changes: 5 additions & 2 deletions test_runner/src/main/kotlin/ftl/ios/Xctestrun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.ios

import com.dd.plist.NSDictionary
import ftl.ios.xctest.findTestNames
import ftl.ios.xctest.findTestsForTestTarget
import ftl.ios.xctest.parseToNSDictionary
import ftl.ios.xctest.rewriteXcTestRun
import java.io.File
Expand All @@ -14,7 +15,9 @@ object Xctestrun {

fun parse(xctestrun: ByteArray): NSDictionary = parseToNSDictionary(xctestrun)

fun findTestNames(xctestrun: String): List<String> = findTestNames(File(xctestrun))
fun findTestNames(xctestrun: String): XctestrunMethods = findTestNames(File(xctestrun))

fun rewrite(root: NSDictionary, methods: Collection<String>) = rewriteXcTestRun(root, methods)
fun findTestsForTarget(testTarget: String, xctestrun: String): List<String> = findTestsForTestTarget(testTarget, File(xctestrun))

fun rewrite(xctestrun: String, methods: List<String>) = rewriteXcTestRun(xctestrun, methods)
}
23 changes: 17 additions & 6 deletions test_runner/src/main/kotlin/ftl/ios/xctest/FindTestNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,43 @@ import com.dd.plist.NSDictionary
import com.dd.plist.NSObject
import com.dd.plist.NSString
import ftl.ios.Parse
import ftl.ios.XctestrunMethods
import ftl.run.exception.FlankGeneralError
import java.io.File
import java.nio.file.Paths

// Finds tests in a xctestrun file
internal fun findTestNames(xctestrun: File): List<String> =
internal fun findTestNames(xctestrun: File): XctestrunMethods =
parseToNSDictionary(xctestrun).run {
val testRoot = xctestrun.parent + "/"
allKeys().map { testTarget ->
(get(testTarget) as NSDictionary).findTestsForTarget(
testTarget to (get(testTarget) as NSDictionary).findTestsForTarget(
testRoot = testRoot,
testTarget = testTarget
)
}.flatten().distinct()
}.distinct().toMap()
}

internal fun findTestsForTestTarget(testTarget: String, xctestrun: File): List<String> =
parseToNSDictionary(xctestrun).run { findTestsForTarget(testTarget, testRoot = xctestrun.parent + "/") }

private fun NSDictionary.findTestsForTarget(testTarget: String, testRoot: String): List<String> =
if (testTarget.isMetadata()) emptyList()
else findXcTestTargets(testTarget)
?.run { findBinaryTests(testRoot) - findTestsToSkip() }
?: throw FlankGeneralError("No tests found")

private fun NSDictionary.findXcTestTargets(testTarget: String): NSObject? =
get("DependentProductPaths")
?.let { it as? NSArray }?.array
?.first { product -> product.toString().containsTestTarget(testTarget) }
get("DependentProductPaths")
?.let { it as? NSArray }?.array
?.first { product -> product.toString().containsTestTarget(testTarget) }
// get(testTarget)
// ?.let {
// get("DependentProductPaths")
// ?.let { it as? NSArray }?.array
// ?.first { product -> product.toString().containsTestTarget(testTarget) }
// }
// ?: throw FlankGeneralError("Test target $testTarget doesn't exist")

private fun String.containsTestTarget(name: String): Boolean = contains("/$name.xctest")

Expand Down
20 changes: 17 additions & 3 deletions test_runner/src/main/kotlin/ftl/ios/xctest/RewriteXcTestRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ package ftl.ios.xctest

import com.dd.plist.NSArray
import com.dd.plist.NSDictionary
import ftl.ios.Xctestrun.parse
import ftl.ios.XctestrunMethods
import java.io.File

fun rewriteXcTestRun(
fun rewriteXcTestRun(xctestrun: String, methods: List<String>): ByteArray {
val xctestrunFile = File(xctestrun)
val methodsToRun = findTestNames(xctestrunFile).mapValues { (_, list) -> list.filter(methods::contains) }
return rewriteXcTestRun(parse(xctestrun), methodsToRun)
}

fun rewriteXcTestRun(xctestrun: String, methodsData: XctestrunMethods): ByteArray {
val xctestrunFile = File(xctestrun)
return rewriteXcTestRun(parse(xctestrun), methodsData)
}

internal fun rewriteXcTestRun(
root: NSDictionary,
methods: Collection<String>
methods: XctestrunMethods
): ByteArray = root.clone().apply {
allKeys().filterNot(String::isMetadata).forEach { testTarget ->
(get(testTarget) as NSDictionary).setOnlyTestIdentifiers(methods)
(get(testTarget) as NSDictionary).setOnlyTestIdentifiers(methods[testTarget] ?: emptyList())
}
}.toByteArray()

Expand Down
27 changes: 14 additions & 13 deletions test_runner/src/test/kotlin/ftl/ios/XctestrunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.dd.plist.NSArray
import com.dd.plist.NSDictionary
import com.google.common.truth.Truth.assertThat
import ftl.config.FtlConstants.isWindows
import ftl.ios.xctest.rewriteXcTestRun
import ftl.test.util.FlankTestRunner
import ftl.test.util.TestHelper.normalizeLineEnding
import ftl.run.exception.FlankGeneralError
Expand Down Expand Up @@ -68,10 +69,10 @@ class XctestrunTest {
assumeFalse(isWindows)

val root = Xctestrun.parse(swiftXctestrun)
val methods = Xctestrun.findTestNames(testTarget = "EarlGreyExampleSwiftTests", xctestrun = swiftXctestrun)
val methods = Xctestrun.findTestsForTarget(testTarget = "EarlGreyExampleSwiftTests", xctestrun = swiftXctestrun)
val methodsData = mapOf<String, List<String>>("EarlGreyExampleSwiftTests" to methods)

val results = String(Xctestrun.rewrite(root, methodsData))
val results = String(rewriteXcTestRun(root, methodsData))

assertThat(results.contains("OnlyTestIdentifiers")).isTrue()
}
Expand All @@ -81,15 +82,15 @@ class XctestrunTest {
assumeFalse(isWindows)

val root = Xctestrun.parse(swiftXctestrun)
val methods = Xctestrun.findTestNames(testTarget = "EarlGreyExampleSwiftTests", xctestrun = swiftXctestrun)
val methods = Xctestrun.findTestsForTarget(testTarget = "EarlGreyExampleSwiftTests", xctestrun = swiftXctestrun)
val methodsData = mapOf<String, List<String>>("EarlGreyExampleSwiftTests" to methods)

// ensure root object isn't modified. Rewrite should return a new object.
val key = "OnlyTestIdentifiers"

assertThat(root.toASCIIPropertyList().contains(key)).isFalse()

Xctestrun.rewrite(root, methodsData)
rewriteXcTestRun(root, methodsData)

assertThat(root.toASCIIPropertyList().contains(key)).isFalse()
}
Expand All @@ -101,9 +102,9 @@ class XctestrunTest {
root["EarlGreyExampleTests"] = NSDictionary()
val methods = listOf("testOne", "testTwo")
val methodsData = mapOf<String, List<String>>("EarlGreyExampleSwiftTests" to methods, "EarlGreyExampleTests" to methods)
Xctestrun.rewrite(root, methodsData)
Xctestrun.rewrite(root, methodsData)
val result = Xctestrun.rewrite(root, methodsData)
rewriteXcTestRun(root, methodsData)
rewriteXcTestRun(root, methodsData)
val result = rewriteXcTestRun(root, methodsData)
val expected = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
Expand Down Expand Up @@ -154,7 +155,7 @@ class XctestrunTest {
val root = Xctestrun.parse(inputXml.toByteArray())

val tests = mapOf<String, List<String>>("EarlGreyExampleSwiftTests" to listOf("testOne", "testTwo"))
val rewrittenXml = String(Xctestrun.rewrite(root, tests))
val rewrittenXml = String(rewriteXcTestRun(root, tests))

assertThat(inputXml).isEqualTo(rewrittenXml.normalizeLineEnding())
}
Expand Down Expand Up @@ -203,30 +204,30 @@ class XctestrunTest {
Files.write(tmpXml, inputXml.toByteArray())
tmpXml.toFile().deleteOnExit()

val actualTests = Xctestrun.findTestNames("EarlGreyExampleSwiftTests", tmpXml.toString()).sorted()
val actualTests = Xctestrun.findTestsForTarget("EarlGreyExampleSwiftTests", tmpXml.toString()).sorted()
assertThat(actualTests).isEqualTo(listOf("EarlGreyExampleSwiftTests/testBasicSelection"))
}

@Test
fun findTestNamesForTestTarget() {
assumeFalse(isWindows)
val names = Xctestrun.findTestNames(testTarget = "EarlGreyExampleSwiftTests", xctestrun = swiftXctestrun).sorted()
val names = Xctestrun.findTestsForTarget(testTarget = "EarlGreyExampleSwiftTests", xctestrun = swiftXctestrun).sorted()
assertThat(swiftTests).isEqualTo(names)
}

@Test(expected = FlankGeneralError::class)
fun `findTestNames for nonexisting test target`() {
assumeFalse(isWindows)
Xctestrun.findTestNames(testTarget = "Incorrect", xctestrun = swiftXctestrun).sorted()
Xctestrun.findTestsForTarget(testTarget = "Incorrect", xctestrun = swiftXctestrun).sorted()
}

@Test
fun `find test names for xctestrun file containing multiple test targets`() {
assumeFalse(isWindows)
val names = Xctestrun.findTestNames(testTarget = "FlankExampleTests", xctestrun = multipleTargetsSwiftXctestrun).sorted()
val names = Xctestrun.findTestsForTarget(testTarget = "FlankExampleTests", xctestrun = multipleTargetsSwiftXctestrun).sorted()
assertThat(names).isEqualTo(listOf("FlankExampleTests/test1", "FlankExampleTests/test2"))

val names2 = Xctestrun.findTestNames(testTarget = "FlankExampleSecondTests", xctestrun = multipleTargetsSwiftXctestrun).sorted()
val names2 = Xctestrun.findTestsForTarget(testTarget = "FlankExampleSecondTests", xctestrun = multipleTargetsSwiftXctestrun).sorted()
assertThat(names2).isEqualTo(listOf("FlankExampleSecondTests/test3", "FlankExampleSecondTests/test4"))
}

Expand Down

0 comments on commit 38ae7d2

Please sign in to comment.