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

Package name bug#1015 #1016

Merged
merged 7 commits into from
Aug 2, 2021
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 @@ -113,7 +113,10 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
* @return list with words that are parts of package name like [org, diktat, name]
*/
private fun calculateRealPackageName(fileName: String, configuration: CommonConfiguration): List<String> {
val filePathParts = fileName.splitPathToDirs()
val filePathParts = fileName
.splitPathToDirs()
.dropLast(1) // remove filename
.flatMap { it.split(".") }

return if (!filePathParts.contains(PACKAGE_PATH_ANCHOR)) {
log.error("Not able to determine a path to a scanned file or \"$PACKAGE_PATH_ANCHOR\" directory cannot be found in it's path." +
Expand All @@ -122,10 +125,10 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
} else {
// creating a real package name:
// 1) getting a path after the base project directory (after "src" directory)
// 2) removing src/main/kotlin/java/e.t.c dirs and removing file name
// 2) removing src/main/kotlin/java/e.t.c dirs
// 3) adding company's domain name at the beginning
val allDirs = languageDirNames + configuration.srcDirectories + configuration.testAnchors
val fileSubDir = filePathParts.subList(filePathParts.lastIndexOf(PACKAGE_PATH_ANCHOR), filePathParts.size - 1)
val fileSubDir = filePathParts.subList(filePathParts.lastIndexOf(PACKAGE_PATH_ANCHOR), filePathParts.size)
.dropWhile { allDirs.contains(it) }
// no need to add DOMAIN_NAME to the package name if it is already in path
val domainPrefix = if (!fileSubDir.joinToString(PACKAGE_SEPARATOR).startsWith(domainName)) domainName.split(PACKAGE_SEPARATOR) else emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package org.cqfn.diktat.ruleset.utils
internal const val SRC_DIRECTORY_NAME = "src"

/**
* Splits [this] string by file path separator
* Splits [this] string by file path separator.
*
* @return list of path parts
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,26 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) {
rulesConfigList = rulesConfigListEmptyDomainName
)
}

@Test
@Tag(WarningNames.PACKAGE_NAME_INCORRECT_PATH)
fun `shouldn't trigger if path contains dot`() {
lintMethod(
"""
|package org.cqfn.diktat.test.utils
""".trimMargin(),
fileName = "/home/testu/project/src/main/kotlin/org/cqfn/diktat/test.utils/Example.kt",
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
)
}

@Test
@Tag(WarningNames.PACKAGE_NAME_INCORRECT_PATH)
fun `shouldn't trigger for gradle script`() {
lintMethod(
"""
|import org.cqfn.diktat.generation.docs.generateAvailableRules
""".trimMargin(),
fileName = "/home/testu/project/build.gradle.kts",
)
}
}