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

Add tests for missing rules in a ruleset #477

Merged
merged 1 commit into from
Jul 5, 2019
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
@@ -0,0 +1,8 @@
package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.test.RuleSetProviderTest

class ExperimentalRuleSetProviderTest : RuleSetProviderTest(
rulesetClass = ExperimentalRuleSetProvider::class.java,
packageName = "com.pinterest.ktlint.ruleset.experimental"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.test.RuleSetProviderTest

class StandardRuleSetProviderTest : RuleSetProviderTest(
rulesetClass = StandardRuleSetProvider::class.java,
packageName = "com.pinterest.ktlint.ruleset.standard"
)
1 change: 1 addition & 0 deletions ktlint-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ dependencies {
implementation deps.kotlin.stdlib
implementation deps.kotlin.compiler
implementation deps.assertj
implementation deps.junit
}
5 changes: 5 additions & 0 deletions ktlint-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pinterest.ktlint.test

import com.pinterest.ktlint.core.RuleSetProvider
import java.io.File
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

open class RuleSetProviderTest(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this is the best way for the centralized test code - this could be just a simple top-level function, which may be called from within a test method (e.g. in case StandardRuleSetProviderTest contains other tests), so I'm open for changing this, but currently it works.

private val rulesetClass: Class<out RuleSetProvider>,
private val packageName: String
Copy link
Collaborator Author

@romtsn romtsn Jun 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We could, in theory, get the package name from rulesetClass and this would work with current ktlint structure, but in case of custom rulesets, the rules could be located in a different folder, so I would rather leave it like that

) {

@Test
fun checkAllRulesProvided() {
val srcLocation = rulesetClass.protectionDomain.codeSource.location.path
val rulesDir = File(srcLocation + packageName.replace(".", "/"))
val packageRules = rulesDir.listFiles()
?.map { it.name.removeSuffix(".class") }
?.filter { it.endsWith("Rule") }
?: arrayListOf()

val provider = rulesetClass
val providerRules = provider.newInstance().get().rules.map { it::class.java.simpleName }
val diff = packageRules - providerRules
assertThat(diff)
.withFailMessage("%s is missing to provide the following rules: \n%s", provider.simpleName, diff.joinToString(separator = "\n"))
.hasSize(0)
}
}