Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
pettero committed Feb 7, 2024
1 parent b3a667e commit 392ebed
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.karumi.shot.reports

import java.io.{File, FileWriter}

import com.karumi.shot.domain._
import com.karumi.shot.domain.model.{AppId, Folder, ScreenshotComparisionErrors, ScreenshotsSuite}
import com.karumi.shot.domain.model.{AppId, ScreenshotComparisionErrors, ScreenshotsSuite}
import com.karumi.shot.templates.RecordIndexTemplate.recordIndexTemplate
import com.karumi.shot.templates.VerificationIndexTemplate.verificationIndexTemplate

import java.io.{File, FileWriter}

class HtmlExecutionReporter extends ExecutionReporter {

def generateRecordReport(
Expand All @@ -20,18 +20,6 @@ class HtmlExecutionReporter extends ExecutionReporter {
writeReport(reportFileContents, reportFolder)
}

def generateVerificationReport(
appId: AppId,
comparision: ScreenshotsComparisionResult,
shotFolder: ShotFolder,
showOnlyFailingTestsInReports: Boolean = false
) = {
val reportFileContents =
populateVerificationTemplate(appId, comparision, showOnlyFailingTestsInReports)
resetVerificationReport(shotFolder)
val reportFolder = shotFolder.verificationReportFolder()
writeReport(reportFileContents, reportFolder)
}
private def writeReport(
fileContents: String,
reportFolder: String
Expand Down Expand Up @@ -84,6 +72,19 @@ class HtmlExecutionReporter extends ExecutionReporter {
.mkString("\n")
}

def generateVerificationReport(
appId: AppId,
comparision: ScreenshotsComparisionResult,
shotFolder: ShotFolder,
showOnlyFailingTestsInReports: Boolean = false
) = {
val reportFileContents =
populateVerificationTemplate(appId, comparision, showOnlyFailingTestsInReports)
resetVerificationReport(shotFolder)
val reportFolder = shotFolder.verificationReportFolder()
writeReport(reportFileContents, reportFolder)
}

private def populateVerificationTemplate(
appId: AppId,
comparision: ScreenshotsComparisionResult,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package com.karumi.shot.reports

import com.karumi.shot.domain.{DifferentImageDimensions, DifferentScreenshots, Screenshot, ScreenshotComparisonError, ScreenshotNotFound, ScreenshotsComparisionResult, ShotFolder}
import com.karumi.shot.domain._
import com.karumi.shot.domain.model.{AppId, ScreenshotComparisionErrors, ScreenshotsSuite}

import java.io.{File, FileWriter}
import scala.collection.IterableOnce.iterableOnceExtensionMethods
import scala.language.postfixOps

class JunitExecutionReporter extends ExecutionReporter {

def generateRecordReport(
appId: AppId,
screenshots: ScreenshotsSuite,
shotFolder: ShotFolder
): Unit = ()
appId: AppId,
screenshots: ScreenshotsSuite,
shotFolder: ShotFolder
): Unit = ()

def generateVerificationReport(
appId: AppId,
comparision: ScreenshotsComparisionResult,
shotFolder: ShotFolder,
showOnlyFailingTestsInReports: Boolean = false
): Unit = {
appId: AppId,
comparision: ScreenshotsComparisionResult,
shotFolder: ShotFolder,
showOnlyFailingTestsInReports: Boolean = false
): Unit = {
val reportFileContents =
populateVerificationTemplate(appId, comparision)
resetVerificationReport(shotFolder)
Expand All @@ -29,9 +28,9 @@ class JunitExecutionReporter extends ExecutionReporter {
}

private def writeReport(
fileContents: String,
reportFolder: String
): Unit = {
fileContents: String,
reportFolder: String
): Unit = {
val indexFile = new File(reportFolder + "TEST-Shot.xml")
new File(reportFolder).mkdirs()
val writer = new FileWriter(indexFile)
Expand All @@ -47,9 +46,9 @@ class JunitExecutionReporter extends ExecutionReporter {
}

private def populateVerificationTemplate(
appId: AppId,
comparision: ScreenshotsComparisionResult
): String = {
appId: AppId,
comparision: ScreenshotsComparisionResult
): String = {
val title = s"Screenshot results: $appId"
val summaryTableBody =
generateVerificationSummaryTableBody(comparision)
Expand All @@ -67,52 +66,53 @@ class JunitExecutionReporter extends ExecutionReporter {
}

private def generateVerificationSummaryTableBody(
comparisionResult: ScreenshotsComparisionResult

): String = {
comparisionResult: ScreenshotsComparisionResult
): String = {
val groupedScreenshots =
comparisionResult
.screenshots
.groupBy { (screenshot: Screenshot) =>
screenshot.testClass
}
comparisionResult.screenshots
.groupBy { (screenshot: Screenshot) =>
screenshot.testClass
}
groupedScreenshots
.map { case (testSuite: String, screenshots: Seq[Screenshot]) =>
val tests = screenshots
.map((screenshot) => {
val error = findError(screenshot = screenshot, comparisionResult.errors)
val tests: String = screenshots
.map(f = screenshot => {
val error =
findError(screenshot = screenshot, comparisionResult.errors)
val isFailedTest = error.isDefined
val testClass = screenshot.testClass
val testName = screenshot.fileName
val reason = generateReasonMessage(error)
val testClass = screenshot.testClass
val testName = screenshot.fileName
val reason = generateReasonMessage(error)

val failureString = if (isFailedTest) {
s"""<failure message="${reason}" />"""
s"""<failure message="$reason" />"""
} else {
""
}

s"""<testcase name="$testName" classname="$testClass">
| $failureString
|</testcase>""".stripMargin
}).mkString("\n")
| $failureString
|</testcase>""".stripMargin
})
.mkString("\n")

s"""<testsuite name="${testSuite}">
| ${tests}
s"""<testsuite name="$testSuite">
| $tests
|</testsuite>
|""".stripMargin
}.mkString("\n")
}
.mkString("\n")
}

private def findError(
screenshot: Screenshot,
errors: ScreenshotComparisionErrors
): Option[ScreenshotComparisonError] =
screenshot: Screenshot,
errors: ScreenshotComparisionErrors
): Option[ScreenshotComparisonError] =
errors.find {
case ScreenshotNotFound(error) => screenshot == error
case ScreenshotNotFound(error) => screenshot == error
case DifferentImageDimensions(error, _, _) => screenshot == error
case DifferentScreenshots(error, _) => screenshot == error
case _ => false
case DifferentScreenshots(error, _) => screenshot == error
case _ => false
}

private def generateReasonMessage(error: Option[ScreenshotComparisonError]): String =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.karumi.shot.screenshots

import java.io.File
import com.karumi.shot.base64.Base64Encoder
import com.karumi.shot.domain.model.ScreenshotComparisionErrors
import com.karumi.shot.domain.{DifferentScreenshots, ScreenshotsComparisionResult}
Expand All @@ -9,6 +8,7 @@ import com.sksamuel.scrimage.composite.RedComposite
import com.sksamuel.scrimage.nio.PngWriter

import java.awt.image.BufferedImage
import java.io.File
import scala.collection.parallel.CollectionConverters._

class ScreenshotsDiffGenerator(base64Encoder: Base64Encoder) {
Expand Down Expand Up @@ -40,7 +40,10 @@ class ScreenshotsDiffGenerator(base64Encoder: Base64Encoder) {
.fromFile(new File(originalImagePath))
.copy(BufferedImage.TYPE_INT_ARGB)
val newImage =
ImmutableImage.loader().fromFile(new File(newImagePath)).copy(BufferedImage.TYPE_INT_ARGB)
ImmutableImage
.loader()
.fromFile(new File(newImagePath))
.copy(BufferedImage.TYPE_INT_ARGB)
val diff = newImage.composite(new RedComposite(1d), originalImage)
val outputFilePath = screenshot.getDiffScreenshotPath(outputFolder)
diff.output(PngWriter.MaxCompression, outputFilePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
package com.karumi.shot.screenshots

import java.io.File
import com.karumi.shot.domain.{Dimension, Screenshot, ScreenshotsComparisionResult, ShotFolder}
import com.karumi.shot.domain.model.{FilePath, Folder, ScreenshotsSuite}
import com.karumi.shot.domain.{Dimension, Screenshot, ScreenshotsComparisionResult, ShotFolder}
import com.sksamuel.scrimage.ImmutableImage
import com.sksamuel.scrimage.nio.PngWriter
import org.apache.commons.io.FileUtils

import java.io.File

class ScreenshotsSaver {

def saveRecordedScreenshots(
to: FilePath,
screenshots: ScreenshotsSuite
) = {
): Unit = {
deleteFile(to)
saveScreenshots(screenshots, to)
}

private def saveScreenshots(screenshots: ScreenshotsSuite, folder: Folder) = {
val screenshotsFolder = new File(folder)
if (!screenshotsFolder.exists()) {
screenshotsFolder.mkdirs()
}
screenshots.foreach { screenshot =>
val outputFile = new File(folder + screenshot.fileName)
if (!outputFile.exists()) {
outputFile.createNewFile()
}
val image = ScreenshotComposer.composeNewScreenshot(screenshot)
image.output(PngWriter.MaxCompression, outputFile)
}
}

private def deleteFile(path: String): Unit = {
val folder = new File(path)
if (folder.exists()) {
folder.delete()
}
}

def saveTemporalScreenshots(
screenshots: ScreenshotsSuite,
projectName: String,
Expand All @@ -29,6 +52,10 @@ class ScreenshotsSaver {
saveScreenshots(screenshots, reportFolder)
}

private def deleteOldTemporalScreenshots(projectName: String, shotFolder: ShotFolder): Unit = {
deleteFile(shotFolder.screenshotsTemporalBuildPath() + "/")
}

def copyRecordedScreenshotsToTheReportFolder(
from: FilePath,
to: FilePath
Expand All @@ -45,6 +72,11 @@ class ScreenshotsSaver {
deleteFile(destinyFolder)
}

private def copyFile(screenshot: Screenshot, destinyFolder: Folder): Unit = {
val existingScreenshot = new File(screenshot.recordedScreenshotPath)
FileUtils.copyFile(existingScreenshot, new File(destinyFolder + existingScreenshot.getName))
}

def removeNonFailingReferenceImages(
verificationReferenceImagesFolder: Folder,
screenshotsResult: ScreenshotsComparisionResult
Expand All @@ -53,44 +85,14 @@ class ScreenshotsSaver {
deleteFile(verificationReferenceImagesFolder + screenshot.fileName)
)

private def copyFile(screenshot: Screenshot, destinyFolder: Folder): Unit = {
val existingScreenshot = new File(screenshot.recordedScreenshotPath)
FileUtils.copyFile(existingScreenshot, new File(destinyFolder + existingScreenshot.getName))
}

def getScreenshotDimension(
shotFolder: ShotFolder,
screenshot: Screenshot
): Dimension = {
val screenshotPath = shotFolder.pulledScreenshotsFolder() + screenshot.name + ".png"
val image = ImmutableImage.loader().fromFile(new File(screenshotPath))
val screenshotPath = shotFolder
.pulledScreenshotsFolder() + screenshot.name + ".png"
val image = ImmutableImage.loader().fromFile(new File(screenshotPath))
Dimension(image.width, image.height)
}

private def deleteOldTemporalScreenshots(projectName: String, shotFolder: ShotFolder): Unit = {
deleteFile(shotFolder.screenshotsTemporalBuildPath() + "/")
}

private def deleteFile(path: String): Unit = {
val folder = new File(path)
if (folder.exists()) {
folder.delete()
}
}

private def saveScreenshots(screenshots: ScreenshotsSuite, folder: Folder) = {
val screenshotsFolder = new File(folder)
if (!screenshotsFolder.exists()) {
screenshotsFolder.mkdirs()
}
screenshots.foreach { screenshot =>
val outputFile = new File(folder + screenshot.fileName)
if (!outputFile.exists()) {
outputFile.createNewFile()
}
val image = ScreenshotComposer.composeNewScreenshot(screenshot)
image.output(PngWriter.MaxCompression, outputFile)
}
}

}

0 comments on commit 392ebed

Please sign in to comment.