Skip to content

Commit

Permalink
ignoreLines feature for filtering the input file (#307)
Browse files Browse the repository at this point in the history
* feature/ignoreLines

###What's done:
 * Added ignoreLines option for Fix plugin.
 * Added tests for ignoreLines and Fix plugin.
 * Added ignoreLines option for Warn plugin.
 * Added tests for ignoreLines and Warn plugin.
 * Implemented copying a file in Warn plugin in order to filter out lines that match any regex from ignoreLines.

(#303)
  • Loading branch information
sanyavertolet authored Nov 24, 2021
1 parent 0388458 commit 530f7e8
Show file tree
Hide file tree
Showing 27 changed files with 319 additions and 65 deletions.
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 @@ -75,7 +75,7 @@ class GeneralTest {
val saveFlags = " . --result-output FILE --report-type JSON"
// Execute the script from examples
val execCmd = "$runCmd$saveBinName $saveFlags"
val pb = ProcessBuilder(true, fs).exec(execCmd, examplesDir, null, 100_000L)
val pb = ProcessBuilder(true, fs).exec(execCmd, examplesDir, null, 200_000L)
println("SAVE execution output:\n${pb.stdout.joinToString("\n")}")
if (pb.stderr.isNotEmpty()) {
println("Warning and errors during SAVE execution:\n${pb.stderr.joinToString("\n")}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class Plugin(
if (!excludedTests.isNullOrEmpty()) {
logDebug("Excluded tests for [${testConfig.location}] : $excludedTests")
}

clean()
return if (testFilesList.isNotEmpty()) {
// fixme: remove this logging and convert `testFilesList` back to Sequence
// or at least make `logDebug` accept lazy messages
Expand Down
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 @@ -118,7 +118,7 @@ class Save(
}
reporter.afterAll()
reporter.out.close()
logInfo("SAVE has finished execution. You can rerun with --log debug or --log trace for additional information.")
logInfo("SAVE has finished execution. You can rerun with --log debug or --log all for additional information.")

return reporter
}
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 @@ -47,7 +47,7 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/chapter1"
), 7
), 8
)
}

Expand All @@ -60,6 +60,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 @@ -86,7 +95,7 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/save.toml"
), 9
), 10
)
}

Expand All @@ -95,7 +104,7 @@ class ClassicWarnTest {
runTestsWithDiktat(
listOf(
"warn/chapter1/save.toml"
), 7
), 8
)
}

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
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

0 comments on commit 530f7e8

Please sign in to comment.