Skip to content

Commit

Permalink
Add validation for empty kotlin.build.report.json.directory property
Browse files Browse the repository at this point in the history
#KT-66314: Fixed
  • Loading branch information
nav-nav authored and qodana-bot committed Mar 5, 2024
1 parent a77a1cf commit c202314
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ import org.jetbrains.kotlin.util.prefixIfNot
import java.io.File

internal class PropertiesProvider private constructor(private val project: Project) {
val buildReportSingleFile: File?
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_SINGLE_FILE).orNull?.let { File(it) }
val buildReportSingleFile: String?
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_SINGLE_FILE).orNull

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

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

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

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

val buildReportJsonDir: File?
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_JSON_DIR).orNull?.let { File(it) }
val buildReportJsonDir: String?
get() = property(PropertyNames.KOTLIN_BUILD_REPORT_JSON_DIR).orNull

val buildReportVerbose: Boolean
get() = booleanProperty("kotlin.build.report.verbose") ?: false
Expand Down Expand Up @@ -666,6 +666,7 @@ internal class PropertiesProvider private constructor(private val project: Proje
val KOTLIN_BUILD_REPORT_SINGLE_FILE = property("kotlin.build.report.single_file")
val KOTLIN_BUILD_REPORT_HTTP_URL = property("kotlin.build.report.http.url")
val KOTLIN_BUILD_REPORT_JSON_DIR = property("kotlin.build.report.json.directory")
val KOTLIN_BUILD_REPORT_FILE_DIR = property("kotlin.build.report.file.output_dir")
val KOTLIN_OPTIONS_SUPPRESS_FREEARGS_MODIFICATION_WARNING = property("kotlin.options.suppressFreeCompilerArgsModificationWarning")
val KOTLIN_NATIVE_USE_XCODE_MESSAGE_STYLE = property("kotlin.native.useXcodeMessageStyle")
val KOTLIN_INCREMENTAL_USE_CLASSPATH_SNAPSHOT = property("kotlin.incremental.useClasspathSnapshot")
Expand Down Expand Up @@ -729,5 +730,6 @@ internal class PropertiesProvider private constructor(private val project: Proje
}

internal val Project.kotlinPropertiesProvider get() = invoke(this)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import org.jetbrains.kotlin.build.report.HttpReportSettings
import org.jetbrains.kotlin.build.report.metrics.GradleBuildPerformanceMetric
import org.jetbrains.kotlin.build.report.metrics.GradleBuildTime
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_FILE_DIR
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_SINGLE_FILE
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_HTTP_URL
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_BUILD_REPORT_JSON_DIR
import org.jetbrains.kotlin.gradle.plugin.internal.isProjectIsolationEnabled
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toUpperCaseAsciiOnly
import java.io.File

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

Expand All @@ -36,7 +38,10 @@ internal fun reportingSettings(project: Project): ReportingSettings {
else -> BuildReportMode.VERBOSE
}
val fileReportSettings = if (buildReportOutputTypes.contains(BuildReportType.FILE)) {
val buildReportDir = properties.buildReportFileOutputDir ?: (if (project.isProjectIsolationEnabled) {
val buildReportDir = properties.buildReportFileOutputDir?.let {
validateFileName(it, KOTLIN_BUILD_REPORT_FILE_DIR)
File(it)
} ?: (if (project.isProjectIsolationEnabled) {
// TODO: it's a workaround for KT-52963, should be reworked – KT-55763
project.rootDir.resolve("build")
} else {
Expand Down Expand Up @@ -75,13 +80,17 @@ internal fun reportingSettings(project: Project): ReportingSettings {
}

val singleOutputFile = if (buildReportOutputTypes.contains(BuildReportType.SINGLE_FILE)) {
properties.buildReportSingleFile
?: throw IllegalStateException("Can't configure single file report: '$KOTLIN_BUILD_REPORT_SINGLE_FILE' property is mandatory")
properties.buildReportSingleFile?.let {
validateFileName(it, KOTLIN_BUILD_REPORT_SINGLE_FILE)
File(it)
} ?: throw IllegalStateException("Can't configure single file report: '$KOTLIN_BUILD_REPORT_SINGLE_FILE' property is mandatory")
} else null

val jsonReportDir = if (buildReportOutputTypes.contains(BuildReportType.JSON)) {
properties.buildReportJsonDir
?: throw IllegalStateException("Can't configure json report: '$KOTLIN_BUILD_REPORT_JSON_DIR' property is mandatory")
properties.buildReportJsonDir?.let {
validateFileName(it, KOTLIN_BUILD_REPORT_JSON_DIR)
File(it)
} ?: throw IllegalStateException("Can't configure json report: '$KOTLIN_BUILD_REPORT_JSON_DIR' property is mandatory")
} else null

return ReportingSettings(
Expand All @@ -98,4 +107,11 @@ internal fun reportingSettings(project: Project): ReportingSettings {
)
}

private fun validateFileName(fileName: String, propertyName: String) {
if (fileName.isBlank()) {
throw IllegalStateException("The property '$propertyName' must not be empty. Please provide a valid value.")
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlin.gradle.unitTests.report

import org.gradle.api.internal.plugins.PluginApplicationException
import org.jetbrains.kotlin.gradle.util.applyKotlinJvmPlugin
import org.jetbrains.kotlin.gradle.util.buildProject
import org.jetbrains.kotlin.gradle.util.propertiesExtension
import org.jetbrains.kotlin.util.assertThrows
import kotlin.test.Test
import kotlin.test.assertEquals

class ConfigureReportingTest {

@Test
fun validateMandatoryJsonDirectory() {
val exception =
assertThrows<PluginApplicationException>("The property 'kotlin.build.report.json.directory' is mandatory for JSON output. Validation should fail.") {
buildProject {
propertiesExtension.set("kotlin.build.report.output", "json")
applyKotlinJvmPlugin()
}
}

assertEquals("Can't configure json report: 'kotlin.build.report.json.directory' property is mandatory", exception.cause?.message)
}

@Test
fun validateInvalidJsonDirectory() {
val exception =
assertThrows<PluginApplicationException>("The property 'kotlin.build.report.json.directory' should not be empty. Validation should fail.") {
buildProject {
propertiesExtension.set("kotlin.build.report.output", "json")
propertiesExtension.set("kotlin.build.report.json.directory", "")
applyKotlinJvmPlugin()
}
}

assertEquals("The property 'kotlin.build.report.json.directory' must not be empty. Please provide a valid value.", exception.cause?.message)
}
}

0 comments on commit c202314

Please sign in to comment.