|
19 | 19 |
|
20 | 20 | package org.ossreviewtoolkit.plugins.packagemanagers.maven
|
21 | 21 |
|
| 22 | +import io.kotest.core.TestConfiguration |
22 | 23 | import io.kotest.core.spec.style.WordSpec
|
23 | 24 | import io.kotest.engine.spec.tempdir
|
| 25 | +import io.kotest.engine.spec.tempfile |
| 26 | +import io.kotest.matchers.collections.beEmpty |
24 | 27 | import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
|
| 28 | +import io.kotest.matchers.should |
| 29 | +import io.kotest.matchers.shouldBe |
| 30 | +import io.kotest.matchers.string.shouldContain |
25 | 31 |
|
| 32 | +import io.mockk.every |
26 | 33 | import io.mockk.mockk
|
| 34 | +import io.mockk.spyk |
| 35 | + |
| 36 | +import java.io.File |
| 37 | +import java.io.PrintStream |
| 38 | + |
| 39 | +import org.apache.maven.cli.MavenCli |
| 40 | +import org.apache.maven.execution.MavenSession |
| 41 | +import org.apache.maven.project.MavenProject |
| 42 | + |
| 43 | +import org.eclipse.aether.graph.DependencyNode |
| 44 | + |
| 45 | +import org.ossreviewtoolkit.model.Identifier |
| 46 | +import org.ossreviewtoolkit.model.Package |
| 47 | +import org.ossreviewtoolkit.model.Severity |
| 48 | +import org.ossreviewtoolkit.model.utils.DependencyGraphBuilder |
| 49 | +import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.DependencyTreeMojoNode |
| 50 | +import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.JSON |
27 | 51 |
|
28 | 52 | class TychoTest : WordSpec({
|
29 | 53 | "mapDefinitionFiles()" should {
|
@@ -56,4 +80,98 @@ class TychoTest : WordSpec({
|
56 | 80 | mappedDefinitionFiles shouldContainExactlyInAnyOrder listOf(tychoDefinitionFile1, tychoDefinitionFile2)
|
57 | 81 | }
|
58 | 82 | }
|
| 83 | + |
| 84 | + "resolveDependencies()" should { |
| 85 | + "generate warnings about auto-generated pom files" { |
| 86 | + val definitionFile = tempfile() |
| 87 | + val rootProject = createMavenProject("root", definitionFile) |
| 88 | + val subProject = createMavenProject("sub") |
| 89 | + val projectsList = listOf(rootProject, subProject) |
| 90 | + |
| 91 | + val tycho = spyk(Tycho("Tycho", tempdir(), mockk(relaxed = true), mockk(relaxed = true))) |
| 92 | + injectCliMock(tycho, projectsList.toJson(), projectsList) |
| 93 | + |
| 94 | + val pkg1 = mockk<Package>(relaxed = true) |
| 95 | + val pkg2 = mockk<Package> { |
| 96 | + every { id } returns Identifier("Tycho:org.ossreviewtoolkit:sub-dependency:1.0.0") |
| 97 | + every { description } returns "POM was created by Sonatype Nexus" |
| 98 | + } |
| 99 | + |
| 100 | + val graphBuilder = mockk<DependencyGraphBuilder<DependencyNode>>(relaxed = true) { |
| 101 | + every { packages() } returns setOf(pkg1, pkg2) |
| 102 | + } |
| 103 | + |
| 104 | + every { tycho.createGraphBuilder(any(), any()) } returns graphBuilder |
| 105 | + |
| 106 | + val results = tycho.resolveDependencies(definitionFile, emptyMap()) |
| 107 | + |
| 108 | + val (rootResults, subResults) = results.partition { it.project.id.name == "root" } |
| 109 | + |
| 110 | + with(rootResults.single().issues.single()) { |
| 111 | + severity shouldBe Severity.HINT |
| 112 | + message shouldContain pkg2.id.toCoordinates() |
| 113 | + message shouldContain "auto-generated POM" |
| 114 | + source shouldBe "Tycho" |
| 115 | + } |
| 116 | + |
| 117 | + subResults.single().issues should beEmpty() |
| 118 | + } |
| 119 | + } |
59 | 120 | })
|
| 121 | + |
| 122 | +/** |
| 123 | + * Create a mock [MavenProject] with default coordinates and the given [name] and optional [definitionFile]. |
| 124 | + */ |
| 125 | +private fun TestConfiguration.createMavenProject(name: String, definitionFile: File = tempfile()): MavenProject = |
| 126 | + mockk(relaxed = true) { |
| 127 | + every { groupId } returns "org.ossreviewtoolkit" |
| 128 | + every { artifactId } returns name |
| 129 | + every { version } returns "1.0.0" |
| 130 | + every { file } returns definitionFile |
| 131 | + every { id } returns "org.ossreviewtoolkit:$name:jar:1.0.0" |
| 132 | + every { parent } returns null |
| 133 | + } |
| 134 | + |
| 135 | +/** |
| 136 | + * Return a [DependencyTreeMojoNode] with the properties of this [MavenProject]. |
| 137 | + */ |
| 138 | +private fun MavenProject.toDependencyTreeMojoNode(): DependencyTreeMojoNode = |
| 139 | + DependencyTreeMojoNode(groupId, artifactId, version, "jar", "compile", "") |
| 140 | + |
| 141 | +/** |
| 142 | + * Generate JSON output analogously to the Maven Dependency Plugin for this list of [MavenProject]s. Here only the |
| 143 | + * root project nodes are relevant; dependencies are not included. |
| 144 | + */ |
| 145 | +private fun Collection<MavenProject>.toJson(): String = |
| 146 | + joinToString("\n") { JSON.encodeToString(it.toDependencyTreeMojoNode()) } |
| 147 | + |
| 148 | +/** |
| 149 | + * Create a mock for a [MavenCli] object and configure the given [tycho] spi to use it. The mock CLI simulates a |
| 150 | + * Maven build that produces the given [buildOutput] and detects the given [projectsList]. It returns the given |
| 151 | + * [exitCode]. |
| 152 | + */ |
| 153 | +private fun injectCliMock( |
| 154 | + tycho: Tycho, |
| 155 | + buildOutput: String, |
| 156 | + projectsList: List<MavenProject>, |
| 157 | + exitCode: Int = 0 |
| 158 | +): MavenCli { |
| 159 | + val cli = mockk<MavenCli> { |
| 160 | + every { doMain(any(), any(), any(), any()) } returns exitCode |
| 161 | + } |
| 162 | + |
| 163 | + val session = mockk<MavenSession> { |
| 164 | + every { projects } returns projectsList |
| 165 | + } |
| 166 | + |
| 167 | + every { tycho.createMavenCli(any(), any()) } answers { |
| 168 | + val collector = firstArg<TychoProjectsCollector>() |
| 169 | + collector.afterSessionEnd(session) |
| 170 | + |
| 171 | + val out = secondArg<PrintStream>() |
| 172 | + out.println(buildOutput) |
| 173 | + cli |
| 174 | + } |
| 175 | + |
| 176 | + return cli |
| 177 | +} |
0 commit comments