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,12 @@
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
# I WILL DISSAPEAR AS WELL
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.*", "^#.*"]
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]
1 change: 0 additions & 1 deletion examples/kotlin-diktat/save.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
expectedWarningsPattern = "// ;warn:?(.*):(\\d*): (.+)"
timeOutMillis = 30000


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
*/
val ignoreLinesPatterns: MutableList<Regex>

/**
* Location of the toml config
*/
Expand Down Expand Up @@ -76,6 +81,7 @@ data class GeneralConfig(
val timeOutMillis: Long? = null,
) : PluginConfig {
override val type = TestConfigSections.GENERAL
override val ignoreLinesPatterns: MutableList<Regex> = mutableListOf()

@Transient
override var configLocation: Path = "undefined_toml_location".toPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassicFixTest {
runTestsWithDiktat(
listOf(
"fix/save.toml"
), 2
), 5
)
}

Expand All @@ -28,7 +28,43 @@ class ClassicFixTest {
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 @@ -46,7 +46,7 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/chapter1"
), 6
), 7
)
}

Expand All @@ -59,6 +59,15 @@ class ClassicWarnTest {
)
}

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

@Test
fun `test output file set`() {
runTestsWithDiktat(
Expand All @@ -85,7 +94,7 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/save.toml"
), 8
), 9
)
}

Expand All @@ -94,16 +103,7 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/chapter1/save.toml"
), 6
)
}

@Test
fun `executing warn plugin so that no resources will be found`() {
runTestsWithDiktat(
listOf(
"warn/chapter1/TestsWithRegex/NoMatchingTests/",
), 0
), 7
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.Test
class FixDirTest {
@Test
fun `execute fix plugin`() {
runTestsWithDiktat(listOf("fix/smoke/src/main/kotlin/org/cqfn/save"), 2)
runTestsWithDiktat(listOf("fix/smoke/src/main/kotlin/org/cqfn/save"), 5)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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 @@ -26,14 +26,19 @@ data class FixAndWarnPluginConfig(
override var configLocation: Path = "undefined_toml_location".toPath()
override val resourceNamePatternStr: String = "(${fix.resourceNamePatternStr})|(${warn.resourceNamePatternStr})"

@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 val ignoreLinesPatterns: MutableList<Regex> = mutableListOf()

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 @@ -49,6 +54,8 @@ data class FixAndWarnPluginConfig(
return FixAndWarnPluginConfig(
fix.validateAndSetDefaults(),
warn.validateAndSetDefaults()
).also { it.configLocation = this.configLocation }
).also {
it.configLocation = this.configLocation
}
}
}
Loading