@@ -29,10 +29,16 @@ import io.kotest.matchers.maps.beEmpty
29
29
import io.kotest.matchers.should
30
30
import io.kotest.matchers.shouldBe
31
31
32
+ import java.io.File
33
+
32
34
import org.ossreviewtoolkit.analyzer.managers.*
33
35
import org.ossreviewtoolkit.model.VcsInfo
34
36
import org.ossreviewtoolkit.model.VcsType
37
+ import org.ossreviewtoolkit.model.config.Excludes
38
+ import org.ossreviewtoolkit.model.config.PathExclude
39
+ import org.ossreviewtoolkit.model.config.PathExcludeReason
35
40
import org.ossreviewtoolkit.utils.test.createSpecTempDir
41
+ import org.ossreviewtoolkit.utils.test.createTestTempDir
36
42
37
43
class PackageManagerTest : WordSpec ({
38
44
val definitionFiles = listOf(
@@ -71,11 +77,7 @@ class PackageManagerTest : WordSpec({
71
77
val projectDir = createSpecTempDir()
72
78
73
79
beforeSpec {
74
- definitionFiles.forEach { file ->
75
- projectDir.resolve(file).also { dir ->
76
- dir.parentFile.mkdirs()
77
- }.writeText("Dummy text to avoid the file to be empty, as empty files are skipped.")
78
- }
80
+ definitionFiles.writeFiles(projectDir)
79
81
}
80
82
81
83
" findManagedFiles" should {
@@ -88,11 +90,7 @@ class PackageManagerTest : WordSpec({
88
90
it is Unmanaged .Factory
89
91
}
90
92
91
- // The keys in expected and actual maps of definition files are different instances of package manager
92
- // factories. So to compare values use the package manager types as keys instead.
93
- val managedFilesByName = managedFiles.map { (manager, files) ->
94
- manager.type to files.map { it.relativeTo(projectDir).invariantSeparatorsPath }
95
- }.toMap()
93
+ val managedFilesByName = managedFiles.groupByName(projectDir)
96
94
97
95
assertSoftly {
98
96
managedFilesByName[" Bower" ] should containExactly("bower/bower.json")
@@ -144,11 +142,7 @@ class PackageManagerTest : WordSpec({
144
142
145
143
managedFiles.size shouldBe 3
146
144
147
- // The keys in expected and actual maps of definition files are different instances of package manager
148
- // factories. So to compare values use the package manager types as keys instead.
149
- val managedFilesByName = managedFiles.map { (manager, files) ->
150
- manager.type to files.map { it.relativeTo(projectDir).invariantSeparatorsPath }
151
- }.toMap()
145
+ val managedFilesByName = managedFiles.groupByName(projectDir)
152
146
153
147
managedFilesByName[" Gradle" ] should containExactlyInAnyOrder(
154
148
"gradle-groovy/build.gradle",
@@ -167,6 +161,38 @@ class PackageManagerTest : WordSpec({
167
161
managedFiles should beEmpty()
168
162
}
169
163
164
+ " take path excludes into account" {
165
+ val tempDir = " test/"
166
+ val definitionFilesWithExcludes = definitionFiles +
167
+ listOf("pom.xml", "build.gradle", "build.sbt").map { " $tempDir$it " }
168
+ val rootDir = createTestTempDir()
169
+ definitionFilesWithExcludes.writeFiles(rootDir)
170
+
171
+ val pathExclude = PathExclude ("$tempDir**", PathExcludeReason .TEST_OF )
172
+ val excludes = Excludes (paths = listOf(pathExclude))
173
+
174
+ val managedFilesByName = PackageManager .findManagedFiles(rootDir, excludes = excludes).groupByName(rootDir)
175
+
176
+ managedFilesByName[" Gradle" ] should containExactlyInAnyOrder(
177
+ "gradle-groovy/build.gradle",
178
+ "gradle-kotlin/build.gradle.kts"
179
+ )
180
+ managedFilesByName[" Maven" ] should containExactly("maven/pom.xml")
181
+ managedFilesByName[" SBT" ] should containExactly("sbt/build.sbt")
182
+ }
183
+
184
+ " handle specific excluded definition files" {
185
+ val pathExclude = PathExclude ("gradle-groovy/build.gradle", PathExcludeReason .OTHER )
186
+ val excludes = Excludes (paths = listOf(pathExclude))
187
+
188
+ val managedFiles = PackageManager .findManagedFiles(projectDir, excludes = excludes)
189
+ val managedFilesByName = managedFiles.groupByName(projectDir)
190
+
191
+ managedFilesByName[" Gradle" ] should containExactly(
192
+ "gradle-kotlin/build.gradle.kts"
193
+ )
194
+ }
195
+
170
196
" fail if the provided file is not a directory" {
171
197
shouldThrow<IllegalArgumentException > {
172
198
PackageManager .findManagedFiles(projectDir.resolve("pom.xml"))
@@ -221,3 +247,24 @@ class PackageManagerTest : WordSpec({
221
247
}
222
248
}
223
249
})
250
+
251
+ /* *
252
+ * Transform this map with definition files grouped by package manager factories, so that the results of specific
253
+ * package managers can be easily accessed. The keys in expected and actual maps of definition files are different
254
+ * instances of package manager factories. So to compare values use the package manager types as keys instead.
255
+ */
256
+ private fun ManagedProjectFiles.groupByName (projectDir : File ) =
257
+ map { (manager, files) ->
258
+ manager.type to files.map { it.relativeTo(projectDir).invariantSeparatorsPath }
259
+ }.toMap()
260
+
261
+ /* *
262
+ * Create files with a dummy content in the given [directory] for all the path names in this collection.
263
+ */
264
+ private fun Collection<String>.writeFiles (directory : File ) {
265
+ forEach { file ->
266
+ directory.resolve(file).also { dir ->
267
+ dir.parentFile.mkdirs()
268
+ }.writeText(" Dummy text to avoid the file to be empty, as empty files are skipped." )
269
+ }
270
+ }
0 commit comments