Skip to content

Commit

Permalink
Remove freemarker. (#238)
Browse files Browse the repository at this point in the history
* Can just use Scala string interpolation instead of freemarker.

Fixes #231

* Format code

Co-authored-by: Pedro Gómez <pedro@karumi.com>
  • Loading branch information
yogurtearl and pedrovgs authored Jul 5, 2021
1 parent 6efd906 commit c9ee88a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 48 deletions.
1 change: 0 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {
implementation 'org.scala-lang:scala-library:2.12.13'
implementation 'org.scala-lang.modules:scala-xml_2.12:1.3.0'
implementation 'com.sksamuel.scrimage:scrimage-core_2.12:2.1.8'
implementation 'org.freemarker:freemarker:2.3.31'
implementation 'io.github.bitstorm:tinyzip-core:1.0.0'
implementation 'org.json4s:json4s-native_2.12:3.6.11'
implementation 'org.json4s:json4s-jackson_2.12:3.6.11'
Expand Down
62 changes: 24 additions & 38 deletions core/src/main/scala/com/karumi/shot/reports/ExecutionReporter.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
package com.karumi.shot.reports

import java.io.{File, FileWriter}
import java.util

import scala.collection.JavaConverters._
import org.apache.commons.io.FileUtils
import com.karumi.shot.domain._
import com.karumi.shot.domain.model.{AppId, Folder, ScreenshotComparisionErrors, ScreenshotsSuite}
import freemarker.template.{Configuration, Template, TemplateExceptionHandler}
import com.karumi.shot.templates.RecordIndexTemplate.recordIndexTemplate
import com.karumi.shot.templates.VerificationIndexTemplate.verificationIndexTemplate

class ExecutionReporter {

private val freeMarkerConfig: Configuration = {
val config = new Configuration(Configuration.VERSION_2_3_23)
config.setClassForTemplateLoading(getClass, "/templates/")
config.setDefaultEncoding("UTF-8")
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER)
config
}

def generateRecordReport(
appId: AppId,
screenshots: ScreenshotsSuite,
buildFolder: Folder,
flavor: String,
buildType: String
) = {
val input = generateRecordTemplateValues(appId, screenshots)
val template = freeMarkerConfig.getTemplate("recordIndex.ftl")
val reportFileContents = populateRecordTemplate(appId, screenshots)
resetVerificationReport(flavor, buildType)
val reportFolder = buildFolder + Config.recordingReportFolder(flavor, buildType) + "/"
writeReport(buildFolder, input, template, reportFolder)
writeReport(reportFileContents, reportFolder)
}

def generateVerificationReport(
Expand All @@ -41,24 +30,21 @@ class ExecutionReporter {
buildType: String,
showOnlyFailingTestsInReports: Boolean = false
) = {
val input =
generateVerificationTemplateValues(appId, comparision, showOnlyFailingTestsInReports)
val template = freeMarkerConfig.getTemplate("verificationIndex.ftl")
val reportFileContents =
populateVerificationTemplate(appId, comparision, showOnlyFailingTestsInReports)
resetVerificationReport(flavor, buildType)
val reportFolder = buildFolder + Config.verificationReportFolder(flavor, buildType) + "/"
writeReport(buildFolder, input, template, reportFolder)
writeReport(reportFileContents, reportFolder)
}

private def writeReport(
buildFolder: Folder,
input: util.Map[String, String],
template: Template,
fileContents: String,
reportFolder: String
) = {
val indexFile = new File(reportFolder + "index.html")
new File(reportFolder).mkdirs()
val writer = new FileWriter(indexFile)
template.process(input, writer)
writer.write(fileContents)
writer.close()
}

Expand All @@ -69,20 +55,20 @@ class ExecutionReporter {
}
}

private def generateRecordTemplateValues(
private def populateRecordTemplate(
appId: AppId,
screenshots: ScreenshotsSuite
): util.Map[String, String] = {
): String = {
val title = s"Record results: $appId"
val numberOfTests = screenshots.size
val summaryResults =
s"$numberOfTests screenshot tests recorded."
val summaryTableBody = generateRecordSummaryTableBody(screenshots)
Map(
"title" -> title,
"summaryResult" -> summaryResults,
"summaryTableBody" -> summaryTableBody
).asJava
recordIndexTemplate(
title = title,
summaryResult = summaryResults,
summaryTableBody = summaryTableBody
)
}

private def generateRecordSummaryTableBody(screenshots: ScreenshotsSuite): String = {
Expand All @@ -101,11 +87,11 @@ class ExecutionReporter {
.mkString("\n")
}

private def generateVerificationTemplateValues(
private def populateVerificationTemplate(
appId: AppId,
comparision: ScreenshotsComparisionResult,
showOnlyFailingTestsInReports: Boolean
): util.Map[String, String] = {
): String = {
val title = s"Verification results: $appId"
val screenshots = comparision.screenshots
val numberOfTests = screenshots.size
Expand All @@ -117,12 +103,12 @@ class ExecutionReporter {
generateVerificationSummaryTableBody(comparision, showOnlyFailingTestsInReports)
val screenshotsTableBody =
generateScreenshotsTableBody(comparision, showOnlyFailingTestsInReports)
Map(
"title" -> title,
"summaryResult" -> summaryResults,
"summaryTableBody" -> summaryTableBody,
"screenshotsTableBody" -> screenshotsTableBody
).asJava
verificationIndexTemplate(
title = title,
summaryResult = summaryResults,
summaryTableBody = summaryTableBody,
screenshotsTableBody = screenshotsTableBody
)
}

private def getSortedByResultScreenshots(comparison: ScreenshotsComparisionResult) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package com.karumi.shot.templates

object RecordIndexTemplate {
def recordIndexTemplate(
title: String,
summaryResult: String,
summaryTableBody: String
): String = {
// language=HTML
s"""
<!doctype html>
<html>
<head>
Expand All @@ -19,7 +29,7 @@
<body>
<nav>
<div class="nav-wrapper indigo darken-3">
<a href="#" class="brand-logo left">${title}</a>
<a href="#" class="brand-logo left">$title</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="https://github.com/karumi/shot">Shot Documentation</a></li>
</ul>
Expand All @@ -29,14 +39,14 @@
<div class="section">
<table class="highlight responsive-table">
<thead>
<h5 class="indigo-text darken-3">Shot record results: ${summaryResult}</h5>
<h5 class="indigo-text darken-3">Shot record results: $summaryResult</h5>
<tr>
<th>Test name</th>
<th>Screenshot</th>
</tr>
</thead>
<tbody>
${summaryTableBody}
$summaryTableBody
</tbody>
</table>
</div>
Expand Down Expand Up @@ -76,4 +86,7 @@
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
</body>
</html>
</html>
"""
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package com.karumi.shot.templates

object VerificationIndexTemplate {
def verificationIndexTemplate(
title: String,
summaryResult: String,
summaryTableBody: String,
screenshotsTableBody: String
): String = {
// language=HTML
s"""
<!doctype html>
<html>
<head>
Expand All @@ -19,7 +30,7 @@
<body>
<nav>
<div class="nav-wrapper indigo darken-3">
<a href="#" class="brand-logo left">${title}</a>
<a href="#" class="brand-logo left">$title</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="https://github.com/karumi/shot">Shot Documentation</a></li>
</ul>
Expand All @@ -29,15 +40,15 @@
<div class="section">
<table class="highlight responsive-table">
<thead>
<h5 class="indigo-text darken-3">Shot verification results: ${summaryResult}</h5>
<h5 class="indigo-text darken-3">Shot verification results: $summaryResult</h5>
<tr>
<th>Result</th>
<th>Test name</th>
<th>Failure reason</th>
</tr>
</thead>
<tbody>
${summaryTableBody}
$summaryTableBody
</tbody>
</table>
</div>
Expand All @@ -54,7 +65,7 @@
</tr>
</thead>
<tbody>
${screenshotsTableBody}
$screenshotsTableBody
</tbody>
</table>
</div>
Expand Down Expand Up @@ -94,4 +105,7 @@
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
</body>
</html>
</html>
"""
}
}

0 comments on commit c9ee88a

Please sign in to comment.