Repository: analysis-dev/diktat (kotlin)

Issue:
package naming: Allow default package in kotlin files in buildSrc
For example, because of old and well-known bug in gradle, a common pattern to use `object Versions {...}` to describe all versions requires `Versions` to be from default package.

Pull request:
What's done:
* Disabled rule for buildSrc directory
Closes #915

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 c6cadf5bc1..08218b2f4d 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
@@ -91,12 +91,18 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
         realPackageName: List<String>,
         filePath: String) {
         val fileName = filePath.substringAfterLast(File.separator)
-        PACKAGE_NAME_MISSING.warnAndFix(configRules, emitWarn, isFixMode, fileName,
-            initialPackageDirectiveNode.startOffset, initialPackageDirectiveNode) {
-            if (realPackageName.isNotEmpty()) {
-                // creating node for package directive using Kotlin parser
-                val newPackageDirectiveName = realPackageName.joinToString(PACKAGE_SEPARATOR)
-                insertNewPackageName(initialPackageDirectiveNode, newPackageDirectiveName)
+
+        // if the file path contains "buildSrc" - don't add the package name to the file
+        val isBuildSrcPath = "buildSrc" in filePath
+
+        if (!isBuildSrcPath) {
+            PACKAGE_NAME_MISSING.warnAndFix(configRules, emitWarn, isFixMode, fileName,
+                initialPackageDirectiveNode.startOffset, initialPackageDirectiveNode) {
+                if (realPackageName.isNotEmpty()) {
+                    // creating node for package directive using Kotlin parser
+                    val newPackageDirectiveName = realPackageName.joinToString(PACKAGE_SEPARATOR)
+                    insertNewPackageName(initialPackageDirectiveNode, newPackageDirectiveName)
+                }
             }
         }
     }


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 674a17102c..30cc776207 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
@@ -71,6 +71,24 @@ class PackageNamingWarnTest : LintTestBase(::PackageNaming) {
         )
     }
 
+    @Test
+    @Tag(WarningNames.PACKAGE_NAME_MISSING)
+    fun `don't add the package name to the file in buildSrc path`() {
+        lintMethod(
+            """
+                import org.cqfn.diktat.a.b.c
+
+                /**
+                 * testComment
+                 */
+                class TestPackageName {  }
+
+            """.trimIndent(),
+            fileName = "~/diktat/buildSrc/src/main/kotlin/Version.kt",
+            rulesConfigList = rulesConfigList
+        )
+    }
+
     @Test
     @Tag(WarningNames.PACKAGE_NAME_INCORRECT_CASE)
     fun `package name should be in a lower case (check)`() {