Skip to content

test: Add test for CycloneDX XML with authors #10271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import io.kotest.matchers.shouldNotBe
import org.cyclonedx.parsers.JsonParser
import org.cyclonedx.parsers.XmlParser

import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.plugins.reporters.cyclonedx.CycloneDxReporter.Companion.REPORT_BASE_FILENAME
import org.ossreviewtoolkit.reporter.ORT_RESULT
import org.ossreviewtoolkit.reporter.ORT_RESULT_WITH_ILLEGAL_COPYRIGHTS
Expand Down Expand Up @@ -94,6 +96,30 @@ class CycloneDxReporterFunTest : WordSpec({
}
}

"create the expected XML file with authors" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe instead of adding a new separate test, it would make sense to instead enhance the input of "create the expected XML file". For enhancing it withAuthors() imo is ok. Furthermore, the analog JSON test case IMO should also use / keep on using the same input, so that XML and JSON tests remain analog.

val expectedBom = readResource("/cyclonedx-reporter-expected-result-with-authors.xml")

val ortResult = ORT_RESULT.withAuthors(
packageId = Identifier("NPM:@ort:license-file:1.0"),
authors = setOf("Author One", "Author Two")
)

val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
outputFileFormats = listOf("xml")
).generateReport(ReporterInput(ortResult), outputDir)

bomFileResults.shouldBeSingleton {
it shouldBeSuccess { bomFile ->
bomFile shouldBe aFile()
bomFile shouldNotBe emptyFile()

val actualBom = bomFile.readText().patchCycloneDxResult().normalizeLineBreaks()
actualBom shouldBe expectedBom
}
}
}

"the expected XML file even if some copyrights contain non printable characters" {
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
Expand Down Expand Up @@ -224,6 +250,26 @@ class CycloneDxReporterFunTest : WordSpec({
}
})

/**
* Add [authors] to the [package][packageId].
*/
private fun OrtResult.withAuthors(packageId: Identifier, authors: Set<String>): OrtResult =
analyzer?.let { analyzer ->
copy(
analyzer = analyzer.copy(
result = analyzer.result.copy(
packages = analyzer.result.packages.map { pkg ->
if (pkg.id == packageId) {
pkg.copy(authors = authors)
} else {
pkg
}
}.toSet()
)
)
)
} ?: this

private fun String.patchCycloneDxResult(): String =
replaceFirst(
"""urn:uuid:[a-f0-9]{8}(?:-[a-f0-9]{4}){4}[a-f0-9]{8}""".toRegex(),
Expand Down
Loading
Loading