Skip to content

Commit fd71440

Browse files
committed
refactor(scanner)!: Remove the unused ScannerConfigMatcher
The `ScannerConfigMatcher` was intended to support cases where the `ScannerDetails.configuration` must not be matched exactly. As the feature was not taken into use since its introduction three years ago, replace it with a simpler approach to match either the whole configuration or ignore the configuration to simplify code. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent a84a1f4 commit fd71440

File tree

3 files changed

+27
-61
lines changed

3 files changed

+27
-61
lines changed

scanner/src/main/kotlin/ScannerCriteria.kt

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ import org.ossreviewtoolkit.model.config.Options
2525
import org.semver4j.Semver
2626
import org.semver4j.Semver.VersionDiff
2727

28-
/**
29-
* Definition of a predicate to check whether the configuration of a scanner is compatible with the requirements
30-
* specified by a [ScannerCriteria].
31-
*
32-
* When testing whether a scan result is compatible with specific criteria this function is invoked on the
33-
* scanner configuration data stored in the result. By having different, scanner-specific matcher functions, this
34-
* compatibility check can be made very flexible.
35-
*
36-
* TODO: Switch to a more advanced type than String to represent the scanner configuration.
37-
*/
38-
typealias ScannerConfigMatcher = (String) -> Boolean
39-
4028
/**
4129
* A data class defining selection criteria for scanners.
4230
*
@@ -69,17 +57,12 @@ data class ScannerCriteria(
6957
val maxVersion: Semver,
7058

7159
/**
72-
* A function to check whether the configuration of a scanner is compatible with this [ScannerCriteria].
60+
* Criterion to match the [configuration][ScannerDetails.configuration] of the scanner. If `null`, all
61+
* configurations are matched.
7362
*/
74-
val configMatcher: ScannerConfigMatcher
63+
val configuration: String?
7564
) {
7665
companion object {
77-
/**
78-
* A matcher for scanner configurations that accepts all configurations passed in. This function can be
79-
* used if the concrete configuration of a scanner is irrelevant.
80-
*/
81-
val ALL_CONFIG_MATCHER: ScannerConfigMatcher = { true }
82-
8366
/**
8467
* The name of the property defining the regular expression for the scanner name as part of [ScannerCriteria].
8568
* The [scanner details][ScannerDetails] of the corresponding scanner must match the criteria.
@@ -99,10 +82,10 @@ data class ScannerCriteria(
9982
const val PROP_CRITERIA_MAX_VERSION = "maxVersion"
10083

10184
/**
102-
* A matcher for scanner configurations that accepts only exact matches of the [originalConfig]. This
103-
* function can be used by scanners that are extremely sensitive about their configuration.
85+
* The name of the property defining the configuration of the scanner as part of [ScannerCriteria]. The
86+
* [scanner details][ScannerDetails] of the corresponding scanner must match the criteria.
10487
*/
105-
fun exactConfigMatcher(originalConfig: String): ScannerConfigMatcher = { config -> originalConfig == config }
88+
const val PROP_CRITERIA_CONFIGURATION = "configuration"
10689

10790
/**
10891
* Generate a [ScannerCriteria] instance that is compatible with the given [details] and versions that differ
@@ -122,7 +105,7 @@ data class ScannerCriteria(
122105
regScannerName = details.name,
123106
minVersion = minVersion,
124107
maxVersion = maxVersion,
125-
configMatcher = exactConfigMatcher(details.configuration)
108+
configuration = details.configuration
126109
)
127110
}
128111

@@ -138,8 +121,9 @@ data class ScannerCriteria(
138121
val minVersion = parseVersion(options[PROP_CRITERIA_MIN_VERSION]) ?: scannerVersion
139122
val maxVersion = parseVersion(options[PROP_CRITERIA_MAX_VERSION]) ?: minVersion.nextMinor()
140123
val name = options[PROP_CRITERIA_NAME] ?: details.name
124+
val configuration = options[PROP_CRITERIA_CONFIGURATION] ?: details.configuration
141125

142-
return ScannerCriteria(name, minVersion, maxVersion, exactConfigMatcher(details.configuration))
126+
return ScannerCriteria(name, minVersion, maxVersion, configuration)
143127
}
144128
}
145129

@@ -160,7 +144,8 @@ data class ScannerCriteria(
160144
if (!nameRegex.matches(details.name)) return false
161145

162146
val version = Semver(details.version)
163-
return minVersion <= version && version < maxVersion && configMatcher(details.configuration)
147+
return minVersion <= version && version < maxVersion &&
148+
(configuration == null || configuration == details.configuration)
164149
}
165150
}
166151

scanner/src/test/kotlin/ScannerCriteriaTest.kt

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,6 @@ import org.ossreviewtoolkit.model.ScannerDetails
2727
import org.semver4j.Semver
2828

2929
class ScannerCriteriaTest : WordSpec({
30-
"ScannerCriteria" should {
31-
"provide a config matcher that accepts every configuration" {
32-
ScannerCriteria.ALL_CONFIG_MATCHER("") shouldBe true
33-
ScannerCriteria.ALL_CONFIG_MATCHER("foo") shouldBe true
34-
ScannerCriteria.ALL_CONFIG_MATCHER("Supercalifragilisticexpialidocious") shouldBe true
35-
}
36-
37-
"provide a config matcher that accepts only exact configuration matches" {
38-
val orgConfig = "--info --copyright --licenses"
39-
val matcher = ScannerCriteria.exactConfigMatcher(orgConfig)
40-
41-
matcher(orgConfig) shouldBe true
42-
matcher("$orgConfig --more") shouldBe false
43-
}
44-
}
45-
4630
"ScannerCriteria.forDetails()" should {
4731
"create criteria that only match the passed details by default" {
4832
val criteria = ScannerCriteria.forDetails(testDetails)
@@ -73,20 +57,23 @@ class ScannerCriteriaTest : WordSpec({
7357
criteria.regScannerName shouldBe SCANNER_NAME
7458
criteria.minVersion.version shouldBe SCANNER_VERSION
7559
criteria.maxVersion shouldBe Semver(SCANNER_VERSION).nextMinor()
60+
criteria.configuration shouldBe SCANNER_CONFIGURATION
7661
}
7762

7863
"obtain values from the configuration" {
7964
val options = mapOf(
8065
ScannerCriteria.PROP_CRITERIA_NAME to "foo",
8166
ScannerCriteria.PROP_CRITERIA_MIN_VERSION to "1.2.3",
82-
ScannerCriteria.PROP_CRITERIA_MAX_VERSION to "4.5.6"
67+
ScannerCriteria.PROP_CRITERIA_MAX_VERSION to "4.5.6",
68+
ScannerCriteria.PROP_CRITERIA_CONFIGURATION to "config"
8369
)
8470

8571
val criteria = ScannerCriteria.create(testDetails, options)
8672

8773
criteria.regScannerName shouldBe "foo"
8874
criteria.minVersion.version shouldBe "1.2.3"
8975
criteria.maxVersion.version shouldBe "4.5.6"
76+
criteria.configuration shouldBe "config"
9077
}
9178

9279
"parse versions in a lenient way" {
@@ -100,13 +87,6 @@ class ScannerCriteriaTest : WordSpec({
10087
criteria.minVersion.version shouldBe "1.0.0"
10188
criteria.maxVersion.version shouldBe "3.7.0"
10289
}
103-
104-
"use an exact configuration matcher" {
105-
val criteria = ScannerCriteria.create(testDetails)
106-
107-
criteria.configMatcher(testDetails.configuration) shouldBe true
108-
criteria.configMatcher(testDetails.configuration + "_other") shouldBe false
109-
}
11090
}
11191

11292
"ScannerCriteria.matches()" should {
@@ -144,26 +124,31 @@ class ScannerCriteriaTest : WordSpec({
144124
criteria.matches(testDetails) shouldBe false
145125
}
146126

147-
"detect a difference reported by the config matcher" {
148-
val criteria = matchingCriteria.copy(
149-
configMatcher = ScannerCriteria.exactConfigMatcher(testDetails.configuration + "_other")
150-
)
127+
"detect a scanner configuration that does not match" {
128+
val criteria = matchingCriteria.copy(configuration = "${testDetails.configuration}_other")
151129

152130
criteria.matches(testDetails) shouldBe false
153131
}
132+
133+
"ignore the scanner configuration if it is null" {
134+
val criteria = matchingCriteria.copy(configuration = null)
135+
136+
criteria.matches(testDetails) shouldBe true
137+
}
154138
}
155139
})
156140

157141
private const val SCANNER_NAME = "ScannerCriteriaTest"
158142
private const val SCANNER_VERSION = "3.2.1-rc2"
143+
private const val SCANNER_CONFIGURATION = "--command-line-option"
159144

160145
/** Test details to match against. */
161-
private val testDetails = ScannerDetails(SCANNER_NAME, SCANNER_VERSION, "--command-line-option")
146+
private val testDetails = ScannerDetails(SCANNER_NAME, SCANNER_VERSION, SCANNER_CONFIGURATION)
162147

163148
/** A test instance which should accept the test details. */
164149
private val matchingCriteria = ScannerCriteria(
165150
regScannerName = testDetails.name,
166151
minVersion = Semver(testDetails.version),
167152
maxVersion = Semver(testDetails.version).nextPatch(),
168-
configMatcher = ScannerCriteria.exactConfigMatcher(testDetails.configuration)
153+
configuration = testDetails.configuration
169154
)

scanner/src/test/kotlin/storages/ClearlyDefinedStorageTest.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,7 @@ private val TEST_PACKAGE =
106106
)
107107

108108
/** The scanner details used by tests. */
109-
private val SCANNER_CRITERIA =
110-
ScannerCriteria(
111-
"aScanner", Semver("1.0.0"), Semver("2.0.0"),
112-
ScannerCriteria.exactConfigMatcher("aConfig")
113-
)
109+
private val SCANNER_CRITERIA = ScannerCriteria("aScanner", Semver("1.0.0"), Semver("2.0.0"), "aConfig")
114110

115111
/** The template for a ClearlyDefined definitions request. */
116112
private val DEFINITIONS_TEMPLATE = readDefinitionsTemplate()

0 commit comments

Comments
 (0)