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

Fix bug in PackageNaming for kotlin scripts #863

Merged
merged 4 commits into from
Apr 29, 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 @@ -48,6 +48,15 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
domainName = it
if (node.elementType == PACKAGE_DIRECTIVE) {
val filePath = node.getRootNode().getFilePath()

// getting all identifiers from existing package name into the list like [org, diktat, project]
val wordsInPackageName = node.findAllDescendantsWithSpecificType(IDENTIFIER)

if (wordsInPackageName.isEmpty() && filePath.isKotlinScript()) {
// kotlin scripts are allowed to have empty package; in this case we don't suggest a new one and don't run checks
return
}

// calculating package name based on the directory where the file is placed
val realPackageName = calculateRealPackageName(filePath, configuration)

Expand All @@ -57,9 +66,6 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
return
}

// getting all identifiers from existing package name into the list like [org, diktat, project]
val wordsInPackageName = node.findAllDescendantsWithSpecificType(IDENTIFIER)

// no need to check that packageIdentifiers is empty, because in this case parsing will fail
checkPackageName(wordsInPackageName, node)
// fix in checkFilePathMatchesWithPackageName is much more aggressive than fixes in checkPackageName, they can conflict
Expand Down Expand Up @@ -126,7 +132,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
}

// package name should start from a company's domain name
if (wordsInPackageName.isNotEmpty() && !isDomainMatches(wordsInPackageName)) {
if (!isDomainMatches(wordsInPackageName)) {
PACKAGE_NAME_INCORRECT_PREFIX.warnAndFix(configRules, emitWarn, isFixMode, domainName,
wordsInPackageName[0].startOffset, wordsInPackageName[0]) {
val oldPackageName = wordsInPackageName.joinToString(PACKAGE_SEPARATOR) { it.text }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

import java.io.File
import java.time.LocalDate

import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.createTempFile
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -204,10 +206,31 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
"extendedIndentOfParameters" to "false",
)
)
) // so that trailing newline isn't checked
) // so that trailing newline isn't checked, because it's incorrectly read in tests and we are comparing file with itself
// file name is `gradle_` so that IDE doesn't suggest to import gradle project
fixAndCompare("../../../build.gradle_.kts", "../../../build.gradle_.kts")
val tmpTestFile = javaClass.classLoader.getResource("$resourceFilePath/../../../build.gradle_.kts")!!.toURI().let {
val tmpTestFile = File(it).parentFile.resolve("build.gradle.kts")
File(it).copyTo(tmpTestFile)
tmpTestFile
}
val tmpFilePath = "../../../build.gradle.kts"
fixAndCompare(tmpFilePath, tmpFilePath)
Assertions.assertTrue(unfixedLintErrors.isEmpty())
tmpTestFile.delete()
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files #2`() {
fixAndCompare("script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
Assertions.assertEquals(3, unfixedLintErrors.size)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files with package name`() {
fixAndCompare("script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts")
Assertions.assertEquals(3, unfixedLintErrors.size)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.cqfn.diktat.script

run {
println("hello world!")
}

fun foo() {
println()
}

val q = Config()

run {
println("a")
}

also {
println("a")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.cqfn.diktat.script

run {
println("hello world!")
}

fun foo() {
println()
}

val q = Config()

run {
println("a")
}

also {
println("a")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed, need to add test for kts with package name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

run {
println("hello world!")
}

fun foo() {
println()
}

val q = Config()

run {
println("a")
}

also {
println("a")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

println("hello world!")

fun foo() {
println()
}

val q = Config()

run {
println("a")
}

also {
println("a")
}