Skip to content

Commit f327391

Browse files
nav-navSpace Team
authored and
Space Team
committed
Add validation for empty kotlin.build.report.json.directory property
#KT-66314: Fixed (cherry picked from commit c202314)
1 parent 2f19d2e commit f327391

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/PropertiesProvider.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ internal class PropertiesProvider private constructor(private val project: Proje
7171
val singleBuildMetricsFile: File?
7272
get() = property("kotlin.internal.single.build.metrics.file").orNull?.let { File(it) }
7373

74-
val buildReportSingleFile: File?
75-
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_SINGLE_FILE).orNull?.let { File(it) }
74+
val buildReportSingleFile: String?
75+
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_SINGLE_FILE).orNull
7676

7777
val buildReportOutputs: List<String>
7878
get() = property("kotlin.build.report.output").orNull?.split(",") ?: emptyList()
7979

8080
val buildReportLabel: String?
8181
get() = property("kotlin.build.report.label").orNull
8282

83-
val buildReportFileOutputDir: File?
84-
get() = property("kotlin.build.report.file.output_dir").orNull?.let { File(it) }
83+
val buildReportFileOutputDir: String?
84+
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_FILE_DIR).orNull
8585

8686
val buildReportHttpUrl: String?
8787
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_HTTP_URL).orNull
@@ -110,8 +110,8 @@ internal class PropertiesProvider private constructor(private val project: Proje
110110
val buildReportMetrics: Boolean
111111
get() = booleanProperty("kotlin.build.report.metrics") ?: false
112112

113-
val buildReportJsonDir: File?
114-
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_JSON_DIR).orNull?.let { File(it) }
113+
val buildReportJsonDir: String?
114+
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_JSON_DIR).orNull
115115

116116
val buildReportVerbose: Boolean
117117
get() = booleanProperty("kotlin.build.report.verbose") ?: false
@@ -627,6 +627,7 @@ internal class PropertiesProvider private constructor(private val project: Proje
627627
val KOTLIN_BUILD_REPORT_SINGLE_FILE = property("kotlin.build.report.single_file")
628628
val KOTLIN_BUILD_REPORT_HTTP_URL = property("kotlin.build.report.http.url")
629629
val KOTLIN_BUILD_REPORT_JSON_DIR = property("kotlin.build.report.json.directory")
630+
val KOTLIN_BUILD_REPORT_FILE_DIR = property("kotlin.build.report.file.output_dir")
630631
val KOTLIN_OPTIONS_SUPPRESS_FREEARGS_MODIFICATION_WARNING = property("kotlin.options.suppressFreeCompilerArgsModificationWarning")
631632
val KOTLIN_NATIVE_USE_XCODE_MESSAGE_STYLE = property("kotlin.native.useXcodeMessageStyle")
632633
val KOTLIN_COMPILER_USE_PRECISE_COMPILATION_RESULTS_BACKUP = property("kotlin.compiler.preciseCompilationResultsBackup")

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/configureReporing.kt

+21-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import org.jetbrains.kotlin.build.report.metrics.BuildTime
1313
import org.jetbrains.kotlin.build.report.metrics.GradleBuildPerformanceMetric
1414
import org.jetbrains.kotlin.build.report.metrics.GradleBuildTime
1515
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
16+
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_FILE_DIR
1617
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_SINGLE_FILE
1718
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_HTTP_URL
1819
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_JSON_DIR
1920
import org.jetbrains.kotlin.gradle.plugin.internal.isProjectIsolationEnabled
2021
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toUpperCaseAsciiOnly
22+
import java.io.File
2123

2224
private val availableMetrics = GradleBuildTime.values().map { it.name } + GradleBuildPerformanceMetric.values().map { it.name }
2325

@@ -38,7 +40,10 @@ internal fun reportingSettings(project: Project): ReportingSettings {
3840
else -> BuildReportMode.VERBOSE
3941
}
4042
val fileReportSettings = if (buildReportOutputTypes.contains(BuildReportType.FILE)) {
41-
val buildReportDir = properties.buildReportFileOutputDir ?: (if (project.isProjectIsolationEnabled) {
43+
val buildReportDir = properties.buildReportFileOutputDir?.let {
44+
validateFileName(it, KOTLIN_BUILD_REPORT_FILE_DIR)
45+
File(it)
46+
} ?: (if (project.isProjectIsolationEnabled) {
4247
// TODO: it's a workaround for KT-52963, should be reworked – KT-55763
4348
project.rootDir.resolve("build")
4449
} else {
@@ -74,13 +79,17 @@ internal fun reportingSettings(project: Project): ReportingSettings {
7479
}
7580

7681
val singleOutputFile = if (buildReportOutputTypes.contains(BuildReportType.SINGLE_FILE)) {
77-
properties.buildReportSingleFile
78-
?: throw IllegalStateException("Can't configure single file report: '$KOTLIN_BUILD_REPORT_SINGLE_FILE' property is mandatory")
82+
properties.buildReportSingleFile?.let {
83+
validateFileName(it, KOTLIN_BUILD_REPORT_SINGLE_FILE)
84+
File(it)
85+
} ?: throw IllegalStateException("Can't configure single file report: '$KOTLIN_BUILD_REPORT_SINGLE_FILE' property is mandatory")
7986
} else null
8087

8188
val jsonReportDir = if (buildReportOutputTypes.contains(BuildReportType.JSON)) {
82-
properties.buildReportJsonDir
83-
?: throw IllegalStateException("Can't configure json report: '$KOTLIN_BUILD_REPORT_JSON_DIR' property is mandatory")
89+
properties.buildReportJsonDir?.let {
90+
validateFileName(it, KOTLIN_BUILD_REPORT_JSON_DIR)
91+
File(it)
92+
} ?: throw IllegalStateException("Can't configure json report: '$KOTLIN_BUILD_REPORT_JSON_DIR' property is mandatory")
8493
} else null
8594

8695
//temporary solution. support old property
@@ -100,4 +109,11 @@ internal fun reportingSettings(project: Project): ReportingSettings {
100109
)
101110
}
102111

112+
private fun validateFileName(fileName: String, propertyName: String) {
113+
if (fileName.isBlank()) {
114+
throw IllegalStateException("The property '$propertyName' must not be empty. Please provide a valid value.")
115+
}
116+
}
117+
118+
103119

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.gradle.unitTests.report
7+
8+
import org.gradle.api.internal.plugins.PluginApplicationException
9+
import org.jetbrains.kotlin.gradle.util.applyKotlinJvmPlugin
10+
import org.jetbrains.kotlin.gradle.util.buildProject
11+
import org.jetbrains.kotlin.gradle.util.propertiesExtension
12+
import org.junit.jupiter.api.assertThrows
13+
import kotlin.test.Test
14+
import kotlin.test.assertEquals
15+
16+
class ConfigureReportingTest {
17+
18+
@Test
19+
fun validateMandatoryJsonDirectory() {
20+
val exception =
21+
assertThrows<PluginApplicationException>("The property 'kotlin.build.report.json.directory' is mandatory for JSON output. Validation should fail.") {
22+
buildProject {
23+
propertiesExtension.set("kotlin.build.report.output", "json")
24+
applyKotlinJvmPlugin()
25+
}
26+
}
27+
28+
assertEquals("Can't configure json report: 'kotlin.build.report.json.directory' property is mandatory", exception.cause?.message)
29+
}
30+
31+
@Test
32+
fun validateInvalidJsonDirectory() {
33+
val exception =
34+
assertThrows<PluginApplicationException>("The property 'kotlin.build.report.json.directory' should not be empty. Validation should fail.") {
35+
buildProject {
36+
propertiesExtension.set("kotlin.build.report.output", "json")
37+
propertiesExtension.set("kotlin.build.report.json.directory", "")
38+
applyKotlinJvmPlugin()
39+
}
40+
}
41+
42+
assertEquals("The property 'kotlin.build.report.json.directory' must not be empty. Please provide a valid value.", exception.cause?.message)
43+
}
44+
}

0 commit comments

Comments
 (0)