Skip to content
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,55 @@ 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.result!!)
throw AssertionError("Logs do not match:\n${comparison.result}")
}
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
Copy Markdown
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 startTime = System.currentTimeMillis()
var comparison: Comparison<LogType>
do {
val recordedLogs = recorder.getRecordedLogs()
val result = comparator.compare(recordedLogs, goldenStringLogs.map { stringMapper.toLog(it) })
Comment thread
dnieto-r marked this conversation as resolved.
Outdated
comparison = Comparison(result, recordedLogs)
if (!comparison.success) {
Thread.sleep(RESULT_POLLING_INTERVAL_MS)
Comment thread
dnieto-r marked this conversation as resolved.
}
} while (!comparison.success && System.currentTimeMillis() - startTime < RESULT_TIMEOUT_MS)
Comment thread
dnieto-r marked this conversation as resolved.
return comparison
}

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

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