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

update InvalidPackageDeclaration to report if rootPackage is not present #4484

Merged
merged 5 commits into from Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,7 +25,7 @@ class InvalidPackageDeclaration(config: Config = Config.empty) : Rule(config) {
debt = Debt.FIVE_MINS
)

@Configuration("if specified this part of the package structure is ignored")
@Configuration("if specified the declaration must start with this root, but it is ignored in the file structure")
mateot1 marked this conversation as resolved.
Show resolved Hide resolved
private val rootPackage: String by config("")

override fun visitPackageDirective(directive: KtPackageDirective) {
Expand All @@ -34,26 +34,28 @@ class InvalidPackageDeclaration(config: Config = Config.empty) : Rule(config) {
if (declaredPath.isNotBlank()) {
val normalizedFilePath = directive.containingKtFile.absolutePath().parent.toNormalizedForm()
val normalizedRootPackage = packageNameToNormalizedForm(rootPackage)
val expectedPath =
if (normalizedRootPackage.isBlank()) {
declaredPath
} else {
declaredPath.substringAfter(normalizedRootPackage)
val expectedPath = when {
normalizedRootPackage.isBlank() -> declaredPath
!declaredPath.startsWith(normalizedRootPackage) -> {
directive.reportInvalidPackageDeclaration("The package declaration is missing the root package")
return
}
else -> declaredPath.substringAfter(normalizedRootPackage)
}
mateot1 marked this conversation as resolved.
Show resolved Hide resolved

val isInRootPackage = expectedPath.isBlank()
if (!isInRootPackage && !normalizedFilePath.endsWith(expectedPath)) {
report(
CodeSmell(
issue,
Entity.from(directive),
"The package declaration does not match the actual file location.",
)
directive.reportInvalidPackageDeclaration(
"The package declaration does not match the actual file location."
)
}
}
}

private fun KtElement.reportInvalidPackageDeclaration(message: String) {
report(CodeSmell(issue, Entity.from(this), message))
}

private fun <T> Iterable<T>.toNormalizedForm() = joinToString("|")

private fun packageNameToNormalizedForm(packageName: String) = packageName.split('.').toNormalizedForm()
Expand Down
Expand Up @@ -93,6 +93,20 @@ internal class InvalidPackageDeclarationSpec : Spek({

assertThat(findings).hasSize(1)
}

it("should report if root package is missing") {
val source = """
package foo.bar

class C
"""

val ktFile = compileContentForTest(source, createPath("src/foo/bar/File.kt"))
mateot1 marked this conversation as resolved.
Show resolved Hide resolved
val findings = InvalidPackageDeclaration(config).lint(ktFile)

assertThat(findings).hasSize(1)
}

it("should report if file path matches root package but package declaration differs") {
val source = """
package io.foo.bar
Expand Down