Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a full path in DiktatReporter #1798

Merged
merged 8 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.io.path.inputStream
*/
data class DiktatRunnerArguments(
val configInputStream: InputStream,
val sourceRootDir: Path,
val sourceRootDir: Path?,
nulls marked this conversation as resolved.
Show resolved Hide resolved
val files: Collection<Path>,
val baselineFile: Path?,
val reporterType: String,
Expand All @@ -32,7 +32,7 @@ data class DiktatRunnerArguments(
) {
constructor(
configFile: Path,
sourceRootDir: Path,
sourceRootDir: Path?,
files: Collection<Path>,
baselineFile: Path?,
reporterType: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DiktatRunnerFactory(

private fun resolveBaseline(
baselineFile: Path?,
sourceRootDir: Path,
sourceRootDir: Path?,
): Pair<DiktatBaseline, DiktatProcessorListener> = baselineFile
?.let { diktatBaselineFactory.tryToLoad(it, sourceRootDir) }
?.let { it to DiktatProcessorListener.empty }
Expand All @@ -68,7 +68,7 @@ class DiktatRunnerFactory(
reporterOutput: OutputStream?,
colorNameInPlain: String?,
groupByFileInPlain: Boolean?,
sourceRootDir: Path,
sourceRootDir: Path?,
): DiktatReporter {
val (outputStream, closeOutputStream) = reporterOutput?.let { it to true } ?: (System.`out` to false)
return if (reporterType == diktatReporterFactory.plainId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface DiktatBaselineFactory {
*/
fun tryToLoad(
baselineFile: Path,
sourceRootDir: Path,
sourceRootDir: Path?,
): DiktatBaseline?

/**
Expand All @@ -23,6 +23,6 @@ interface DiktatBaselineFactory {
*/
fun generator(
baselineFile: Path,
sourceRootDir: Path,
sourceRootDir: Path?,
): DiktatProcessorListener
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ typealias DiktatReporter = DiktatProcessorListener
/**
* A factory to create [DiktatReporter]
*/
interface DiktatReporterFactory : Function4<String, OutputStream, Boolean, Path, DiktatReporter> {
interface DiktatReporterFactory : Function4<String, OutputStream, Boolean, Path?, DiktatReporter> {
/**
* Set of supported IDs
*/
Expand All @@ -35,7 +35,7 @@ interface DiktatReporterFactory : Function4<String, OutputStream, Boolean, Path,
id: String,
outputStream: OutputStream,
closeOutputStreamAfterAll: Boolean,
sourceRootDir: Path,
sourceRootDir: Path?,
): DiktatReporter

/**
Expand All @@ -49,7 +49,7 @@ interface DiktatReporterFactory : Function4<String, OutputStream, Boolean, Path,
fun createPlain(
outputStream: OutputStream,
closeOutputStreamAfterAll: Boolean,
sourceRootDir: Path,
sourceRootDir: Path?,
colorName: String? = null,
groupByFile: Boolean? = null,
): DiktatReporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import groovy.lang.Closure
import org.gradle.api.Project
import java.io.File
import java.nio.file.Files
import java.nio.file.Path

@Suppress(
"MISSING_KDOC_TOP_LEVEL",
Expand Down Expand Up @@ -39,6 +40,16 @@ class KotlinClosure1<in T : Any?, V : Any>(
fun <T> Any.closureOf(action: T.() -> Unit): Closure<Any?> =
KotlinClosure1(action, this, this)

/**
* @param diktatExtension
* @return returns sourceRootDir as projectDir for sarif report
*/
fun Project.getSourceRootDir(diktatExtension: DiktatExtension): Path? = when {
diktatExtension.githubActions -> projectDir.toPath()
diktatExtension.reporter == "sarif" -> projectDir.toPath()
else -> null
}

/**
* Create CLI flag to set reporter for ktlint based on [diktatExtension].
* [DiktatExtension.githubActions] should have higher priority than a custom input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl
import com.saveourtool.diktat.plugin.gradle.DiktatExtension
import com.saveourtool.diktat.plugin.gradle.getOutputFile
import com.saveourtool.diktat.plugin.gradle.getReporterType
import com.saveourtool.diktat.plugin.gradle.getSourceRootDir
import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl
import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl

Expand Down Expand Up @@ -79,7 +80,7 @@ abstract class DiktatTaskBase(
private val diktatRunnerArguments by lazy {
DiktatRunnerArguments(
configFile = extension.diktatConfigFile.toPath(),
sourceRootDir = project.projectDir.toPath(),
sourceRootDir = project.getSourceRootDir(extension),
files = actualInputs.files.map { it.toPath() },
baselineFile = extension.baseline?.let { project.file(it).toPath() },
reporterType = project.getReporterType(extension),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

override fun tryToLoad(
baselineFile: Path,
sourceRootDir: Path,
sourceRootDir: Path?,
): DiktatBaseline? = loadBaseline(baselineFile.absolutePathString())
.takeIf { it.status == Baseline.Status.VALID }
?.let { ktLintBaseline ->
Expand All @@ -34,6 +34,12 @@
}
}

override fun generator(baselineFile: Path, sourceRootDir: Path): DiktatProcessorListener =
baselineReporterProvider.get(baselineFile.outputStream(), closeOutAfterAll = true, emptyMap()).wrap(sourceRootDir)
override fun generator(
baselineFile: Path,
sourceRootDir: Path?,
): DiktatProcessorListener = baselineReporterProvider.get(
baselineFile.outputStream(),
closeOutAfterAll = true,
emptyMap(),
).wrap(sourceRootDir)

Check warning on line 44 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatBaselineFactoryImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatBaselineFactoryImpl.kt#L40-L44

Added lines #L40 - L44 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
get() = plainReporterProvider.id

override val colorNamesInPlain: Set<String>
get() = Color.values().map { it.name }.toSet()
get() = Color.entries.map { it.name }.toSet()

Check warning on line 41 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterFactoryImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterFactoryImpl.kt#L41

Added line #L41 was not covered by tests

override fun invoke(
id: String,
outputStream: OutputStream,
closeOutputStreamAfterAll: Boolean,
sourceRootDir: Path,
sourceRootDir: Path?,
): DiktatReporter {
val reporterProvider = reporterProviders[id] ?: throw IllegalArgumentException("Not supported reporter id by ${DiktatBaselineFactoryImpl::class.simpleName}")
if (reporterProvider is SarifReporterProvider) {
System.setProperty("user.home", sourceRootDir.pathString)
sourceRootDir?.let { System.setProperty("user.home", it.pathString) }
}
val opt = if (reporterProvider is PlainReporterProvider) {
mapOf("color_name" to Color.DARK_GRAY.name)
Expand All @@ -61,7 +61,7 @@
override fun createPlain(
outputStream: OutputStream,
closeOutputStreamAfterAll: Boolean,
sourceRootDir: Path,
sourceRootDir: Path?,
colorName: String?,
groupByFile: Boolean?,
): DiktatReporter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class DiktatReporterImpl(
private val ktLintReporter: ReporterV2,
private val sourceRootDir: Path,
private val sourceRootDir: Path?,

Check warning on line 17 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt#L17

Added line #L17 was not covered by tests
) : DiktatReporter {
override fun beforeAll(files: Collection<Path>): Unit = ktLintReporter.beforeAll()
override fun before(file: Path): Unit = ktLintReporter.before(file.relativePathStringTo(sourceRootDir))
Expand All @@ -31,7 +31,7 @@
* @param sourceRootDir
* @return [DiktatReporter] which wraps __KtLint__'s [ReporterV2]
*/
fun ReporterV2.wrap(sourceRootDir: Path): DiktatReporter = DiktatReporterImpl(this, sourceRootDir)
fun ReporterV2.wrap(sourceRootDir: Path?): DiktatReporter = DiktatReporterImpl(this, sourceRootDir)

Check warning on line 34 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt#L34

Added line #L34 was not covered by tests

/**
* @return __KtLint__'s [ReporterV2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fun String.correctErrorDetail(canBeAutoCorrected: Boolean): String = if (canBeAu
* @param sourceRootDir
* @return relative path to [sourceRootDir] as [String]
*/
fun Path.relativePathStringTo(sourceRootDir: Path): String = relativeTo(sourceRootDir).invariantSeparatorsPathString
fun Path.relativePathStringTo(sourceRootDir: Path?): String = (sourceRootDir?.let { relativeTo(it) } ?: this).invariantSeparatorsPathString

/**
* @param out [OutputStream] for [ReporterV2]
Expand Down
Loading