Skip to content

Commit 9f32d0c

Browse files
authored
ANDROID-15781 Fail in case plugin is applied before app/library plugin and fix issues with file overwriting in emulators. (#6)
1 parent a8b0eb3 commit 9f32d0c

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.telefonica.loggerazzi
2+
3+
class LoggerazziNoDeviceProviderInstrumentTestTasksException : Exception(
4+
"No device provider instrument test tasks found. Make sure you are applying the Loggerazzi plugin after the Android app/library plugin."
5+
)

include-build/gradle-plugin/src/main/java/com/telefonica/loggerazzi/LoggerazziPlugin.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ class LoggerazziPlugin @Inject constructor(
1616

1717
override fun apply(project: Project) {
1818
project.afterEvaluate {
19-
project.tasks
19+
20+
val deviceProviderInstrumentTestTasks = project.tasks
2021
.withType(DeviceProviderInstrumentTestTask::class.java)
22+
23+
if (deviceProviderInstrumentTestTasks.isEmpty()) {
24+
throw LoggerazziNoDeviceProviderInstrumentTestTasksException()
25+
}
26+
27+
deviceProviderInstrumentTestTasks
2128
.forEach { deviceProviderTask ->
2229
val capitalizedVariant = deviceProviderTask.variantName.capitalizeFirstLetter()
2330
val beforeTaskName = "loggerazziBefore$capitalizedVariant"
@@ -48,10 +55,12 @@ class LoggerazziPlugin @Inject constructor(
4855
val recordedFolderFile = reportsFolder.dir("recorded").asFile.apply {
4956
mkdirs()
5057
deviceFileManager.pullRecordedLogs(absolutePath)
58+
processAndFilterResults()
5159
}
5260
val failuresFolderFile = reportsFolder.dir("failures").asFile.apply {
5361
mkdirs()
5462
deviceFileManager.pullFailuresLogs(absolutePath)
63+
processAndFilterResults()
5564
}
5665
val goldenForFailuresReportFolderFile = reportsFolder.dir("golden").asFile.apply {
5766
mkdirs()
@@ -93,7 +102,7 @@ class LoggerazziPlugin @Inject constructor(
93102
} else {
94103
File(getAbsoluteGoldenLogsSourcePath()).apply {
95104
mkdirs()
96-
deviceFileManager.pullRecordedLogs(absolutePath)
105+
recordedFolderFile.copyRecursively(this, true)
97106
}
98107
}
99108
}
@@ -123,4 +132,22 @@ class LoggerazziPlugin @Inject constructor(
123132
private fun String.capitalizeFirstLetter(): String {
124133
return replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
125134
}
135+
136+
private fun File.processAndFilterResults() {
137+
listFiles()
138+
?.groupBy {
139+
it.name.substringBeforeLast(".")
140+
}
141+
?.forEach { (key, filesGroup) ->
142+
val lastFile = filesGroup.maxByOrNull {
143+
it.name.substringAfterLast(".").toLong()
144+
}
145+
filesGroup.forEach { file ->
146+
if (file != lastFile) {
147+
file.delete()
148+
}
149+
}
150+
lastFile?.renameTo(File(this, "$key.txt"))
151+
}
152+
}
126153
}

include-build/gradle-plugin/src/main/java/com/telefonica/loggerazzi/LoggerazziReportConst.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,6 @@ REPORT_TEMPLATE_BODY
8787
</div>
8888
</div>
8989
90-
<div id="imageBottomSheet" class="modal bottom-sheet max-height">
91-
<div class="modal-content center-align">
92-
<img id="modalImage" src="" alt="">
93-
</div>
94-
</div>
95-
9690
<footer class="page-footer orange">
9791
<div class="container">
9892
<a class="us" href="https://github.com/Telefonica/loggerazzi" target="_blank"

loggerazzi/src/main/java/com/telefonica/loggerazzi/LoggerazziRule.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,24 @@ open class GenericLoggerazziRule<LogType>(
4747
override fun succeeded(description: Description?) {
4848
super.succeeded(description)
4949

50-
val testName = "${description?.className}_${description?.methodName}.txt"
50+
val testName = "${description?.className}_${description?.methodName}"
51+
val fileName = "${testName}.${System.nanoTime()}"
5152

5253
val recordedLogs = recorder.getRecordedLogs()
5354
val log = recordedLogs.joinToString("\n") { stringMapper.fromLog(it) }
54-
val testFile = File(recordedDir, testName)
55-
testFile.delete()
55+
val testFile = File(recordedDir, fileName)
5656
testFile.createNewFile()
5757
testFile.writeText(log)
5858

5959
if (InstrumentationRegistry.getArguments().getString("record") != "true") {
6060
val goldenFile =
6161
InstrumentationRegistry.getInstrumentation().context.assets.open(
62-
"loggerazzi-golden-files/$testName"
62+
"loggerazzi-golden-files/${testName}.txt"
6363
)
6464
val goldenStringLogs = String(goldenFile.readBytes()).takeIf { it.isNotEmpty() }?.split("\n") ?: emptyList()
6565
val result = comparator.compare(recordedLogs, goldenStringLogs.map { stringMapper.toLog(it) })
6666
if (result != null) {
67-
val compareFile = File(failuresDir, testName)
68-
compareFile.delete()
67+
val compareFile = File(failuresDir, fileName)
6968
compareFile.createNewFile()
7069
compareFile.writeText(result)
7170
throw AssertionError("Logs do not match:\n$result")

0 commit comments

Comments
 (0)