Skip to content

Commit

Permalink
Enable ktlint (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshafeinberg authored Feb 14, 2024
1 parent ae16557 commit 81972dd
Show file tree
Hide file tree
Showing 27 changed files with 138 additions and 129 deletions.
1 change: 1 addition & 0 deletions affectedmoduledetector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'kotlin'
id 'java-gradle-plugin'
id "com.vanniktech.maven.publish"
id "org.jlleitschuh.gradle.ktlint"
}

apply from: rootProject.file("gradle/jacoco.gradle")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ class AffectedModuleConfiguration {
var specifiedBranch: String? = null

var compareFrom: String = "PreviousCommit"
set(value) {
val commitShaProviders = listOf(
"PreviousCommit",
"ForkCommit",
"SpecifiedBranchCommit",
"SpecifiedBranchCommitMergeBase"
)
require(commitShaProviders.contains(value)) {
"The property configuration compareFrom must be one of the following: ${commitShaProviders.joinToString(", ")}"
}
if (value == "SpecifiedBranchCommit" || value == "SpecifiedBranchCommitMergeBase") {
requireNotNull(specifiedBranch) {
"Specify a branch using the configuration specifiedBranch"
set(value) {
val commitShaProviders = listOf(
"PreviousCommit",
"ForkCommit",
"SpecifiedBranchCommit",
"SpecifiedBranchCommitMergeBase"
)
require(commitShaProviders.contains(value)) {
"The property configuration compareFrom must be one of the following: ${commitShaProviders.joinToString(", ")}"
}
if (value == "SpecifiedBranchCommit" || value == "SpecifiedBranchCommitMergeBase") {
requireNotNull(specifiedBranch) {
"Specify a branch using the configuration specifiedBranch"
}
}
field = value
}
field = value
}

/**
* A set of modules that will not be considered in the build process, even if changes are made in them.
Expand Down Expand Up @@ -130,4 +130,4 @@ class AffectedModuleConfiguration {

const val name = "affectedModuleDetector"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ abstract class AffectedModuleDetector {
require(rootProject == rootProject.rootProject) {
"Project provided must be root, project was ${rootProject.path}"
}

val enabled = isProjectEnabled(rootProject)
if (!enabled) {
setInstance(
Expand Down Expand Up @@ -380,7 +380,7 @@ class AffectedModuleDetectorImpl constructor(
override fun hasAffectedProjects() = affectedProjects.isNotEmpty()

override fun isProjectProvided2(project: Project): Boolean {
if(modules == null ) return true
if (modules == null) return true
return modules.contains(project.path)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
else -> AffectedModuleDetector.isProjectAffected(pathOrNull.project)
}

if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) {
if (onlyIf && AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) {
task.dependsOn(path)
}
pathOrNull?.onlyIf { onlyIf }
Expand Down Expand Up @@ -223,7 +223,7 @@ class AffectedModuleDetectorPlugin : Plugin<Project> {
}

private fun getPathAndTask(project: Project, task: String?): String? {
return if (task.isNullOrBlank()) null else "${project.path}:${task}"
return if (task.isNullOrBlank()) null else "${project.path}:$task"
}

private fun filterAndroidTests(project: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package com.dropbox.affectedmoduledetector
*/
open class AffectedTestConfiguration {

var assembleAndroidTestTask : String? = DEFAULT_ASSEMBLE_ANDROID_TEST_TASK
var runAndroidTestTask : String? = DEFAULT_ANDROID_TEST_TASK
var jvmTestTask : String? = DEFAULT_JVM_TEST_TASK
var assembleAndroidTestTask: String? = DEFAULT_ASSEMBLE_ANDROID_TEST_TASK
var runAndroidTestTask: String? = DEFAULT_ANDROID_TEST_TASK
var jvmTestTask: String? = DEFAULT_JVM_TEST_TASK

companion object {
const val name = "affectedTestConfiguration"
Expand All @@ -20,4 +20,4 @@ open class AffectedTestConfiguration {
internal const val DEFAULT_ASSEMBLE_ANDROID_TEST_TASK = "assembleDebugAndroidTest"
internal const val DEFAULT_ANDROID_TEST_TASK = "connectedDebugAndroidTest"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ class DependencyTracker constructor(
project.configurations.forEach { config ->
logger?.info("checking config ${project.path}/$config for dependencies")
config
.dependencies
.filterIsInstance(ProjectDependency::class.java)
.forEach {
logger?.info("there is a dependency from ${project.path} to " +
it.dependencyProject.path)
result.getOrPut(it.dependencyProject) { mutableSetOf() }
.add(project)
}
.dependencies
.filterIsInstance(ProjectDependency::class.java)
.forEach {
logger?.info(
"there is a dependency from ${project.path} to " +
it.dependencyProject.path
)
result.getOrPut(it.dependencyProject) { mutableSetOf() }
.add(project)
}
}
}
result
Expand All @@ -61,9 +63,11 @@ class DependencyTracker constructor(
}
}
addAllDependents(project)
logger?.info("dependents of ${project.path} is ${result.map {
it.path
}}")
logger?.info(
"dependents of ${project.path} is ${result.map {
it.path
}}"
)
// the project isn't a dependent of itself
return result.minus(project)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal class GitClientImpl(
return ignoredFiles
.orEmpty()
.map { it.toRegex() }
.foldRight(changedFiles){ignoredFileRegex: Regex, fileList: List<String> ->
.foldRight(changedFiles) { ignoredFileRegex: Regex, fileList: List<String> ->
fileList.filterNot { it.matches(ignoredFileRegex) }
}
}
Expand Down Expand Up @@ -220,4 +220,4 @@ data class Commit(
private val commitSHADelimiter: String = "_CommitSHA:",
private val subjectDelimiter: String = "_Subject:",
private val authorEmailDelimiter: String = "_Author:"
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal enum class InternalTaskType(
override val commandByImpact: String,
override val originalGradleCommand: String,
override val taskDescription: String
): AffectedModuleTaskType {
) : AffectedModuleTaskType {

ANDROID_TEST(
commandByImpact = "runAffectedAndroidTests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ import org.gradle.api.services.BuildServiceParameters
import org.gradle.internal.build.event.BuildEventListenerRegistryInternal
import org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger
import org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext
import org.gradle.internal.operations.*
import org.gradle.internal.operations.BuildOperationDescriptor
import org.gradle.internal.operations.BuildOperationListener
import org.gradle.internal.operations.OperationFinishEvent
import org.gradle.internal.operations.OperationIdentifier
import org.gradle.internal.operations.OperationProgressEvent
import org.gradle.internal.operations.OperationStartEvent
import org.gradle.internal.time.Clock
import org.gradle.invocation.DefaultGradle
import java.io.File
Expand All @@ -42,26 +47,25 @@ import java.io.File
internal open class ToStringLogger(
private val loggerProvider: Provider<ToStringLoggerBuildService>?
) : OutputEventListenerBackedLogger(
"amd",
OutputEventListenerBackedLoggerContext {
System.currentTimeMillis()
}.also {
it.level = LogLevel.DEBUG
it.setOutputEventListener { outputEvent ->
loggerProvider?.get()?.parameters?.getStringBuilderProperty()?.get()?.appendLine(outputEvent.toString())
}
},
Clock {
System.currentTimeMillis()
"amd",
OutputEventListenerBackedLoggerContext {
System.currentTimeMillis()
}.also {
it.level = LogLevel.DEBUG
it.setOutputEventListener { outputEvent ->
loggerProvider?.get()?.parameters?.getStringBuilderProperty()?.get()?.appendLine(outputEvent.toString())
}
},
Clock {
System.currentTimeMillis()
}
) {

/**
* Returns the current log.
*/
fun buildString() = loggerProvider?.get()?.parameters?.getStringBuilderProperty()?.get()?.toString()

@Suppress("UnstableApiUsage") // BuildService is not yet stable
companion object {
internal abstract class ToStringLoggerBuildService : BuildService<ToStringLoggerBuildService.ToStringLoggerBuildServiceParameters>, BuildOperationListener, AutoCloseable {
interface ToStringLoggerBuildServiceParameters : BuildServiceParameters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.dropbox.affectedmoduledetector.commitshaproviders
import com.dropbox.affectedmoduledetector.GitClient
import com.dropbox.affectedmoduledetector.Sha

class ForkCommit: CommitShaProvider {
class ForkCommit : CommitShaProvider {
override fun get(commandRunner: GitClient.CommandRunner): Sha {
val currentBranch = commandRunner.executeAndParseFirst(CURRENT_BRANCH_CMD)

Expand All @@ -25,4 +25,4 @@ class ForkCommit: CommitShaProvider {
const val CURRENT_BRANCH_CMD = "git rev-parse --abbrev-ref HEAD"
const val SHOW_ALL_BRANCHES_CMD = "git show-branch -a"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.dropbox.affectedmoduledetector.commitshaproviders
import com.dropbox.affectedmoduledetector.GitClient
import com.dropbox.affectedmoduledetector.Sha

class PreviousCommit: CommitShaProvider {
class PreviousCommit : CommitShaProvider {
override fun get(commandRunner: GitClient.CommandRunner): Sha {
return commandRunner.executeAndParseFirst(PREV_COMMIT_CMD)
}
companion object {
const val PREV_COMMIT_CMD = "git --no-pager rev-parse HEAD~1"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class SpecifiedBranchCommit(private val branch: String) : CommitShaProvider {
override fun get(commandRunner: GitClient.CommandRunner): Sha {
return commandRunner.executeAndParseFirst("git rev-parse $branch")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ fun String.toPathSections(rootProjectDir: File, gitRootDir: File): List<String>
}
}
return realSections.toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ fun String.toOsSpecificPath(): String {
*/
fun String.toOsSpecificLineEnding(): String {
return this.replace("\n", System.lineSeparator())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AffectedModuleConfigurationTest {
@get:Rule
val tmpFolder = TemporaryFolder()

private lateinit var config : AffectedModuleConfiguration
private lateinit var config: AffectedModuleConfiguration

private val FAKE_TASK = AffectedModuleConfiguration.CustomTask(
commandByImpact = "runFakeTask",
Expand Down Expand Up @@ -122,7 +122,6 @@ class AffectedModuleConfigurationTest {
fail("Expected to catch an exception")
}


@Test
fun `GIVEN AffectedModuleConfiguration WHEN base dir is set and paths affecting module is set THEN succeeds`() {
// GIVEN
Expand Down Expand Up @@ -159,7 +158,6 @@ class AffectedModuleConfigurationTest {
fail("Expected to catch an exception")
}


@Test
fun `GIVEN AffectedModuleConfiguration WHEN companion object name is returned THEN affectedModuleDetector is returned`() {
assertThat(AffectedModuleConfiguration.name).isEqualTo("affectedModuleDetector")
Expand Down Expand Up @@ -231,7 +229,6 @@ class AffectedModuleConfigurationTest {
}

fail("Expected to catch an exception")

}

@Test
Expand Down Expand Up @@ -327,4 +324,4 @@ class AffectedModuleConfigurationTest {

assert(actual.first().taskDescription == "Description of fake task")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.lang.IllegalStateException
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.lang.IllegalStateException

@RunWith(JUnit4::class)
class AffectedModuleDetectorPluginTest {
Expand Down Expand Up @@ -197,7 +197,7 @@ class AffectedModuleDetectorPluginTest {

val customTasks = rootProject
.tasks
.filter { it.group == AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME }
.filter { it.group == AffectedModuleDetectorPlugin.CUSTOM_TASK_GROUP_NAME }

// THEN
assert(customTasks.size == 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.junit.Test

class AffectedTestConfigurationTest {

private lateinit var config : AffectedTestConfiguration
private lateinit var config: AffectedTestConfiguration

@Before
fun setup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dropbox.affectedmoduledetector

import com.nhaarman.mockito_kotlin.mock
import org.gradle.api.provider.Provider
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
Expand All @@ -10,7 +9,7 @@ import org.junit.runners.model.Statement
* Special rule for dependency detector tests that will attach logs to a failure.
*/
class AttachLogsTestRule : TestRule {
internal val logger = mock<ToStringLogger> { }
internal val logger = mock<ToStringLogger> { }
override fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
override fun evaluate() {
Expand All @@ -22,7 +21,7 @@ class AttachLogsTestRule : TestRule {
test failed with msg: ${t.message}
logs:
${logger.buildString()}
""".trimIndent(),
""".trimIndent(),
t
)
}
Expand Down
Loading

0 comments on commit 81972dd

Please sign in to comment.