Skip to content

Commit

Permalink
test(ctrlx-reporter): Improve the functional test
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
nnobelis committed Feb 3, 2025
1 parent fe56fb4 commit 311ade4
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,34 @@ import io.kotest.core.spec.style.StringSpec
import io.kotest.engine.spec.tempdir
import io.kotest.matchers.collections.haveSize
import io.kotest.matchers.collections.shouldBeSingleton
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.result.shouldBeSuccess
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe

import java.io.File

import kotlinx.serialization.json.decodeFromStream

import org.ossreviewtoolkit.model.AnalyzerResult
import org.ossreviewtoolkit.model.AnalyzerRun
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.DependencyReference
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.Repository
import org.ossreviewtoolkit.model.RootDependencyIndex
import org.ossreviewtoolkit.model.Scope
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.plugins.reporters.ctrlx.CtrlXAutomationReporter.Companion.REPORT_FILENAME
import org.ossreviewtoolkit.reporter.ORT_RESULT
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
import org.ossreviewtoolkit.utils.spdx.toSpdx
import org.ossreviewtoolkit.utils.test.getAssetFile

class CtrlXAutomationReporterFunTest : StringSpec({
Expand All @@ -52,4 +71,87 @@ class CtrlXAutomationReporterFunTest : StringSpec({
it shouldBeSuccess outputDir.resolve(REPORT_FILENAME)
}
}

"Generating a report works and produces a valid fossinfo.json" {
val reporter = CtrlXAutomationReporter()
val input = createReporterInput()
val outputDir = createOrtTempDir("ctrlx-automation-reporter-test")

val reporterResult = reporter.generateReport(input, outputDir)

validateReport(reporterResult) {
components shouldNotBeNull {
this shouldHaveSize 2
first().name shouldBe "package1"
last().name shouldBe "package2"
}
}
}
})

private fun validateReport(reporterResult: List<Result<File>>, validate: FossInfo.() -> Unit) {
reporterResult.shouldBeSingleton { result ->
result shouldBeSuccess { file ->
file.name shouldBe "fossinfo.json"
val fossInfo = file.inputStream().use {
CtrlXAutomationReporter.JSON.decodeFromStream<FossInfo>(it)
}

fossInfo.validate()
}
}
}

private fun createReporterInput(): ReporterInput {
val analyzedVcs = VcsInfo(
type = VcsType.GIT,
revision = "master",
url = "https://github.com/path/first-project.git",
path = "sub/path"
)

val package1 = Package.EMPTY.copy(
id = Identifier("Maven:ns:package1:1.0"),
declaredLicenses = setOf("LicenseRef-scancode-broadcom-commercial"),
concludedLicense = "LicenseRef-scancode-broadcom-commercial".toSpdx()
)
val package2 = Package.EMPTY.copy(
id = Identifier("Maven:ns:package2:1.0"),
declaredLicenses = setOf("MIT"),
concludedLicense = "MIT".toSpdx()
)
val project = Project.EMPTY.copy(
id = Identifier.EMPTY.copy(name = "test-project"),
scopeDependencies = setOf(
Scope("scope-1", setOf(package1.toReference(), package2.toReference()))
),
vcs = analyzedVcs,
vcsProcessed = analyzedVcs
)

return ReporterInput(
OrtResult(
repository = Repository(
vcs = analyzedVcs,
vcsProcessed = analyzedVcs
),
analyzer = AnalyzerRun.EMPTY.copy(
result = AnalyzerResult(
projects = setOf(project),
packages = setOf(package1, package2),
dependencyGraphs = mapOf(
"test" to DependencyGraph(
listOf(package1.id, package2.id),
sortedSetOf(
DependencyGraph.DEPENDENCY_REFERENCE_COMPARATOR,
DependencyReference(0),
DependencyReference(1)
),
mapOf(DependencyGraph.qualifyScope(project.id, "scope-1") to listOf(RootDependencyIndex(0)))
)
)
)
)
)
)
}

0 comments on commit 311ade4

Please sign in to comment.