From 30dcf81cc2949f1fa5e4b82f6774113aba4954bb Mon Sep 17 00:00:00 2001 From: Petter Olofsson Date: Wed, 7 Feb 2024 22:07:32 +0100 Subject: [PATCH] Reformat code --- .../shot/reports/HtmlExecutionReporter.scala | 31 +++---- .../shot/reports/JunitExecutionReporter.scala | 88 +++++++++---------- .../ScreenshotsDiffGenerator.scala | 7 +- .../shot/screenshots/ScreenshotsSaver.scala | 74 ++++++++-------- gradle.properties | 2 +- 5 files changed, 104 insertions(+), 98 deletions(-) diff --git a/core/src/main/scala/com/karumi/shot/reports/HtmlExecutionReporter.scala b/core/src/main/scala/com/karumi/shot/reports/HtmlExecutionReporter.scala index 629d63a9..823a4a77 100644 --- a/core/src/main/scala/com/karumi/shot/reports/HtmlExecutionReporter.scala +++ b/core/src/main/scala/com/karumi/shot/reports/HtmlExecutionReporter.scala @@ -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( @@ -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 @@ -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, diff --git a/core/src/main/scala/com/karumi/shot/reports/JunitExecutionReporter.scala b/core/src/main/scala/com/karumi/shot/reports/JunitExecutionReporter.scala index eb9abee7..5677f6b9 100644 --- a/core/src/main/scala/com/karumi/shot/reports/JunitExecutionReporter.scala +++ b/core/src/main/scala/com/karumi/shot/reports/JunitExecutionReporter.scala @@ -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) @@ -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) @@ -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) @@ -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"""""" + s"""""" } else { "" } s""" - | $failureString - |""".stripMargin - }).mkString("\n") + | $failureString + |""".stripMargin + }) + .mkString("\n") - s""" - | ${tests} + s""" + | $tests | |""".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 = diff --git a/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsDiffGenerator.scala b/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsDiffGenerator.scala index 5e7b0a99..248f45be 100644 --- a/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsDiffGenerator.scala +++ b/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsDiffGenerator.scala @@ -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} @@ -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) { @@ -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) diff --git a/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsSaver.scala b/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsSaver.scala index 550416af..f789f991 100644 --- a/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsSaver.scala +++ b/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsSaver.scala @@ -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, @@ -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 @@ -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 @@ -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) - } - } - } diff --git a/gradle.properties b/gradle.properties index 149de2cd..3e689f53 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=6.1.0 +VERSION_NAME=6.0.0 VERSION_CODE=600000 GROUP=com.karumi POM_DESCRIPTION=Gradle plugin developed to facilitate screenshot testing for Android.