Skip to content

Commit

Permalink
Detekt: add ability to specify input directory (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
budnyjj committed Mar 11, 2021
1 parent 013fbb4 commit 3afbca2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ fun hasLintPlugin(): Boolean {
}
}

fun Project.kotlinFiles() = fileTree(mapOf("dir" to ".", "exclude" to "**/build/**", "includes" to listOf("**/*.kt", "**/*.kts")))
fun Project.kotlinFiles(baseDir: String = ".") =
fileTree(mapOf("dir" to baseDir, "exclude" to "**/build/**", "includes" to listOf("**/*.kt", "**/*.kts")))

fun Project.editorconfigFile() = fileTree(mapOf("dir" to ".", "include" to ".editorconfig"))

Expand Down Expand Up @@ -321,7 +322,8 @@ fun Project.addDetekt(rootProject: Project, extension: CodeQualityToolsPluginExt
task.version = extension.detekt.toolVersion
task.outputDirectory = File(buildDir, "reports/detekt/")
task.configFile = rootProject.file(extension.detekt.config)
task.inputs.files(kotlinFiles())
task.input = extension.detekt.input
task.inputs.files(kotlinFiles(baseDir = extension.detekt.input))

task.inputs.property("baseline-file-exists", false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.gradle.api.tasks.TaskExecutionException
import java.io.File

@CacheableTask open class DetektCheckTask : DefaultTask() {
@Input var input: String = "."
@Input var failFast: Boolean = true
@Input var buildUponDefaultConfig: Boolean = false
@Input var parallel: Boolean = false
Expand Down Expand Up @@ -57,7 +58,7 @@ import java.io.File
task.main = "io.gitlab.arturbosch.detekt.cli.Main"
task.classpath = configuration
task.args(
"--input", project.file("."),
"--input", project.file(input),
reportKey, reportValue
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ open class DetektExtension {
* @since 0.19.0
*/
var parallel: Boolean = false

/**
* Directories to look for source files.
* Defaults to current directory.
*
* @since 0.21.0
*/
var input: String = "."
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,29 @@ class CodeQualityToolsPluginDetektTest {
.fails(taskToRun = "check", taskToCheck = "detektCheck", containsMessage = "NewLineAtEndOfFile - [Foo.kt] at")
}

@Test fun runningWithExplicitInputSucceeds() {
Roboter(testProjectDir, inputDirectoryName = testProjectDir.root.toString())
.withConfiguration("failFast: true")
.withKotlinFile(testPath, testCode)
.succeeds()
}

@Test fun failsWithUnexistentInput() {
Roboter(testProjectDir, inputDirectoryName = "unexistent")
.withConfiguration("failFast: true")
.withKotlinFile(testPath, testCode)
.fails(containsMessage = "does not exist", assertReportsExist = false)
}

class Roboter(
private val directory: TemporaryFolder,
private val config: String = "code_quality_tools/detekt.yml",
enabled: Boolean = true,
version: String = "1.0.0",
private val baselineFileName: String? = null,
buildUponDefaultConfig: Boolean = false,
parallel: Boolean = false
parallel: Boolean = false,
inputDirectoryName: String = "."
) {
init {
directory.newFile("build.gradle").writeText("""
Expand All @@ -137,6 +152,7 @@ class CodeQualityToolsPluginDetektTest {
| config = "$config"
| toolVersion = "$version"
| baselineFileName = ${baselineFileName.wrap("\"")}
| input = ${inputDirectoryName.wrap("\"")}
| }
| ktlint.enabled = false
| checkstyle.enabled = false
Expand Down Expand Up @@ -168,11 +184,18 @@ class CodeQualityToolsPluginDetektTest {
assertReportsExist()
}

fun fails(taskToRun: String = "detektCheck", taskToCheck: String = taskToRun, containsMessage: String) = apply {
fun fails(
taskToRun: String = "detektCheck",
taskToCheck: String = taskToRun,
assertReportsExist: Boolean = true,
containsMessage: String
) = apply {
val buildResult = run(taskToRun).buildAndFail()
assertThat(buildResult.task(":$taskToCheck")?.outcome).isEqualTo(TaskOutcome.FAILED)
assertThat(buildResult.output).contains(containsMessage)
assertReportsExist()
if (assertReportsExist) {
assertReportsExist()
}
}

private fun assertReportsExist() {
Expand Down

0 comments on commit 3afbca2

Please sign in to comment.