Skip to content

Commit

Permalink
Add additional debug logging to Snapshot SDK (#97)
Browse files Browse the repository at this point in the history
* Add additional debug logging to Snapshot SDK

* Lint
  • Loading branch information
rbro112 authored Oct 26, 2023
1 parent e4c9897 commit 42d46c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.emergetools.snapshots.runner

import android.util.Log
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.emergetools.snapshots.EmergeSnapshots
import org.junit.Rule
Expand All @@ -16,16 +17,26 @@ internal class SnapshotsRunner(private val testClass: Class<*>) : AndroidJUnit4C

override fun run(notifier: RunNotifier) {
if (hasEmergeSnapshotRule(testClass)) {
Log.d(TAG, "Running test class: ${testClass.simpleName}")
super.run(notifier)
} else {
Log.d(TAG, "Ignoring test class: ${testClass.simpleName}")
notifier.fireTestIgnored(description)
}
}

companion object {
const val TAG = "SnapshotsRunner"

private fun hasEmergeSnapshotRule(testClass: Class<*>): Boolean {
return testClass.methods.any {
it.isAnnotationPresent(Rule::class.java) && it.returnType == EmergeSnapshots::class.java
val hasEmergeSnapshotRule =
it.isAnnotationPresent(Rule::class.java) && it.returnType == EmergeSnapshots::class.java
Log.d(
TAG,
"${testClass.simpleName} method ${it.name} hasEmergeSnapshotRule: $hasEmergeSnapshotRule"
)
hasEmergeSnapshotRule
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.emergetools.snapshots.runner

import android.util.Log
import org.junit.Test
import org.junit.internal.builders.JUnit4Builder
import org.junit.runner.Runner
Expand All @@ -9,18 +10,25 @@ class SnapshotsRunnerBuilder : JUnit4Builder() {
// If one method in the class has a @Test method, we can safely assume it's a test class
// and the SnapshotRunner should handle.
return if (isTestClass(testClass)) {
Log.d(TAG, "Using SnapshotsRunner for class: ${testClass.simpleName}")
SnapshotsRunner(testClass)
} else {
// Fallback to allow AndroidRunnerBuilder to handle the class.
Log.d(TAG, "Using default runner for class: ${testClass.simpleName}")
null
}
}

companion object {
const val TAG = "SnapshotsRunnerBuilder"

private fun isTestClass(testClass: Class<*>): Boolean {
// Check if the class has a method annotated with @Test
return testClass.methods.any { it.isAnnotationPresent(Test::class.java) }
return testClass.methods.any {
val annotatedWithTest = it.isAnnotationPresent(Test::class.java)
Log.d(TAG, "${testClass.simpleName} method ${it.name} annotated with test: $annotatedWithTest")
annotatedWithTest
}
}
}
}

0 comments on commit 42d46c9

Please sign in to comment.