Skip to content
Open
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
35 changes: 14 additions & 21 deletions src/main/kotlin/com/jaredsburrows/spoon/SpoonTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,29 @@ import com.squareup.spoon.SpoonRunner
import java.io.File
import java.time.Duration
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Task
import org.gradle.api.tasks.TaskAction

/** A [Task] that creates and runs the Spoon test runner. */
open class SpoonTask : DefaultTask() { // tasks can't be final

/** Use our Spoon extension. */
lateinit var extension: SpoonExtension
var extension: SpoonExtension = SpoonExtension()

/** Application APK (eg. app-debug.apk). */
lateinit var applicationApk: File
var applicationApk: File? = null

/** Instrumentation APK (eg. app-debug-androidTest.apk). */
lateinit var instrumentationApk: File
var instrumentationApk: File? = null

/** Results baseOutputDir. */
lateinit var outputDir: File

/** TESTING ONLY */
var testing: Boolean = false
var testValue: Boolean = true
var spoonRenderer: SpoonRunner.Builder? = null
var outputDir: File? = null

@Suppress("unused")
@TaskAction
fun spoonTask() {
if (extension.className.isEmpty() && extension.methodName.isNotEmpty()) {
throw IllegalStateException("'${extension.methodName}' must have a fully qualified class " +
"name.")
check(!(extension.className.isEmpty() and extension.methodName.isNotEmpty())) {
"'${extension.methodName}' must have a fully qualified class name."
}

val builder = SpoonRunner.Builder()
Expand All @@ -55,8 +48,10 @@ open class SpoonTask : DefaultTask() { // tasks can't be final
.setClearAppDataBeforeEachTest(extension.clearAppDataBeforeEachTest)

// APKs
if (!testing) {
instrumentationApk?.let {
builder.setTestApk(instrumentationApk)
}
applicationApk?.let {
builder.addOtherApk(applicationApk)
}

Expand All @@ -81,8 +76,8 @@ open class SpoonTask : DefaultTask() { // tasks can't be final
if (extension.instrumentationArgs.isNotEmpty()) {
val instrumentationArgs = hashMapOf<String, String>()
extension.instrumentationArgs.forEach { instrumentation ->
if (!(instrumentation.contains(':') or instrumentation.contains('='))) {
throw UnsupportedOperationException("Please use '=' or ':' to separate arguments.")
check(instrumentation.contains(':') or instrumentation.contains('=')) {
"Please use '=' or ':' to separate arguments."
}

val keyVal = if (instrumentation.contains(':')) {
Expand Down Expand Up @@ -110,12 +105,10 @@ open class SpoonTask : DefaultTask() { // tasks can't be final
builder.addDevice(it)
}

spoonRenderer = builder
builder.build().run()

val success = if (testing) testValue else builder.build().run()
if (!success && !extension.ignoreFailures) {
throw GradleException("Tests failed! " +
"See ${ConsoleRenderer.asClickableFileUrl(File(outputDir, "index.html"))}")
check(builder.build().run() && extension.ignoreFailures) {
"Tests failed! See ${ConsoleRenderer.asClickableFileUrl(File(outputDir, "index.html"))}"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SpoonPluginSpec extends Specification {
@Rule TemporaryFolder testProjectDir = new TemporaryFolder()
private def MANIFEST_FILE_PATH = 'src/main/AndroidManifest.xml'
private def MANIFEST = "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package=\"com.example\"/>"
private def APP_APK = 'project-debug.apk'
private def APP_APK = 'project-debug.apk'
private def TEST_APK = 'project-debug-androidTest.apk'
private Project project
private File appApk
Expand Down
5 changes: 0 additions & 5 deletions src/test/groovy/com/jaredsburrows/spoon/SpoonTaskSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ final class SpoonTaskSpec extends Specification {
project.evaluate()

SpoonTask task = project.tasks.getByName(taskName) as SpoonTask
task.testing = true
task.applicationApk = appApk
task.instrumentationApk = testApk
task.execute()
Expand Down Expand Up @@ -148,7 +147,6 @@ final class SpoonTaskSpec extends Specification {
project.evaluate()

SpoonTask task = project.tasks.getByName(taskName) as SpoonTask
task.testing = true
task.applicationApk = appApk
task.instrumentationApk = testApk
task.execute()
Expand Down Expand Up @@ -250,7 +248,6 @@ final class SpoonTaskSpec extends Specification {
project.evaluate()

SpoonTask task = project.tasks.getByName(taskName) as SpoonTask
task.testing = true
task.applicationApk = testApk
task.instrumentationApk = testApk
task.execute()
Expand Down Expand Up @@ -350,8 +347,6 @@ final class SpoonTaskSpec extends Specification {
project.evaluate()

SpoonTask task = project.tasks.getByName(taskName) as SpoonTask
task.testing = true
task.testValue = false
task.applicationApk = appApk
task.instrumentationApk = testApk
task.execute()
Expand Down