From 7abb0d9f0cd37916b9d2c4170a1405591d36fb64 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Mon, 26 Jun 2023 14:01:20 +0300 Subject: [PATCH] [Gradle] Minor: prettify extraction of Tooling diagnostics from output --- .../gradle/testbase/diagnosticsAssertions.kt | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/diagnosticsAssertions.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/diagnosticsAssertions.kt index b8723c410d4..a19bdd82d4d 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/diagnosticsAssertions.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/diagnosticsAssertions.kt @@ -71,34 +71,44 @@ fun String.assertNoDiagnostic(diagnosticFactory: ToolingDiagnosticFactory, withS */ fun BuildResult.extractProjectsAndTheirVerboseDiagnostics(): String = buildString { var diagnosticStarted = false + val currentDiagnostic = StringBuilder() + + fun startDiagnostic(line: String, lineIndex: Int) { + require(!diagnosticStarted) { + printBuildOutput() + "Unexpected start of diagnostic $line on line ${lineIndex + 1}. The end of the previous diagnostic wasn't found yet" + } + + currentDiagnostic.appendLine(line) + diagnosticStarted = true + } + + fun continueDiagnostic(line: String) { + currentDiagnostic.appendLine(line) + } + + fun endDiagnostic(line: String, lineIndex: Int) { + require(diagnosticStarted) { + printBuildOutput() + "Unexpected end of diagnostic $line on line ${lineIndex + 1}" + } + + currentDiagnostic.appendLine(line) + currentDiagnostic.appendLine() + append(currentDiagnostic.toString()) + + currentDiagnostic.clear() + diagnosticStarted = false + } + + for ((index, line) in output.lines().withIndex()) { when { - line.trim() == VERBOSE_DIAGNOSTIC_SEPARATOR -> { - if (diagnosticStarted) { - appendLine(line) - appendLine() - diagnosticStarted = false - } else { - printBuildOutput() - error("Unexpected end of diagnostic $line on line ${index + 1}") - } - } + line.trim() == VERBOSE_DIAGNOSTIC_SEPARATOR -> endDiagnostic(line, index) - DIAGNOSTIC_START_REGEX.matches(line) -> { - if (!diagnosticStarted) { - appendLine(line) - diagnosticStarted = true - } else { - printBuildOutput() - error( - "Unexpected start of diagnostic $line on line ${index + 1}. The end of the previous diagnostic wasn't found yet" - ) - } - } + DIAGNOSTIC_START_REGEX.matches(line) -> startDiagnostic(line, index) - diagnosticStarted -> { - appendLine(line) - } + diagnosticStarted -> continueDiagnostic(line) line.startsWith(CONFIGURE_PROJECT_PREFIX) -> { appendLine() // additional empty line between projects