From 3ef668bf25f8834b05fa476e1ff125eee043182d Mon Sep 17 00:00:00 2001 From: Dima Golovinov Date: Fri, 3 May 2024 10:10:03 +0200 Subject: [PATCH] "Found" string should contain information about new issues --- baseline-cli/src/main/kotlin/BaselineCli.kt | 17 ++++++++++------- baseline-cli/src/test/kotlin/BaselineCliTest.kt | 15 ++++++++++++++- ...baseline.sarif.json => twoissues.sarif.json} | 8 ++++---- 3 files changed, 28 insertions(+), 12 deletions(-) rename baseline-cli/src/test/resources/{baseline.sarif.json => twoissues.sarif.json} (92%) diff --git a/baseline-cli/src/main/kotlin/BaselineCli.kt b/baseline-cli/src/main/kotlin/BaselineCli.kt index cf288fa..4a8d035 100644 --- a/baseline-cli/src/main/kotlin/BaselineCli.kt +++ b/baseline-cli/src/main/kotlin/BaselineCli.kt @@ -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 { @@ -138,14 +142,9 @@ internal object BaselineCli { hasBaseline: Boolean, thresholds: SeverityThresholds ): List { - 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() @@ -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 } + } } diff --git a/baseline-cli/src/test/kotlin/BaselineCliTest.kt b/baseline-cli/src/test/kotlin/BaselineCliTest.kt index 6e53107..e1a1dd9 100644 --- a/baseline-cli/src/test/kotlin/BaselineCliTest.kt +++ b/baseline-cli/src/test/kotlin/BaselineCliTest.kt @@ -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() @@ -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:")) @@ -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") diff --git a/baseline-cli/src/test/resources/baseline.sarif.json b/baseline-cli/src/test/resources/twoissues.sarif.json similarity index 92% rename from baseline-cli/src/test/resources/baseline.sarif.json rename to baseline-cli/src/test/resources/twoissues.sarif.json index f69a2dc..b1997f9 100644 --- a/baseline-cli/src/test/resources/baseline.sarif.json +++ b/baseline-cli/src/test/resources/twoissues.sarif.json @@ -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": [ { @@ -24,7 +24,7 @@ "charOffset": 143, "charLength": 12, "snippet": { - "text": "unusedResult" + "text": "unusedResult123" }, "sourceLanguage": "JAVA" }, @@ -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 }" } } },