|
| 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