Skip to content

Commit d38880f

Browse files
committed
test(ctrlx-reporter): Improve the functional test
This new test does not only check if a report can be generated, it actually verifies the content of the report. Signed-off-by: Nicolas Nobelis <[email protected]>
1 parent 79332d6 commit d38880f

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

plugins/reporters/ctrlx/src/funTest/kotlin/CtrlXAutomationReporterFunTest.kt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,34 @@ import io.kotest.core.spec.style.StringSpec
2323
import io.kotest.engine.spec.tempdir
2424
import io.kotest.matchers.collections.haveSize
2525
import io.kotest.matchers.collections.shouldBeSingleton
26+
import io.kotest.matchers.collections.shouldHaveSize
2627
import io.kotest.matchers.nulls.shouldNotBeNull
2728
import io.kotest.matchers.result.shouldBeSuccess
2829
import io.kotest.matchers.should
30+
import io.kotest.matchers.shouldBe
31+
32+
import java.io.File
2933

3034
import kotlinx.serialization.json.decodeFromStream
3135

36+
import org.ossreviewtoolkit.model.AnalyzerResult
37+
import org.ossreviewtoolkit.model.AnalyzerRun
38+
import org.ossreviewtoolkit.model.DependencyGraph
39+
import org.ossreviewtoolkit.model.DependencyReference
40+
import org.ossreviewtoolkit.model.Identifier
41+
import org.ossreviewtoolkit.model.OrtResult
42+
import org.ossreviewtoolkit.model.Package
43+
import org.ossreviewtoolkit.model.Project
44+
import org.ossreviewtoolkit.model.Repository
45+
import org.ossreviewtoolkit.model.RootDependencyIndex
46+
import org.ossreviewtoolkit.model.Scope
47+
import org.ossreviewtoolkit.model.VcsInfo
48+
import org.ossreviewtoolkit.model.VcsType
3249
import org.ossreviewtoolkit.plugins.reporters.ctrlx.CtrlXAutomationReporter.Companion.REPORT_FILENAME
3350
import org.ossreviewtoolkit.reporter.ORT_RESULT
3451
import org.ossreviewtoolkit.reporter.ReporterInput
52+
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
53+
import org.ossreviewtoolkit.utils.spdx.toSpdx
3554
import org.ossreviewtoolkit.utils.test.getAssetFile
3655

3756
class CtrlXAutomationReporterFunTest : StringSpec({
@@ -52,4 +71,87 @@ class CtrlXAutomationReporterFunTest : StringSpec({
5271
it shouldBeSuccess outputDir.resolve(REPORT_FILENAME)
5372
}
5473
}
74+
75+
"Generating a report works and produces a valid fossinfo.json" {
76+
val reporter = CtrlXAutomationReporter()
77+
val input = createReporterInput()
78+
val outputDir = createOrtTempDir("ctrlx-automation-reporter-test")
79+
80+
val reporterResult = reporter.generateReport(input, outputDir)
81+
82+
validateReport(reporterResult) {
83+
components shouldNotBeNull {
84+
this shouldHaveSize 2
85+
first().name shouldBe "package1"
86+
last().name shouldBe "package2"
87+
}
88+
}
89+
}
5590
})
91+
92+
private fun validateReport(reporterResult: List<Result<File>>, validate: FossInfo.() -> Unit) {
93+
reporterResult.shouldBeSingleton { result ->
94+
result shouldBeSuccess { file ->
95+
file.name shouldBe "fossinfo.json"
96+
val fossInfo = file.inputStream().use {
97+
CtrlXAutomationReporter.JSON.decodeFromStream<FossInfo>(it)
98+
}
99+
100+
fossInfo.validate()
101+
}
102+
}
103+
}
104+
105+
private fun createReporterInput(): ReporterInput {
106+
val analyzedVcs = VcsInfo(
107+
type = VcsType.GIT,
108+
revision = "master",
109+
url = "https://github.com/path/first-project.git",
110+
path = "sub/path"
111+
)
112+
113+
val package1 = Package.EMPTY.copy(
114+
id = Identifier("Maven:ns:package1:1.0"),
115+
declaredLicenses = setOf("LicenseRef-scancode-broadcom-commercial"),
116+
concludedLicense = "LicenseRef-scancode-broadcom-commercial".toSpdx()
117+
)
118+
val package2 = Package.EMPTY.copy(
119+
id = Identifier("Maven:ns:package2:1.0"),
120+
declaredLicenses = setOf("MIT"),
121+
concludedLicense = "MIT".toSpdx()
122+
)
123+
val project = Project.EMPTY.copy(
124+
id = Identifier.EMPTY.copy(name = "test-project"),
125+
scopeDependencies = setOf(
126+
Scope("scope-1", setOf(package1.toReference(), package2.toReference()))
127+
),
128+
vcs = analyzedVcs,
129+
vcsProcessed = analyzedVcs
130+
)
131+
132+
return ReporterInput(
133+
OrtResult(
134+
repository = Repository(
135+
vcs = analyzedVcs,
136+
vcsProcessed = analyzedVcs
137+
),
138+
analyzer = AnalyzerRun.EMPTY.copy(
139+
result = AnalyzerResult(
140+
projects = setOf(project),
141+
packages = setOf(package1, package2),
142+
dependencyGraphs = mapOf(
143+
"test" to DependencyGraph(
144+
listOf(package1.id, package2.id),
145+
sortedSetOf(
146+
DependencyGraph.DEPENDENCY_REFERENCE_COMPARATOR,
147+
DependencyReference(0),
148+
DependencyReference(1)
149+
),
150+
mapOf(DependencyGraph.qualifyScope(project.id, "scope-1") to listOf(RootDependencyIndex(0)))
151+
)
152+
)
153+
)
154+
)
155+
)
156+
)
157+
}

0 commit comments

Comments
 (0)