Skip to content

Commit

Permalink
refactor(fossid): Align filterLegacyRules with Kotlin standards
Browse files Browse the repository at this point in the history
`filter` functions in Kotlin usually filter elements of the collection
they are called on. Align `filterLegacyRules` with that approach to
prevent confusion.

Signed-off-by: Martin Nonnenmacher <[email protected]>
  • Loading branch information
mnonnenmacher committed Jun 20, 2024
1 parent c13102c commit f19e8d9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/scanners/fossid/src/main/kotlin/FossId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ class FossId internal constructor(
}.orEmpty()

// Create an issue for each legacy rule.
val legacyRules = excludesRules.filterLegacyRules(existingRules, issues)
val legacyRules = existingRules.filterLegacyRules(excludesRules, issues)
if (legacyRules.isNotEmpty()) {
logger.warn { "${legacyRules.size} legacy rules have been found." }
}
Expand Down
11 changes: 6 additions & 5 deletions plugins/scanners/fossid/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ private fun PathExclude.mapToRule(): IgnoreRule? {
}

/**
* Check if some elements of [rulesToTest] are legacy rules i.e. are not present in a reference list (current object).
* Create an issue on [issues] for each legacy rule and return a list of the latter.
* Filter [IgnoreRule]s which are not contained in the [referenceRules]. These are legacy rules because they were not
* created from the [Excludes] defined in the repository configuration. Also create an [Issue] for each legacy rule and
* add it to [issues].
*/
internal fun List<IgnoreRule>.filterLegacyRules(
rulesToTest: List<IgnoreRule>,
referenceRules: List<IgnoreRule>,
issues: MutableList<Issue>
): List<IgnoreRule> =
rulesToTest.filterNot { ruleToTest ->
any { it.value == ruleToTest.value && it.type == ruleToTest.type }
filterNot { rule ->
referenceRules.any { it.value == rule.value && it.type == rule.type }
}.onEach {
issues += Issue(
source = "FossID.compare",
Expand Down
4 changes: 2 additions & 2 deletions plugins/scanners/fossid/src/test/kotlin/LegacyRulesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class LegacyRulesTest : WordSpec({
)
val issues = mutableListOf<Issue>()

val legacyRules = referenceRules.filterLegacyRules(rulesToTest, issues)
val legacyRules = rulesToTest.filterLegacyRules(referenceRules, issues)

issues shouldHaveSize 1
issues.first() shouldNotBeNull {
Expand All @@ -65,7 +65,7 @@ class LegacyRulesTest : WordSpec({
)
val issues = mutableListOf<Issue>()

val legacyRules = referenceRules.filterLegacyRules(rulesToTest, issues)
val legacyRules = rulesToTest.filterLegacyRules(referenceRules, issues)

issues should beEmpty()
legacyRules should beEmpty()
Expand Down

0 comments on commit f19e8d9

Please sign in to comment.