|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 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.plugins.scanners.scanoss |
| 21 | + |
| 22 | +import io.kotest.core.spec.style.WordSpec |
| 23 | +import io.kotest.matchers.collections.beEmpty |
| 24 | +import io.kotest.matchers.collections.shouldBeSingleton |
| 25 | +import io.kotest.matchers.should |
| 26 | +import io.kotest.matchers.shouldBe |
| 27 | + |
| 28 | +import org.ossreviewtoolkit.model.TextLocation |
| 29 | +import org.ossreviewtoolkit.model.config.SnippetChoices |
| 30 | +import org.ossreviewtoolkit.model.config.snippet.Choice |
| 31 | +import org.ossreviewtoolkit.model.config.snippet.Given |
| 32 | +import org.ossreviewtoolkit.model.config.snippet.Provenance |
| 33 | +import org.ossreviewtoolkit.model.config.snippet.SnippetChoice |
| 34 | +import org.ossreviewtoolkit.model.config.snippet.SnippetChoiceReason |
| 35 | + |
| 36 | +// Sample files in the results. |
| 37 | +private const val FILE_1 = "a.java" |
| 38 | +private const val FILE_2 = "b.java" |
| 39 | + |
| 40 | +// A sample purl in the results. |
| 41 | +private const val PURL_1 = "pkg:github/fakeuser/[email protected]" |
| 42 | + |
| 43 | +class ScanOssTest : WordSpec({ |
| 44 | + "processSnippetChoices()" should { |
| 45 | + "create empty rules when no snippet choices exist" { |
| 46 | + val scanoss = createScanOss(createScanOssConfig()) |
| 47 | + |
| 48 | + val emptySnippetChoices: List<SnippetChoices> = listOf() |
| 49 | + |
| 50 | + val rules = scanoss.processSnippetChoices(emptySnippetChoices) |
| 51 | + |
| 52 | + rules.ignoreRules should beEmpty() |
| 53 | + rules.removeRules should beEmpty() |
| 54 | + rules.includeRules should beEmpty() |
| 55 | + rules.replaceRules should beEmpty() |
| 56 | + } |
| 57 | + |
| 58 | + "create an include rule for snippet choices with ORIGINAL finding" { |
| 59 | + val vcsInfo = createVcsInfo() |
| 60 | + val scanoss = createScanOss(createScanOssConfig()) |
| 61 | + |
| 62 | + val location = TextLocation(FILE_1, 10, 20) |
| 63 | + val snippetChoices = createSnippetChoices( |
| 64 | + vcsInfo.url, |
| 65 | + createSnippetChoice( |
| 66 | + location, |
| 67 | + PURL_1, |
| 68 | + "This is an original finding" |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + val rules = scanoss.processSnippetChoices(snippetChoices) |
| 73 | + |
| 74 | + rules.includeRules.shouldBeSingleton { rule -> |
| 75 | + rule.purl shouldBe PURL_1 |
| 76 | + rule.path shouldBe FILE_1 |
| 77 | + } |
| 78 | + |
| 79 | + rules.removeRules should beEmpty() |
| 80 | + rules.ignoreRules should beEmpty() |
| 81 | + rules.replaceRules should beEmpty() |
| 82 | + } |
| 83 | + |
| 84 | + "create a remove rule for snippet choices with NOT_FINDING reason" { |
| 85 | + val vcsInfo = createVcsInfo() |
| 86 | + |
| 87 | + val scanoss = createScanOss(createScanOssConfig()) |
| 88 | + |
| 89 | + val location = TextLocation(FILE_2, 15, 30) |
| 90 | + val snippetChoices = createSnippetChoices( |
| 91 | + vcsInfo.url, |
| 92 | + createSnippetChoice( |
| 93 | + location, |
| 94 | + null, // null PURL for NOT_FINDING. |
| 95 | + "This is not a relevant finding" |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + val rules = scanoss.processSnippetChoices(snippetChoices) |
| 100 | + |
| 101 | + rules.removeRules.shouldBeSingleton { rule -> |
| 102 | + rule.path shouldBe FILE_2 |
| 103 | + rule.startLine shouldBe 15 |
| 104 | + rule.endLine shouldBe 30 |
| 105 | + } |
| 106 | + |
| 107 | + rules.includeRules should beEmpty() |
| 108 | + rules.ignoreRules should beEmpty() |
| 109 | + rules.replaceRules should beEmpty() |
| 110 | + } |
| 111 | + |
| 112 | + "handle multiple snippet choices with different reasons correctly" { |
| 113 | + val vcsInfo = createVcsInfo() |
| 114 | + val scanoss = createScanOss(createScanOssConfig()) |
| 115 | + |
| 116 | + val location1 = TextLocation(FILE_1, 10, 20) |
| 117 | + val location2 = TextLocation(FILE_2, 15, 30) |
| 118 | + |
| 119 | + val snippetChoices = createSnippetChoices( |
| 120 | + vcsInfo.url, |
| 121 | + createSnippetChoice( |
| 122 | + location1, |
| 123 | + PURL_1, |
| 124 | + "This is an original finding" |
| 125 | + ), |
| 126 | + createSnippetChoice( |
| 127 | + location2, |
| 128 | + null, |
| 129 | + "This is not a relevant finding" |
| 130 | + ) |
| 131 | + ) |
| 132 | + |
| 133 | + val rules = scanoss.processSnippetChoices(snippetChoices) |
| 134 | + |
| 135 | + rules.includeRules.shouldBeSingleton { rule -> |
| 136 | + rule.purl shouldBe PURL_1 |
| 137 | + rule.path shouldBe FILE_1 |
| 138 | + } |
| 139 | + |
| 140 | + rules.removeRules.shouldBeSingleton { rule -> |
| 141 | + rule.path shouldBe FILE_2 |
| 142 | + rule.startLine shouldBe 15 |
| 143 | + rule.endLine shouldBe 30 |
| 144 | + } |
| 145 | + |
| 146 | + rules.ignoreRules should beEmpty() |
| 147 | + rules.replaceRules should beEmpty() |
| 148 | + } |
| 149 | + |
| 150 | + "create a remove rule without line ranges when snippet choice has UNKNOWN_LINE (-1) values" { |
| 151 | + val vcsInfo = createVcsInfo() |
| 152 | + val scanoss = createScanOss(createScanOssConfig()) |
| 153 | + |
| 154 | + // Create a TextLocation with -1 for start and end lines. |
| 155 | + val location = TextLocation(FILE_2, TextLocation.UNKNOWN_LINE, TextLocation.UNKNOWN_LINE) |
| 156 | + val snippetChoices = createSnippetChoices( |
| 157 | + vcsInfo.url, |
| 158 | + createSnippetChoice( |
| 159 | + location, |
| 160 | + null, // null PURL for NOT_FINDING. |
| 161 | + "This is a not relevant finding with no line ranges" |
| 162 | + ) |
| 163 | + ) |
| 164 | + |
| 165 | + val rules = scanoss.processSnippetChoices(snippetChoices) |
| 166 | + |
| 167 | + rules.removeRules.shouldBeSingleton { rule -> |
| 168 | + rule.path shouldBe FILE_2 |
| 169 | + rule.startLine shouldBe null |
| 170 | + rule.endLine shouldBe null |
| 171 | + } |
| 172 | + |
| 173 | + rules.includeRules should beEmpty() |
| 174 | + rules.ignoreRules should beEmpty() |
| 175 | + rules.replaceRules should beEmpty() |
| 176 | + } |
| 177 | + } |
| 178 | +}) |
| 179 | + |
| 180 | +private fun createSnippetChoices(provenanceUrl: String, vararg snippetChoices: SnippetChoice) = |
| 181 | + listOf(SnippetChoices(Provenance(provenanceUrl), snippetChoices.asList())) |
| 182 | + |
| 183 | +private fun createSnippetChoice(location: TextLocation, purl: String? = null, comment: String) = |
| 184 | + SnippetChoice( |
| 185 | + Given( |
| 186 | + location |
| 187 | + ), |
| 188 | + Choice( |
| 189 | + purl, |
| 190 | + if (purl == null) SnippetChoiceReason.NO_RELEVANT_FINDING else SnippetChoiceReason.ORIGINAL_FINDING, |
| 191 | + comment |
| 192 | + ) |
| 193 | + ) |
0 commit comments