Skip to content

Commit

Permalink
Merge pull request #270 from takahirom/takahirom/respect-gradle-build…
Browse files Browse the repository at this point in the history
…-dir/2024-02-23

Respect gradle build dir for reports
  • Loading branch information
takahirom authored Feb 23, 2024
2 parents 32e4747 + 50236c4 commit 44498e3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class RoborazziContextImpl {
override fun toString(): String {
return """
RoborazziContext(
outputDirectory=$outputDirectory,
ruleOverrideOutputDirectory=$ruleOverrideOutputDirectory,
ruleOverrideRoborazziOptions=$ruleOverrideRoborazziOptions,
ruleOverrideFileProvider=$ruleOverrideFileProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ fun roborazziSystemPropertyOutputDirectory(): String {
return System.getProperty("roborazzi.output.dir", DEFAULT_ROBORAZZI_OUTPUT_DIR_PATH)
}

@ExperimentalRoborazziApi
fun roborazziSystemPropertyResultDirectory(): String {
return System.getProperty("roborazzi.result.dir",
"build/${RoborazziReportConst.resultDirPathFromBuildDir}"
)
}

@ExperimentalRoborazziApi
// This will be removed when we found if this is safe.
fun roborazziEnableContextData(): Boolean {
Expand Down Expand Up @@ -235,11 +242,11 @@ data class RoborazziOptions(
class JsonOutputCaptureResultReporter : CaptureResultReporter {

init {
File(RoborazziReportConst.resultDirPath).mkdirs()
File(roborazziSystemPropertyResultDirectory()).mkdirs()
}

override fun report(captureResult: CaptureResult, roborazziTaskType: RoborazziTaskType) {
val absolutePath = File(RoborazziReportConst.resultDirPath).absolutePath
val absolutePath = File(roborazziSystemPropertyResultDirectory()).absolutePath
val nameWithoutExtension = when (captureResult) {
is CaptureResult.Added -> captureResult.compareFile
is CaptureResult.Changed -> captureResult.goldenFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
package com.github.takahirom.roborazzi

@InternalRoborazziApi
object RoborazziReportConst {
@Deprecated(
message = "Use resultsSummaryFilePathFromBuildDir instead",
replaceWith = ReplaceWith("resultsSummaryFilePathFromBuildDir"),
level = DeprecationLevel.ERROR
)
const val resultsSummaryFilePath = "build/test-results/roborazzi/results-summary.json"
@Deprecated(
message = "Use resultDirPathFromBuildDir instead",
replaceWith = ReplaceWith("resultDirPathFromBuildDir"),
level = DeprecationLevel.ERROR
)
const val resultDirPath = "build/test-results/roborazzi/results/"
@Deprecated(
message = "Use reportFilePathFromBuildDir instead",
replaceWith = ReplaceWith("reportFilePathFromBuildDir"),
level = DeprecationLevel.ERROR
)
const val reportFilePath = "build/reports/roborazzi/index.html"

const val resultsSummaryFilePathFromBuildDir = "test-results/roborazzi/results-summary.json"
const val resultDirPathFromBuildDir = "test-results/roborazzi/results/"
const val reportFilePathFromBuildDir = "reports/roborazzi/index.html"

sealed interface DefaultContextData {
val key: String
val title: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.junit.rules.TemporaryFolder
import java.io.File

class RoborazziGradleProject(val testProjectDir: TemporaryFolder) {
var buildDirName = "build"
init {
File("./src/integrationTest/projects").copyRecursively(testProjectDir.root, true)
}
Expand Down Expand Up @@ -103,12 +104,12 @@ class RoborazziGradleProject(val testProjectDir: TemporaryFolder) {
}

fun removeRoborazziOutputDir() {
File(testProjectDir.root, "app/build/outputs/roborazzi").deleteRecursively()
File(testProjectDir.root, "app/$buildDirName/outputs/roborazzi").deleteRecursively()
}

fun removeRoborazziAndIntermediateOutputDir() {
File(testProjectDir.root, "app/build/outputs/roborazzi").deleteRecursively()
File(testProjectDir.root, "app/build/intermediates/roborazzi").deleteRecursively()
File(testProjectDir.root, "app/$buildDirName/outputs/roborazzi").deleteRecursively()
File(testProjectDir.root, "app/$buildDirName/intermediates/roborazzi").deleteRecursively()
}

fun assertNotSkipped(output: String) {
Expand Down Expand Up @@ -278,13 +279,13 @@ dependencies {
}

fun checkResultFileExists(nameSuffix: String) {
testProjectDir.root.resolve("app/build/test-results/roborazzi/results/").listFiles()
testProjectDir.root.resolve("app/$buildDirName/test-results/roborazzi/results/").listFiles()
.firstOrNull { it.name.endsWith(nameSuffix) }
?: error("File not found: $nameSuffix")
}

fun checkResultFileNotExists(nameSuffix: String) {
testProjectDir.root.resolve("app/build/test-results/roborazzi/results/").listFiles()
testProjectDir.root.resolve("app/$buildDirName/test-results/roborazzi/results/").listFiles()
?.firstOrNull { it.name.endsWith(nameSuffix) }
?.let {
error("File exists: $nameSuffix")
Expand All @@ -294,15 +295,15 @@ dependencies {

fun checkResultsSummaryFileNotExists() {
val recordedFile =
testProjectDir.root.resolve("app/build/test-results/roborazzi/results-summary.json")
testProjectDir.root.resolve("app/$buildDirName/test-results/roborazzi/results-summary.json")
assert(!recordedFile.exists()) {
"File exists: ${recordedFile.absolutePath}"
}
}

fun checkResultsSummaryFileExists() {
val recordedFile =
testProjectDir.root.resolve("app/build/test-results/roborazzi/results-summary.json")
testProjectDir.root.resolve("app/$buildDirName/test-results/roborazzi/results-summary.json")
assert(recordedFile.exists()) {
"File not exists: ${recordedFile.absolutePath}"
}
Expand All @@ -315,7 +316,7 @@ dependencies {
unchanged: Int = 0
) {
val recordedFile =
testProjectDir.root.resolve("app/build/test-results/roborazzi/results-summary.json")
testProjectDir.root.resolve("app/$buildDirName/test-results/roborazzi/results-summary.json")
val resutls = CaptureResults.fromJsonFile(recordedFile.absolutePath)
assert(resutls.resultSummary.recorded == recorded) {
"Expected count: $recorded, actual count: ${resutls.resultSummary.recorded} summary:${resutls.resultSummary}"
Expand Down Expand Up @@ -350,6 +351,13 @@ dependencies {
}
}

fun changeBuildDir(buildDirName: String) {
testProjectDir.root.resolve("gradle.properties").appendText(
"\nbuildDir=$buildDirName"
)
this.buildDirName = buildDirName
}

fun changeScreen() {
val file =
testProjectDir.root.resolve("app/src/main/java/com/github/takahirom/integration_test_project/MainActivity.kt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder


/**
* Run this test with `cd include-build` and `./gradlew roborazzi-gradle-plugin:check`
*/
Expand All @@ -14,23 +13,32 @@ class RoborazziGradleProjectTest {
val testProjectDir = TemporaryFolder()

private val className = "com.github.takahirom.integration_test_project.RoborazziTest"
private val screenshotAndName =
"app/build/outputs/roborazzi/$className"

private val defaultRoborazziOutputDir = "build/outputs/roborazzi"
private val customReferenceScreenshotAndName =
"app/$defaultRoborazziOutputDir/customdir/custom_file"
private val customCompareScreenshotAndName =
"app/$defaultRoborazziOutputDir/custom_compare_outputDirectoryPath/custom_file"
private val customReferenceScreenshotAndNameWithRoborazziContext =
"app/$defaultRoborazziOutputDir/custom_outputDirectoryPath_from_rule/$defaultRoborazziOutputDir/customdir/custom_file"
private val customCompareScreenshotAndNameWithRoborazziContext =
"app/$defaultRoborazziOutputDir/custom_compare_outputDirectoryPath/custom_file"
private var defaultBuildDir = "build"

private val screenshotAndName
get() =
"app/$defaultBuildDir/outputs/roborazzi/$className"

private val defaultRoborazziOutputDir get() = "$defaultBuildDir/outputs/roborazzi"
private val customReferenceScreenshotAndName
get() =
"app/$defaultRoborazziOutputDir/customdir/custom_file"
private val customCompareScreenshotAndName
get() =
"app/$defaultRoborazziOutputDir/custom_compare_outputDirectoryPath/custom_file"
private val customReferenceScreenshotAndNameWithRoborazziContext
get() =
"app/$defaultRoborazziOutputDir/custom_outputDirectoryPath_from_rule/$defaultRoborazziOutputDir/customdir/custom_file"
private val customCompareScreenshotAndNameWithRoborazziContext
get() =
"app/$defaultRoborazziOutputDir/custom_compare_outputDirectoryPath/custom_file"

private val addedScreenshotAndName =
"app/$defaultRoborazziOutputDir/com.github.takahirom.integration_test_project.AddedRoborazziTest"
private val addedScreenshotAndName
get() =
"app/$defaultRoborazziOutputDir/com.github.takahirom.integration_test_project.AddedRoborazziTest"

private val resultFileSuffix = "$className.testCapture.json"
private val resultFileSuffix get() = "$className.testCapture.json"

@Test
fun record() {
Expand Down Expand Up @@ -118,7 +126,6 @@ class RoborazziGradleProjectTest {
}
}


@Test
fun unitTestWhenRunTwice() {
RoborazziGradleProject(testProjectDir).apply {
Expand Down Expand Up @@ -284,8 +291,11 @@ class RoborazziGradleProjectTest {
}

@Test
fun verify_nochange() {
fun verify_nochange_with_changed_build_dir() {
RoborazziGradleProject(testProjectDir).apply {
val buildDirName = "testCustomBuildDirName"
changeBuildDir(buildDirName)
defaultBuildDir = buildDirName
record()
verify()

Expand All @@ -294,10 +304,10 @@ class RoborazziGradleProjectTest {
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
checkResultCount(unchanged = 1)
}
}


@Test
fun verifyAndRecord_changeDetect() {
RoborazziGradleProject(testProjectDir).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.github.takahirom.roborazzi.CaptureResult
import com.github.takahirom.roborazzi.CaptureResults
import com.github.takahirom.roborazzi.InternalRoborazziApi
import com.github.takahirom.roborazzi.RoborazziReportConst
import org.gradle.api.Action
import org.gradle.api.DefaultTask
Expand Down Expand Up @@ -48,6 +49,7 @@ open class RoborazziExtension @Inject constructor(objects: ObjectFactory) {
abstract class RoborazziPlugin : Plugin<Project> {
@Inject abstract fun getEventsListenerRegistry(): BuildEventsListenerRegistry

@OptIn(InternalRoborazziApi::class)
override fun apply(project: Project) {
val extension = project.extensions.create("roborazzi", RoborazziExtension::class.java)

Expand Down Expand Up @@ -183,16 +185,16 @@ abstract class RoborazziPlugin : Plugin<Project> {
}
}
val outputDirRelativePathFromProjectProvider = outputDir.map { project.relativePath(it) }
val resultDirFileTree =
project.fileTree(RoborazziReportConst.resultDirPath)
val resultDirFileProperty =
project.layout.projectDirectory.dir(
RoborazziReportConst.resultDirPath
)
project.layout.buildDirectory.dir(RoborazziReportConst.resultDirPathFromBuildDir)
val resultDirFileTree =
resultDirFileProperty.map { it.asFileTree }
val resultDirRelativePathFromProjectProvider =
resultDirFileProperty.map { project.relativePath(it) }
val resultSummaryFileProperty =
project.layout.projectDirectory.file(RoborazziReportConst.resultsSummaryFilePath)
project.layout.buildDirectory.file(RoborazziReportConst.resultsSummaryFilePathFromBuildDir)
val reportFileProperty =
project.layout.projectDirectory.file(RoborazziReportConst.reportFilePath)
project.layout.buildDirectory.file(RoborazziReportConst.reportFilePathFromBuildDir)

val finalizeTestRoborazziTask = project.tasks.register(
/* name = */ "finalizeTestRoborazzi$variantSlug",
Expand All @@ -217,22 +219,22 @@ abstract class RoborazziPlugin : Plugin<Project> {
)
}

val results: List<CaptureResult> = resultDirFileTree.mapNotNull {
val results: List<CaptureResult> = resultDirFileTree.get().mapNotNull {
if (it.name.endsWith(".json")) {
CaptureResult.fromJsonFile(it.path)
} else {
null
}
}
val resultsSummaryFile = resultSummaryFileProperty.asFile
val resultsSummaryFile = resultSummaryFileProperty.get().asFile

val roborazziResults = CaptureResults.from(results)
t.infoln("Roborazzi: Save result to ${resultsSummaryFile.absolutePath} with results:${results.size} summary:${roborazziResults.resultSummary}")

val jsonResult = roborazziResults.toJson()
resultsSummaryFile.parentFile.mkdirs()
resultsSummaryFile.writeText(jsonResult)
val reportFile = reportFileProperty.asFile
val reportFile = reportFileProperty.get().asFile
reportFile.parentFile.mkdirs()
reportFile.writeText(
RoborazziReportConst.reportHtml.replace(
Expand All @@ -245,7 +247,7 @@ abstract class RoborazziPlugin : Plugin<Project> {
})
testTaskProvider
.configureEach { test ->
val resultsDir = project.file(RoborazziReportConst.resultDirPath)
val resultsDir = resultDirFileProperty.get().asFile
if (restoreOutputDirRoborazziTaskProvider.isPresent) {
test.inputs.dir(restoreOutputDirRoborazziTaskProvider.map {
if (!it.outputDir.get().asFile.exists()) {
Expand Down Expand Up @@ -314,14 +316,17 @@ abstract class RoborazziPlugin : Plugin<Project> {
!key.startsWith("roborazzi.test")
}
)
test.systemProperties["roborazzi.output.dir"] =
outputDirRelativePathFromProjectProvider.get()
test.systemProperties["roborazzi.test.record"] =
isRecordRun.get() || isVerifyAndRecordRun.get()
test.systemProperties["roborazzi.test.compare"] = isCompareRun.get()
test.systemProperties["roborazzi.test.verify"] =
isVerifyRun.get() || isVerifyAndRecordRun.get()
}
test.systemProperties["roborazzi.output.dir"] =
outputDirRelativePathFromProjectProvider.get()
test.systemProperties["roborazzi.result.dir"] =
resultDirRelativePathFromProjectProvider.get()
test.infoln("Roborazzi: Plugin passed system properties " + test.systemProperties + " to the test")
resultsDir.deleteRecursively()
resultsDir.mkdirs()
}
Expand Down

0 comments on commit 44498e3

Please sign in to comment.