Skip to content

Commit ffbf0e6

Browse files
committed
Verify if baseline is outdated
1 parent 32ce913 commit ffbf0e6

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

projectguard/src/main/kotlin/com/rubensousa/projectguard/plugin/internal/SuppressionMap.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ internal class SuppressionMap {
3131
}
3232
}
3333

34+
fun isOutdated(graph: DependencyGraph): Boolean {
35+
suppressions.forEach { (module, suppressions) ->
36+
val graphDependencies = graph.getDependencies(module)
37+
.associateBy { dependency -> dependency.id }
38+
.keys
39+
suppressions.forEach { (dependency, _) ->
40+
if (!graphDependencies.contains(dependency)) {
41+
return true
42+
}
43+
}
44+
}
45+
return false
46+
}
47+
3448
fun add(module: String, dependency: String, reason: String = UNSPECIFIED_REASON) {
3549
add(module, DependencySuppression(dependency, reason))
3650
}

projectguard/src/main/kotlin/com/rubensousa/projectguard/plugin/internal/task/TaskCheck.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.rubensousa.projectguard.plugin.internal.task
1818

1919
import com.rubensousa.projectguard.plugin.internal.BaselineConfiguration
20+
import com.rubensousa.projectguard.plugin.internal.DependencyGraphBuilder
2021
import com.rubensousa.projectguard.plugin.internal.ReportSpec
2122
import com.rubensousa.projectguard.plugin.internal.SuppressionMap
2223
import com.rubensousa.projectguard.plugin.internal.YamlProcessor
@@ -84,6 +85,7 @@ internal class CheckExecutor(
8485

8586
fun execute(): Result<Unit> = runCatching {
8687
val dependencyGraphDump = Json.decodeFromString<DependencyGraphDump>(dependenciesFile.readText())
88+
val graph = DependencyGraphBuilder().buildFromDump(dependencyGraphDump)
8789
val yamlProcessor = YamlProcessor()
8890
val suppressionMap = SuppressionMap()
8991
runCatching {
@@ -93,6 +95,12 @@ internal class CheckExecutor(
9395
}.onFailure {
9496
println("Skipping baseline since it could not be found or was improperly structured!")
9597
}
98+
if (suppressionMap.isOutdated(graph)) {
99+
throw VerificationException(
100+
"Baseline contains dependencies that are no longer relevant, " +
101+
"you need to update it with the task projectGuardBaseline"
102+
)
103+
}
96104
val restrictionDump = Json.decodeFromString<RestrictionDump>(restrictionDumpFile.readText())
97105
val reportBuilder = VerificationReportBuilder(suppressionMap, reportSpec)
98106
val report = reportBuilder.build(dependencyGraphDump, restrictionDump)

projectguard/src/test/kotlin/com/rubensousa/projectguard/plugin/internal/SuppressionMapTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.junit.Test
2222
class SuppressionMapTest {
2323

2424
private val suppressionMap = SuppressionMap()
25+
private val dependencyGraph = DependencyGraph()
2526

2627
@Test
2728
fun `getSuppression returns null if dependency is not suppressed`() {
@@ -117,4 +118,34 @@ class SuppressionMapTest {
117118
assertThat(newLibSuppression?.reason).isEqualTo("AnotherReason")
118119
}
119120

121+
@Test
122+
fun `isOutdated returns false for empty suppressions`() {
123+
// given
124+
dependencyGraph.addInternalDependency(module = ":domain", dependency = ":legacy")
125+
126+
// then
127+
assertThat(suppressionMap.isOutdated(dependencyGraph)).isFalse()
128+
}
129+
130+
@Test
131+
fun `isOutdated returns false for dependencies that still exist`() {
132+
// given
133+
dependencyGraph.addInternalDependency(module = ":domain", dependency = ":legacy")
134+
135+
// when
136+
suppressionMap.add(module = ":domain", dependency = ":legacy")
137+
138+
// then
139+
assertThat(suppressionMap.isOutdated(dependencyGraph)).isFalse()
140+
}
141+
142+
@Test
143+
fun `isOutdated returns true for dependencies that no longer exist`() {
144+
// given
145+
suppressionMap.add(module = ":domain", dependency = ":legacy")
146+
147+
// then
148+
assertThat(suppressionMap.isOutdated(dependencyGraph)).isTrue()
149+
}
150+
120151
}

0 commit comments

Comments
 (0)