From f5df506a9c941f3dad5723a406a213b33033e013 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 30 Jul 2021 16:39:30 +0300 Subject: [PATCH 1/6] Package name path bug with dot ### What's done: Fixed bug Added test --- .../diktat/ruleset/rules/chapter1/PackageNaming.kt | 7 ++++++- .../kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt | 2 +- .../diktat/ruleset/chapter1/PackageNamingWarnTest.kt | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt index ca0d755ff7..d5e82db089 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt @@ -125,7 +125,7 @@ class PackageNaming(configRules: List) : DiktatRule( // 2) removing src/main/kotlin/java/e.t.c dirs and removing file name // 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 - FILE_NAME_AND_TYPE_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() @@ -283,6 +283,11 @@ class PackageNaming(configRules: List) : DiktatRule( */ const val PACKAGE_SEPARATOR = "." + /** + * Count element to drop when splitting file path + */ + const val FILE_NAME_AND_TYPE_SIZE = 2 + /** * tricky hack (counter) that helps not to raise multiple warnings about the package name if config is missing */ diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt index 29343a7f2d..4c6909a988 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt @@ -14,7 +14,7 @@ internal const val SRC_DIRECTORY_NAME = "src" fun String.splitPathToDirs(): List = this.replace("\\", "/") .replace("//", "/") - .split("/") + .split("\\.|\\/".toRegex()) /** * Checks if [this] String is a name of a kotlin script file by checking whether file extension equals 'kts' diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt index 30cc776207..5b497661f9 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt @@ -369,4 +369,15 @@ 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", + ) + } } From d846aa08392cac066e3a552b3bdd12eeff0be143 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 30 Jul 2021 17:08:14 +0300 Subject: [PATCH 2/6] Package name path bug with dot ### What's done: Fixed to build --- .../cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt | 7 +------ .../main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt | 1 + 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt index d5e82db089..ca0d755ff7 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt @@ -125,7 +125,7 @@ class PackageNaming(configRules: List) : DiktatRule( // 2) removing src/main/kotlin/java/e.t.c dirs and removing file name // 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 - FILE_NAME_AND_TYPE_SIZE) + val fileSubDir = filePathParts.subList(filePathParts.lastIndexOf(PACKAGE_PATH_ANCHOR), filePathParts.size - 1) .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() @@ -283,11 +283,6 @@ class PackageNaming(configRules: List) : DiktatRule( */ const val PACKAGE_SEPARATOR = "." - /** - * Count element to drop when splitting file path - */ - const val FILE_NAME_AND_TYPE_SIZE = 2 - /** * tricky hack (counter) that helps not to raise multiple warnings about the package name if config is missing */ diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt index 4c6909a988..4afc7d5270 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt @@ -15,6 +15,7 @@ fun String.splitPathToDirs(): List = this.replace("\\", "/") .replace("//", "/") .split("\\.|\\/".toRegex()) + .dropLast(1) /** * Checks if [this] String is a name of a kotlin script file by checking whether file extension equals 'kts' From 9087e61cb31e6d9f95e5f2b53ec99ec988beda59 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 30 Jul 2021 17:24:14 +0300 Subject: [PATCH 3/6] Package name path bug with dot ### What's done: Updated kdoc --- .../src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt index 4afc7d5270..5389316c4b 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt @@ -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. Drop last element - file type * * @return list of path parts */ From 5787ff963081ca25aceeefcccdb21145f811a44e Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 30 Jul 2021 17:24:44 +0300 Subject: [PATCH 4/6] Package name path bug with dot ### What's done: Simplified code --- .../src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt index 5389316c4b..9991103f7f 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt @@ -14,7 +14,7 @@ internal const val SRC_DIRECTORY_NAME = "src" fun String.splitPathToDirs(): List = this.replace("\\", "/") .replace("//", "/") - .split("\\.|\\/".toRegex()) + .split("[./]".toRegex()) .dropLast(1) /** From 83fadd55f1973293a9ff993e896c0836f2079e5d Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 2 Aug 2021 12:07:12 +0300 Subject: [PATCH 5/6] Package name path bug with dot ### What's done: Fixed after review --- .../diktat/ruleset/rules/chapter1/PackageNaming.kt | 9 ++++++--- .../kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt | 5 ++--- .../diktat/ruleset/chapter1/PackageNamingWarnTest.kt | 11 +++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt index ca0d755ff7..2539060825 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt @@ -113,7 +113,10 @@ class PackageNaming(configRules: List) : DiktatRule( * @return list with words that are parts of package name like [org, diktat, name] */ private fun calculateRealPackageName(fileName: String, configuration: CommonConfiguration): List { - 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." + @@ -122,10 +125,10 @@ class PackageNaming(configRules: List) : 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() diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt index 9991103f7f..78fc5dc649 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FileUtils.kt @@ -7,15 +7,14 @@ package org.cqfn.diktat.ruleset.utils internal const val SRC_DIRECTORY_NAME = "src" /** - * Splits [this] string by file path separator. Drop last element - file type + * Splits [this] string by file path separator. * * @return list of path parts */ fun String.splitPathToDirs(): List = this.replace("\\", "/") .replace("//", "/") - .split("[./]".toRegex()) - .dropLast(1) + .split("/") /** * Checks if [this] String is a name of a kotlin script file by checking whether file extension equals 'kts' diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt index 5b497661f9..bc9c7d21a0 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter1/PackageNamingWarnTest.kt @@ -380,4 +380,15 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) { fileName = "/home/testu/project/src/main/kotlin/org/cqfn/diktat/test.utils/Example.kt", ) } + + @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", + ) + } } From 13baf27d710e2e37852094335f0cdb74200d2625 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 2 Aug 2021 12:15:05 +0300 Subject: [PATCH 6/6] Package name path bug with dot ### What's done: Fixed according to our code style --- .../org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt index 2539060825..c3caae1879 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter1/PackageNaming.kt @@ -115,7 +115,7 @@ class PackageNaming(configRules: List) : DiktatRule( private fun calculateRealPackageName(fileName: String, configuration: CommonConfiguration): List { val filePathParts = fileName .splitPathToDirs() - .dropLast(1) // remove filename + .dropLast(1) // remove filename .flatMap { it.split(".") } return if (!filePathParts.contains(PACKAGE_PATH_ANCHOR)) {