From e4e7832861259d0359332727f608c254f3260390 Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Wed, 27 Apr 2022 21:49:26 -0400 Subject: [PATCH] Fixed #2 in another spot. Added tests. --- .../DependencyGuardListReportWriter.kt | 6 +- .../internal/utils/DependencyListDiff.kt | 21 +++--- .../internal/utils/DependencyListDiffTest.kt | 74 +++++++++++++++++++ 3 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 dependency-guard/src/test/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiffTest.kt diff --git a/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/DependencyGuardListReportWriter.kt b/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/DependencyGuardListReportWriter.kt index 338c674..5365136 100644 --- a/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/DependencyGuardListReportWriter.kt +++ b/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/DependencyGuardListReportWriter.kt @@ -38,13 +38,13 @@ internal class DependencyGuardListReportWriter( ) false } else { + val expectedFileContent = projectDirOutputFile.readText() // Perform Diff DependencyListDiff.performDiff( - type = type, projectPath = report.projectPath, configurationName = report.configurationName, - projectDirOutputFile = projectDirOutputFile, - depsReportString = reportContent, + expectedDependenciesFileContent = expectedFileContent, + actualDependenciesFileContent = reportContent, errorHandler = errorHandler ) } diff --git a/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiff.kt b/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiff.kt index 7d48064..bc39f3e 100644 --- a/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiff.kt +++ b/dependency-guard/src/main/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiff.kt @@ -1,8 +1,6 @@ package com.dropbox.gradle.plugins.dependencyguard.internal.utils -import com.dropbox.gradle.plugins.dependencyguard.DependencyGuardPlugin -import com.dropbox.gradle.plugins.dependencyguard.internal.DependencyGuardReportType -import java.io.File +import com.dropbox.gradle.plugins.dependencyguard.internal.getQualifiedBaselineTaskForProjectPath internal object DependencyListDiff { @@ -11,23 +9,22 @@ internal object DependencyListDiff { */ @Suppress("LongParameterList") fun performDiff( - type: DependencyGuardReportType, projectPath: String, configurationName: String, - projectDirOutputFile: File, - depsReportString: String, + expectedDependenciesFileContent:String, + actualDependenciesFileContent: String, errorHandler: (String) -> Unit, ): Boolean { // Compare - val expectedFileContent = projectDirOutputFile.readText() - val expectedLines = expectedFileContent.lines() - val difference = compare(expectedLines, depsReportString.lines()) + val expectedLines = expectedDependenciesFileContent.lines() + val difference = compare(expectedLines, actualDependenciesFileContent.lines()) if (difference.isNotBlank()) { + val dependenciesChangedMessage = """Dependencies Changed in "$projectPath" for configuration "$configurationName"""" val errorMessage = StringBuilder().apply { appendLine( ColorTerminal.printlnColor( ColorTerminal.ANSI_YELLOW, - "$type Dependencies Changed $projectPath for configuration \"$configurationName\":" + dependenciesChangedMessage ) ) difference.lines().forEach { @@ -45,8 +42,8 @@ internal object DependencyListDiff { ColorTerminal.printlnColor( ColorTerminal.ANSI_RED, """ - $type Dependencies Changed in $projectPath for configuration "$configurationName" - If this is intentional, re-baseline using $projectPath:${DependencyGuardPlugin.DEPENDENCY_GUARD_BASELINE_TASK_NAME} + $dependenciesChangedMessage + If this is intentional, re-baseline using ./gradlew ${getQualifiedBaselineTaskForProjectPath(projectPath)} """.trimIndent() ) ) diff --git a/dependency-guard/src/test/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiffTest.kt b/dependency-guard/src/test/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiffTest.kt new file mode 100644 index 0000000..fcc7885 --- /dev/null +++ b/dependency-guard/src/test/kotlin/com/dropbox/gradle/plugins/dependencyguard/internal/utils/DependencyListDiffTest.kt @@ -0,0 +1,74 @@ +package com.dropbox.gradle.plugins.dependencyguard.internal.utils + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test + +internal class DependencyListDiffTest { + + @Test + fun performDiffTestRootProject() { + val errorSb = StringBuilder() + + DependencyListDiff.performDiff( + projectPath = ":", + configurationName = "classpath", + expectedDependenciesFileContent = """ + :sample:module2 + androidx.activity:activity:1.3.1 + """.trimIndent(), + actualDependenciesFileContent = """ + :sample:module2 + androidx.activity:activity:1.4.0 + """.trimIndent(), + errorHandler = { errorSb.append(it) } + ) + + val actual = errorSb.toString() + val expected = """ + Dependencies Changed in ":" for configuration "classpath" + - androidx.activity:activity:1.3.1 + + androidx.activity:activity:1.4.0 + + Dependencies Changed in ":" for configuration "classpath" + If this is intentional, re-baseline using ./gradlew :dependencyGuardBaseline + + """.trimIndent() + + assertThat(actual) + .isEqualTo(expected) + } + + @Test + fun performDiffTestModule() { + val errorSb = StringBuilder() + + DependencyListDiff.performDiff( + projectPath = ":sample:app", + configurationName = "classpath", + expectedDependenciesFileContent = """ + :sample:module2 + androidx.activity:activity:1.3.1 + """.trimIndent(), + actualDependenciesFileContent = """ + :sample:module2 + androidx.activity:activity:1.4.0 + """.trimIndent(), + errorHandler = { errorSb.append(it) } + ) + + val actual = errorSb.toString() + val expected = """ + Dependencies Changed in ":sample:app" for configuration "classpath" + - androidx.activity:activity:1.3.1 + + androidx.activity:activity:1.4.0 + + Dependencies Changed in ":sample:app" for configuration "classpath" + If this is intentional, re-baseline using ./gradlew :sample:app:dependencyGuardBaseline + + """.trimIndent() + + assertThat(actual) + .isEqualTo(expected) + } + +}