Repository: analysis-dev/diktat (kotlin)

Issue:
PACKAGE_NAME_INCORRECT_PATH: incorrectly calculates PATH to the directory if the directory is named with dots
```D:\save\save-core\src\nativeTest\kotlin\org\cqfn\save\core\test.utils\TestUtilsNative.kt:7:9: [PACKAGE_NAME_INCORRECT_PATH] package name does not match the directory hierarchy for this file, the real package name should be: org.cqfn.save.core.test.utils```

This happens because of the dot in "test.utils"

Pull request description:
### What's done:
- Fixed bug
- Added test
- Closes #1015

Patch:
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..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
@@ -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." +
@@ -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()
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..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,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
  */


Test patch:
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..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
@@ -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",
+        )
+    }
+
+    @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",
+        )
+    }
 }