Skip to content

Commit c202314

Browse files
nav-navSpace Team
authored and
Space Team
committed
Add validation for empty kotlin.build.report.json.directory property
#KT-66314: Fixed
1 parent a77a1cf commit c202314

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ import org.jetbrains.kotlin.util.prefixIfNot
7171
import java.io.File
7272

7373
internal class PropertiesProvider private constructor(private val project: Project) {
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
@@ -666,6 +666,7 @@ internal class PropertiesProvider private constructor(private val project: Proje
666666
val KOTLIN_BUILD_REPORT_SINGLE_FILE = property("kotlin.build.report.single_file")
667667
val KOTLIN_BUILD_REPORT_HTTP_URL = property("kotlin.build.report.http.url")
668668
val KOTLIN_BUILD_REPORT_JSON_DIR = property("kotlin.build.report.json.directory")
669+
val KOTLIN_BUILD_REPORT_FILE_DIR = property("kotlin.build.report.file.output_dir")
669670
val KOTLIN_OPTIONS_SUPPRESS_FREEARGS_MODIFICATION_WARNING = property("kotlin.options.suppressFreeCompilerArgsModificationWarning")
670671
val KOTLIN_NATIVE_USE_XCODE_MESSAGE_STYLE = property("kotlin.native.useXcodeMessageStyle")
671672
val KOTLIN_INCREMENTAL_USE_CLASSPATH_SNAPSHOT = property("kotlin.incremental.useClasspathSnapshot")
@@ -729,5 +730,6 @@ internal class PropertiesProvider private constructor(private val project: Proje
729730
}
730731

731732
internal val Project.kotlinPropertiesProvider get() = invoke(this)
733+
732734
}
733735
}

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
@@ -11,11 +11,13 @@ import org.jetbrains.kotlin.build.report.HttpReportSettings
1111
import org.jetbrains.kotlin.build.report.metrics.GradleBuildPerformanceMetric
1212
import org.jetbrains.kotlin.build.report.metrics.GradleBuildTime
1313
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
14+
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_FILE_DIR
1415
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_SINGLE_FILE
1516
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_HTTP_URL
1617
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_JSON_DIR
1718
import org.jetbrains.kotlin.gradle.plugin.internal.isProjectIsolationEnabled
1819
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toUpperCaseAsciiOnly
20+
import java.io.File
1921

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

@@ -36,7 +38,10 @@ internal fun reportingSettings(project: Project): ReportingSettings {
3638
else -> BuildReportMode.VERBOSE
3739
}
3840
val fileReportSettings = if (buildReportOutputTypes.contains(BuildReportType.FILE)) {
39-
val buildReportDir = properties.buildReportFileOutputDir ?: (if (project.isProjectIsolationEnabled) {
41+
val buildReportDir = properties.buildReportFileOutputDir?.let {
42+
validateFileName(it, KOTLIN_BUILD_REPORT_FILE_DIR)
43+
File(it)
44+
} ?: (if (project.isProjectIsolationEnabled) {
4045
// TODO: it's a workaround for KT-52963, should be reworked – KT-55763
4146
project.rootDir.resolve("build")
4247
} else {
@@ -75,13 +80,17 @@ internal fun reportingSettings(project: Project): ReportingSettings {
7580
}
7681

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

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

8796
return ReportingSettings(
@@ -98,4 +107,11 @@ internal fun reportingSettings(project: Project): ReportingSettings {
98107
)
99108
}
100109

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

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.jetbrains.kotlin.util.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)