Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Regular `connectedXXXXAndroidTest` target invocation is enough for verifications
./gradlew :app:connectedDebugAndroidTest
```

At the end of each test, Loggerazzi compares the recorded logs with the corresponding baseline logs (previously generated in recording mode) allowing up to 5 seconds for the logs to match the expected output.

In case of any failures due logs verifications, regular junit reports include failed tests and comparation failure reason.

Additionally, an specific Loggerazzi report is generated at --> `build/reports/androidTests/connected/debug/loggerazzi/failures.html`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,56 @@ open class GenericLoggerazziRule<LogType>(
val testName = "${description?.className}_${description?.methodName}"
val fileName = "${testName}.${System.nanoTime()}"

val recordedLogs = recorder.getRecordedLogs()
val log = recordedLogs.joinToString("\n") { stringMapper.fromLog(it) }
val testFile = File(recordedDir, fileName)
testFile.createNewFile()
testFile.writeText(log)

val recordedLogs: List<LogType>
if (InstrumentationRegistry.getArguments().getString("record") != "true" && !isTestIgnored) {
val goldenFile =
InstrumentationRegistry.getInstrumentation().context.assets.open(
"loggerazzi-golden-files/${testName}.txt"
)
val goldenStringLogs = String(goldenFile.readBytes()).takeIf { it.isNotEmpty() }?.split("\n") ?: emptyList()
val result = comparator.compare(recordedLogs, goldenStringLogs.map { stringMapper.toLog(it) })
if (result != null) {
val comparison = compare(goldenStringLogs)
if (!comparison.success) {
val compareFile = File(failuresDir, fileName)
compareFile.createNewFile()
compareFile.writeText(result)
throw AssertionError("Logs do not match:\n$result")
compareFile.writeText(comparison.failure!!)
throw AssertionError("Logs do not match:\n${comparison.failure}")
}
recordedLogs = comparison.recordedLogs
} else {
recordedLogs = recorder.getRecordedLogs()
}

val log = recordedLogs.joinToString("\n") { stringMapper.fromLog(it) }
val testFile = File(recordedDir, fileName)
testFile.createNewFile()
testFile.writeText(log)
Comment on lines +75 to +78
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved

}

private fun compare(goldenStringLogs: List<String>): Comparison<LogType> {
val goldenLogs = goldenStringLogs.map { stringMapper.toLog(it) }
val startTime = System.currentTimeMillis()
var comparison: Comparison<LogType>
do {
val recordedLogs = recorder.getRecordedLogs()
val comparisonFailure = comparator.compare(recordedLogs, goldenLogs)
comparison = Comparison(comparisonFailure, recordedLogs)
if (!comparison.success) {
Thread.sleep(RESULT_POLLING_INTERVAL_MS)
}
} while (!comparison.success && System.currentTimeMillis() - startTime < RESULT_TIMEOUT_MS)
return comparison
}

private data class Comparison<LogType>(
val failure: String?,
val recordedLogs: List<LogType>,
) {
val success: Boolean
get() = failure == null
}

private companion object {
const val RESULT_POLLING_INTERVAL_MS = 500L
const val RESULT_TIMEOUT_MS = 5000L
}
}