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

ignoreLines feature for filtering the input file #307

Merged
merged 19 commits into from
Nov 24, 2021
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cqfn.save.IgnoreLinesTest.IgnoreLines

class D {
val x = 0

/**
* @return
*/
fun bar(): Bar {
val qux = 42
return Bar(qux)
}
}

/**
* @param foo
*/
fun readFile(foo: Foo) {
var bar: Bar
}
// ;warn:0: [TEST] JUST_A_TEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.smoke.src.main.kotlin

fun readFile(foo: Foo) {
var bar: Bar
}

class D {val x = 0
fun bar(): Bar {val qux = 42; return Bar(qux)}
}
// ;warn:0: [TEST] JUST_A_TEST
// IGNORE_ME
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
tags = ["FixIgnoreLines"]
description = "Test for ignoreLines option in Fix"
suiteName = "Autofix: Smoke Tests"

[fix]
ignoreLines = ["// IGNORE_ME"]
Copy link
Member

Choose a reason for hiding this comment

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

may be test with regex here?

Copy link
Member

Choose a reason for hiding this comment

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

and better to test with several different regexes

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.cqfn.save.IgnoreLinesTest.IgnoreLinesIsEmpty

class D {
val x = 0

/**
* @return
*/
fun bar(): Bar {
val qux = 42
return Bar(qux)
}
}

/**
* @param foo
*/
fun readFile(foo: Foo) {
var bar: Bar
}
// ;warn:0: [TEST] JUST_A_TEST
// IGNORE_ME
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.smoke.src.main.kotlin

fun readFile(foo: Foo) {
var bar: Bar
}

class D {val x = 0
fun bar(): Bar {val qux = 42; return Bar(qux)}
}
// ;warn:0: [TEST] JUST_A_TEST
// IGNORE_ME
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
tags = ["smoke"]
description = "Smoke tests for diktat"
suiteName = "Autofix: Smoke Tests"

[fix]
ignoreLines = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cqfn.save.IgnoreLinesTest.NoIgnoreLines

class D {
val x = 0

/**
* @return
*/
fun bar(): Bar {
val qux = 42
return Bar(qux)
}
}

/**
* @param foo
*/
fun readFile(foo: Foo) {
var bar: Bar
}
// IGNORE_ME
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.smoke.src.main.kotlin

fun readFile(foo: Foo) {
var bar: Bar
}

class D {val x = 0
fun bar(): Bar {val qux = 42; return Bar(qux)}
}
// ;warn:0: [TEST] JUST_A_TEST
// IGNORE_ME
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[general]
tags = ["smoke"]
description = "Smoke tests for diktat"
suiteName = "Autofix: Smoke Tests"

[fix]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[general]
tags = ["smoke"]
description = "Smoke tests for diktat"
suiteName = "Autofix: Smoke Tests"

[fix]
4 changes: 1 addition & 3 deletions examples/kotlin-diktat/save.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
execCmd = "java -jar ktlint -R diktat.jar"
description = "Test for diktat - linter and formatter for Kotlin"
# this is the default value, you don't need to add it explicitly, but can be useful, if you have different pattern:
expectedWarningsPattern = "// ;warn:?(.*):(\\d*): (.+)"


expectedWarningsPattern = "// ;warn:?(.*):(\\d*): (.+)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cqfn.diktat.test.resources.test.paragraph1.naming.enum_

// ;warn:3:1: [MISSING_KDOC_TOP_LEVEL] all public and internal top-level classes and functions should have Kdoc: EnumTestDetection (cannot be auto-corrected)
// ;warn:30: [WRONG_DECLARATIONS_ORDER] declarations of constants and enum members should be sorted alphabetically: enum entries order is incorrect
// ;warn:10:5: [ENUMS_SEPARATED] enum is incorrectly formatted: enums must end with semicolon
enum class EnumTestDetection {
// ;warn:$line+1:5: [ENUM_VALUE] enum values should be{{ in }}selected UPPER_CASE snake/PascalCase format: paSC_SAl_l
paSC_SAl_l,

// ;warn:5: [ENUM_VALUE] enum values{{ should }}be in selected{{ UPPER_CASE }}snake/PascalCase format: PascAsl_f
PascAsl_f
// ;warn:$line-2:5: [ENUMS_SEPARATED] enum is incorrectly formatted: last enum entry must end with a comma

// ;warn:1:9: {{.*}}[PACKAGE_NAME_INCORRECT_PREFIX] package name should start from company's domain: org.cqfn.save{{.*}}
}
// ;warn:0:0: [YOU SHOULD NOT SEE THIS] this warning should not be shown
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
tags = ["WarnIgnoreLines"]
description = "Test suite that checks if filtering by regex work."

[warn]
testNameRegex = "^E.*T.*D.*"
ignoreLines = [".*// ;warn:0:0: \\[YOU SHOULD NOT SEE THIS\\] this warning should not be shown.*"]
2 changes: 1 addition & 1 deletion examples/kotlin-diktat/warn/save.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
messageCaptureGroupOut = 4
exactWarningsMatch = false
warningTextHasColumn = true
warningTextHasLine = true
warningTextHasLine = true
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ interface PluginConfig {
*/
val type: TestConfigSections

/**
* list of regexes to be ignored
*/
var ignoreLinesPatterns: MutableList<Regex>
Copy link
Member

@orchestr7 orchestr7 Nov 6, 2021

Choose a reason for hiding this comment

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

why var should be here? Because of the hierarchical inheritance of configs?

Copy link
Member Author

Choose a reason for hiding this comment

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

val should be here


/**
* Location of the toml config
*/
Expand Down Expand Up @@ -69,6 +74,7 @@ data class GeneralConfig(
val runConfigPattern: Regex? = null,
) : PluginConfig {
override val type = TestConfigSections.GENERAL
override var ignoreLinesPatterns: MutableList<Regex> = mutableListOf()
Copy link
Member

Choose a reason for hiding this comment

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

why var here?

Copy link
Member Author

Choose a reason for hiding this comment

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

val should be here


@Transient
override var configLocation: Path = "undefined_toml_location".toPath()
Expand All @@ -88,8 +94,10 @@ data class GeneralConfig(
this.suiteName ?: other.suiteName,
this.excludedTests ?: other.excludedTests,
this.expectedWarningsPattern ?: other.expectedWarningsPattern,
this.runConfigPattern ?: other.runConfigPattern,
).also { it.configLocation = this.configLocation }
this.runConfigPattern ?: other.runConfigPattern
).also {
it.configLocation = this.configLocation
}
}

override fun validateAndSetDefaults(): GeneralConfig {
Expand All @@ -113,7 +121,9 @@ data class GeneralConfig(
excludedTests ?: emptyList(),
expectedWarningsPattern ?: defaultExpectedWarningPattern,
runConfigPattern ?: defaultRunConfigPattern,
).also { it.configLocation = this.configLocation }
).also {
it.configLocation = this.configLocation
}
}

private fun errorMsgForRequireCheck(field: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,46 @@ class ClassicFixTest {
runTestsWithDiktat(
listOf(
"fix/save.toml"
), 2
), 5
)

@Test
fun `executing fix plugin on parental save-toml file`() =
runTestsWithDiktat(
listOf(
"fix/smoke/save.toml"
), 2
), 5
)

@Test
fun `execute fix plugin on folder`() =
runTestsWithDiktat(
listOf(
"fix/smoke/src/main/kotlin/org/cqfn/save/"
), 5
)

@Test
fun `check NoIgnoreLines`() =
runTestsWithDiktat(
listOf(
"fix/smoke/src/main/kotlin/org/cqfn/save/IgnoreLinesTest/NoIgnoreLines"
), 1
)

@Test
fun `check IgnoreLinesIsEmpty`() =
runTestsWithDiktat(
listOf(
"fix/smoke/src/main/kotlin/org/cqfn/save/IgnoreLinesTest/IgnoreLinesIsEmpty"
), 1
)

@Test
fun `check IgnoreLines`() =
runTestsWithDiktat(
listOf(
"fix/smoke/src/main/kotlin/org/cqfn/save/IgnoreLinesTest/IgnoreLines"
), 1
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/chapter1"
), 6
), 7
)

@Test
Expand All @@ -49,6 +49,14 @@ class ClassicWarnTest {
), 1
)

@Test
fun `lines that match ignoreLines should be ignored`() =
runTestsWithDiktat(
listOf(
"warn/chapter1/IgnoreLinesTest"
), 1
)

@Test
fun `test output file set`() =
runTestsWithDiktat(
Expand All @@ -73,15 +81,15 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/save.toml"
), 6
), 7
)

@Test
fun `executing warn plugin on parental save-toml file`() =
runTestsWithDiktat(
listOf(
"warn/chapter1/save.toml"
), 6
), 7
)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ class FixAndWarnPlugin(
files.forEach { file ->
val fileData = fs.readLines(file)
filesAndTheirWarningsMap[file] = mutableListOf()

val fileDataWithoutWarnings = fileData.filterIndexed { index, line ->
val isLineWithWarning = (generalConfig.expectedWarningsPattern!!.find(line)?.groups != null)
if (isLineWithWarning) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ data class FixAndWarnPluginConfig(
@Transient
override var configLocation: Path = "undefined_toml_location".toPath()

@Transient
Copy link
Member

Choose a reason for hiding this comment

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

why @Transient? I though that we deserialize this value from toml config

Copy link
Member Author

Choose a reason for hiding this comment

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

The point it that we deserialize ignoreLines from toml config. ignoreLinesPatterns is initialized with ignoreLines that we parse from config so it is not deserialized directly from toml.

override var ignoreLinesPatterns: MutableList<Regex> = defaultIgnoreLines

override fun mergeWith(otherConfig: PluginConfig): PluginConfig {
val other = otherConfig as FixAndWarnPluginConfig
val mergedFixPluginConfig = fix.mergeWith(other.fix)
val mergedWarnPluginConfig = warn.mergeWith(other.warn)
return FixAndWarnPluginConfig(
mergedFixPluginConfig as FixPluginConfig,
mergedWarnPluginConfig as WarnPluginConfig
).also { it.configLocation = this.configLocation }
).also {
it.configLocation = this.configLocation
}
}

override fun validateAndSetDefaults(): PluginConfig {
Expand All @@ -48,6 +53,11 @@ data class FixAndWarnPluginConfig(
return FixAndWarnPluginConfig(
fix.validateAndSetDefaults(),
warn.validateAndSetDefaults()
).also { it.configLocation = this.configLocation }
).also {
it.configLocation = this.configLocation
}
}
companion object {
internal val defaultIgnoreLines: MutableList<Regex> = mutableListOf()
Copy link
Member

@orchestr7 orchestr7 Nov 6, 2021

Choose a reason for hiding this comment

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

Do we really need an extra constant for it? May be you will simply set the default value (mutableListOf()) in place, where it is created?

Copy link
Member Author

Choose a reason for hiding this comment

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

To my mind it looked nicer. But ok, it seems that we don't need it here.

}
}
Loading