Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error messaging and added tests. #11

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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 {
Expand All @@ -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()
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}

}