Skip to content

Commit

Permalink
Add change build dir test
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Feb 23, 2024
1 parent 586fbde commit 50236c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
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

0 comments on commit 50236c4

Please sign in to comment.