Skip to content

Commit

Permalink
"Found" string should contain information about new issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hybloid committed May 3, 2024
1 parent d776021 commit 3ef668b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
17 changes: 10 additions & 7 deletions baseline-cli/src/main/kotlin/BaselineCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ internal object BaselineCli {
cliPrinter: (String) -> Unit,
errPrinter: (String) -> Unit
): Invocation {
val size = results?.size ?: 0
val size = results.orEmpty()
.asSequence()
.filter(baselineFilter(hasBaseline))
.count()

if (size > 0) {
errPrinter("Found $size new problems according to the checks applied")
} else {
Expand Down Expand Up @@ -138,14 +142,9 @@ internal object BaselineCli {
hasBaseline: Boolean,
thresholds: SeverityThresholds
): List<String> {
val baselineFilter: (Result) -> Boolean = when {
!hasBaseline -> { _ -> true }
else -> { x -> x.baselineState == Result.BaselineState.NEW }
}

val resultsBySeverity = results.orEmpty()
.asSequence()
.filter(baselineFilter)
.filter(baselineFilter(hasBaseline))
.groupingBy { it.severity() }
.eachCount()

Expand Down Expand Up @@ -186,4 +185,8 @@ internal object BaselineCli {
return { state.computeIfAbsent(it, f) }
}

private fun baselineFilter(hasBaseline: Boolean): (Result) -> Boolean = when {
!hasBaseline -> { _ -> true }
else -> { x -> x.baselineState == Result.BaselineState.NEW }
}
}
15 changes: 14 additions & 1 deletion baseline-cli/src/test/kotlin/BaselineCliTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import kotlin.io.path.Path
class BaselineCliTest {

private val sarif = copySarifFromResources("report.equal.sarif.json")
private val sarifTwoIssues = copySarifFromResources("twoissues.sarif.json")
private val sarifSingleIssue = copySarifFromResources("single.sarif.json")
private val emptySarif = copySarifFromResources("empty.sarif.json")
private val corruptedSarif = Path("src/test/resources/corrupted.sarif.json").toString()

Expand Down Expand Up @@ -159,7 +161,7 @@ class BaselineCliTest {
@Test
fun `test include absent false`() {
// Act
assertDoesNotThrow { BaselineCli.process(BaselineOptions(copySarifFromResources("single.sarif.json"), sarif, includeAbsent = false), stdout::append, stderr::append) }
assertDoesNotThrow { BaselineCli.process(BaselineOptions(sarifSingleIssue, sarif, includeAbsent = false), stdout::append, stderr::append) }

// Assert
assertFalse(stdout.contains("ABSENT:"))
Expand All @@ -168,6 +170,17 @@ class BaselineCliTest {
assertFalse(content.contains("absent"))
}

@Test
fun `test new result count contains only new issues`() {
// Act
assertDoesNotThrow { BaselineCli.process(BaselineOptions(sarifTwoIssues, sarifSingleIssue, includeAbsent = false), stdout::append, stderr::append) }

// Assert
assertTrue(stdout.contains("UNCHANGED: 1"))
assertTrue(stdout.contains("NEW: 1"))
assertTrue(stderr.contains("Found 1 new problems according to the checks applied"))
}


private fun copySarifFromResources(name: String) = run {
val tmp = Files.createTempFile(null, ".sarif")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"kind": "fail",
"level": "warning",
"message": {
"text": "Result of \u0027A.unusedResult()\u0027 is ignored",
"markdown": "Result of `A.unusedResult()` is ignored"
"text": "Result of \u0027A.unusedResult123()\u0027 is ignored",
"markdown": "Result of `A.unusedResult123()` is ignored"
},
"locations": [
{
Expand All @@ -24,7 +24,7 @@
"charOffset": 143,
"charLength": 12,
"snippet": {
"text": "unusedResult"
"text": "unusedResult123"
},
"sourceLanguage": "JAVA"
},
Expand All @@ -34,7 +34,7 @@
"charOffset": 96,
"charLength": 86,
"snippet": {
"text": " System.out.println(\"Another\");\n }\n unusedResult();\n unusedResult();\n }"
"text": " System.out.println(\"Another\");\n }\n unusedResult123();\n unusedResult();\n }"
}
}
},
Expand Down

0 comments on commit 3ef668b

Please sign in to comment.