Skip to content

Commit a49e6d8

Browse files
committed
feat(Analyzer): Support path excludes in findManagedFiles()
If the analyzer is configured to skip excludes, it passes the Excludes defined in the repository configuration to PackageManager.findManagedFiles(). That way the analyzer result will contain only projects that are not matched by a path exclude. Resolves #5968. Signed-off-by: Oliver Heger <[email protected]>
1 parent 21c872a commit a49e6d8

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

analyzer/src/main/kotlin/Analyzer.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import kotlinx.coroutines.withContext
3939
import org.apache.logging.log4j.kotlin.Logging
4040
import org.apache.logging.log4j.kotlin.logger
4141

42+
import org.ossreviewtoolkit.analyzer.PackageManager.Companion.excludes
4243
import org.ossreviewtoolkit.analyzer.managers.Unmanaged
4344
import org.ossreviewtoolkit.downloader.VersionControlSystem
4445
import org.ossreviewtoolkit.model.AnalyzerResult
@@ -89,7 +90,11 @@ class Analyzer(private val config: AnalyzerConfiguration, private val labels: Ma
8990
// debugging purposes.
9091
mutableMapOf(distinctPackageManagers.first() to listOf(absoluteProjectPath))
9192
} else {
92-
PackageManager.findManagedFiles(absoluteProjectPath, distinctPackageManagers).toMutableMap()
93+
PackageManager.findManagedFiles(
94+
absoluteProjectPath,
95+
distinctPackageManagers,
96+
config.excludes(repositoryConfiguration)
97+
).toMutableMap()
9398
}
9499

95100
// Associate mapped files by the package manager that manages them.
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.analyzer
21+
22+
import io.kotest.core.spec.style.WordSpec
23+
import io.kotest.matchers.collections.shouldContainOnly
24+
import io.kotest.matchers.shouldBe
25+
26+
import io.mockk.every
27+
import io.mockk.mockkObject
28+
import io.mockk.unmockkObject
29+
import io.mockk.verify
30+
31+
import java.io.File
32+
33+
import org.ossreviewtoolkit.analyzer.managers.Maven
34+
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
35+
import org.ossreviewtoolkit.model.config.Excludes
36+
import org.ossreviewtoolkit.model.config.PathExclude
37+
import org.ossreviewtoolkit.model.config.PathExcludeReason
38+
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
39+
40+
class AnalyzerTest : WordSpec({
41+
beforeTest {
42+
mockkObject(PackageManager)
43+
every { PackageManager.ALL } answers { callOriginal() }
44+
every { PackageManager.findManagedFiles(rootDir, any(), any()) } returns projectFiles
45+
}
46+
47+
afterTest {
48+
unmockkObject(PackageManager)
49+
}
50+
51+
"findManagedFiles" should {
52+
"find all managed files" {
53+
val analyzer = Analyzer(AnalyzerConfiguration())
54+
55+
val result = analyzer.findManagedFiles(rootDir, repositoryConfiguration = repositoryConfig)
56+
57+
checkManagedFileInfo(result)
58+
59+
verify {
60+
PackageManager.findManagedFiles(rootDir, any(), Excludes())
61+
}
62+
}
63+
64+
"take excludes into account if configured" {
65+
val analyzer = Analyzer(AnalyzerConfiguration(skipExcludes = true))
66+
67+
val result = analyzer.findManagedFiles(rootDir, repositoryConfiguration = repositoryConfig)
68+
69+
checkManagedFileInfo(result)
70+
71+
verify {
72+
PackageManager.findManagedFiles(rootDir, any(), repositoryConfig.excludes)
73+
}
74+
}
75+
}
76+
})
77+
78+
/** The root dir used when searching for definition files. */
79+
private val rootDir = File("projectRoot").absoluteFile
80+
81+
/**
82+
* A default [RepositoryConfiguration] that also defines some [Excludes]. This is used to test whether [Excludes] are
83+
* correctly propagated to the [PackageManager].
84+
*/
85+
private val repositoryConfig = RepositoryConfiguration(
86+
excludes = Excludes(
87+
paths = listOf(PathExclude("**/src/test/**", PathExcludeReason.TEST_OF))
88+
)
89+
)
90+
91+
/**
92+
* A [PackageManagerFactory] used as key in the default result for [PackageManager.findManagedFiles].
93+
*/
94+
private val factory: PackageManagerFactory = Maven.Factory()
95+
96+
/**
97+
* A default result to be returned from [PackageManager.findManagedFiles].
98+
*/
99+
private val projectFiles: ManagedProjectFiles =
100+
mapOf(factory to listOf(File("pom.xml"), File("sub/pom.xml")))
101+
102+
/**
103+
* Return a [Map] with the names of the [PackageManager]s contained in this map as keys to simplify access.
104+
*/
105+
private fun Map<PackageManager, List<File>>.byName(): Map<String, List<File>> = mapKeys { e -> e.key.managerName }
106+
107+
/**
108+
* Check whether the given [info] contains the expected information.
109+
*/
110+
private fun checkManagedFileInfo(info: Analyzer.ManagedFileInfo) {
111+
with(info) {
112+
absoluteProjectPath shouldBe rootDir
113+
repositoryConfiguration shouldBe repositoryConfig
114+
val managedFilesByName = managedFiles.byName()
115+
managedFilesByName.keys shouldContainOnly listOf("Maven")
116+
managedFilesByName.getValue("Maven") shouldBe projectFiles[factory]
117+
}
118+
}

0 commit comments

Comments
 (0)