Skip to content

Commit 228e224

Browse files
authored
Minor changes in examples and PlainTextReporter (#151)
### What's done: * Run save with default value for properties file in run.sh * Introduced short reason for Fail * Moved logic on test resources emptiness check into base Plugin class * Collapse comment to one line in PlainTextReporter * Improvements for kotlin-diktat example case * Logging cleanup
1 parent 5850da5 commit 228e224

File tree

15 files changed

+100
-119
lines changed

15 files changed

+100
-119
lines changed

examples/kotlin-diktat/fix/smoke/Example1Expected.kt

-44
This file was deleted.

examples/kotlin-diktat/fix/smoke/Example1Test.kt

-31
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.cqfn.diktat
2+
3+
class Example {
4+
@get:JvmName("getIsValid")
5+
val isValid = true
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package test.smoke
2+
3+
class example {
4+
@get : JvmName("getIsValid")
5+
val isValid = true
6+
}

examples/kotlin-diktat/run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ if [ "$1" == "--help" ]; then
2222
For example, `./run.sh warn/chapter1/save.toml` to execute tests located in warn/chapter1
2323
`./run.sh EnumValueSnakeCaseTest.kt to execute a single test`'
2424
else
25-
./save --debug -prop save.properties $1
25+
./save $@
2626
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ;warn:1:9 [PACKAGE_NAME_INCORRECT_PREFIX] package name should start from company's domain: org.cqfn.save
2+
// ;warn:1:1 [FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: GenericFunctionTest.kt vs ClassName
3+
package org.cqfn.diktat.test.paragraph1.naming.generic
4+
5+
private class ClassName<T> {
6+
private fun <Template, T> lock(body: ((Template?) -> T?)?, value: Template?): T? {
7+
try {
8+
// ;warn:6:13 [LOCAL_VARIABLE_EARLY_DECLARATION] local variables should be declared close to the line where they are first used: <variableName> is declared on line <6> and is used for the first time on line <9> (cannot be auto-corrected)
9+
// ;warn:6:43 [NULLABLE_PROPERTY_TYPE] try to avoid use of nullable types: initialize explicitly (cannot be auto-corrected)
10+
val variableName: Template? = null
11+
// ;warn:7:17 [VARIABLE_NAME_INCORRECT_FORMAT] variable name should be in lowerCamelCase and should contain only latin (ASCII) letters or numbers and should start from lower letter: variableT
12+
// ;warn:7:33 [NULLABLE_PROPERTY_TYPE] try to avoid use of nullable types: initialize explicitly (cannot be auto-corrected)
13+
val variableT: T? = null
14+
println(variableT)
15+
return body!!(variableName)
16+
} finally {
17+
println()
18+
}
19+
}
20+
21+
// ;warn:15:5 [MISSING_KDOC_ON_FUNCTION] all public, internal and protected functions should have Kdoc with proper tags: foo
22+
fun foo(var1: T, var2: ((T?) -> T?)?) {
23+
lock<T, T>(var2, var1)
24+
}
25+
}

examples/kotlin-diktat/warn/save.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ fileNameCaptureGroupOut = 1
1414
lineCaptureGroupOut = 2
1515
columnCaptureGroupOut = 3
1616
messageCaptureGroupOut = 4
17-
exactWarningsMatch = true
17+
exactWarningsMatch = false
1818
warningTextHasColumn = true
1919
warningTextHasLine = true

save-cli/src/nativeMain/kotlin/org/cqfn/save/cli/SaveConfigReader.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import org.cqfn.save.cli.logging.logErrorAndExit
88
import org.cqfn.save.core.config.SaveProperties
99
import org.cqfn.save.core.config.defaultConfig
1010
import org.cqfn.save.core.logging.logDebug
11-
import org.cqfn.save.core.logging.logInfo
1211

1312
import okio.FileNotFoundException
1413
import okio.FileSystem
@@ -58,7 +57,7 @@ fun createConfigFromArgs(args: Array<String>): SaveProperties {
5857
val configFromPropertiesFile = readPropertiesFile(configFromCli.propertiesFile ?: defaultConfig().propertiesFile)
5958
// merging two configurations into single [SaveProperties] class with a priority to command line arguments
6059
val mergedProperties = configFromCli.mergeConfigWithPriorityToThis(configFromPropertiesFile)
61-
logInfo("Using the following properties for SAVE execution:\n${mergedProperties.getFields()}")
60+
logDebug("Using the following properties for SAVE execution:\n${mergedProperties.getFields()}")
6261
return mergedProperties.validate()
6362
}
6463

save-common/src/commonMain/kotlin/org/cqfn/save/core/plugin/Plugin.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.cqfn.save.core.plugin
33
import org.cqfn.save.core.config.TestConfig
44
import org.cqfn.save.core.config.isSaveTomlConfig
55
import org.cqfn.save.core.files.findDescendantDirectoriesBy
6+
import org.cqfn.save.core.logging.logDebug
67
import org.cqfn.save.core.result.TestResult
78
import org.cqfn.save.core.utils.ProcessBuilder
89

@@ -32,10 +33,14 @@ abstract class Plugin(
3233
*/
3334
fun execute(): Sequence<TestResult> {
3435
clean()
35-
return handleFiles(
36-
// todo: pass individual groups of files to handleFiles? Or it will play bad with batch mode?
37-
discoverTestFiles(testConfig.directory)
38-
)
36+
// todo: pass individual groups of files to handleFiles? Or it will play bad with batch mode?
37+
val testFilesSequence = discoverTestFiles(testConfig.directory)
38+
return if (testFilesSequence.any()) {
39+
logDebug("Discovered the following test resources: ${testFilesSequence.toList()}")
40+
handleFiles(testFilesSequence)
41+
} else {
42+
emptySequence()
43+
}
3944
}
4045

4146
/**

save-common/src/commonMain/kotlin/org/cqfn/save/core/reporter/Reporter.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ interface Reporter {
2424
/**
2525
* This function is called before any tests are executed
2626
*/
27-
fun beforeAll() {
28-
out.write("Initializing reporter ${this::class.qualifiedName} of type $type\n".encodeToByteArray())
29-
}
27+
fun beforeAll()
3028

3129
/**
3230
* This function is called after all tests are executed

save-common/src/commonMain/kotlin/org/cqfn/save/core/result/TestStatus.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ data class Pass(val message: String?) : TestStatus()
1616

1717
/**
1818
* @property reason reason of failure
19+
* @property shortReason
1920
*/
20-
data class Fail(val reason: String) : TestStatus()
21+
data class Fail(val reason: String, val shortReason: String) : TestStatus()
2122

2223
/**
2324
* @property reason reason of test ignoring

save-core/src/commonMain/kotlin/org/cqfn/save/core/Save.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ class Save(
7878

7979
private fun executePlugin(plugin: Plugin, reporter: Reporter) {
8080
reporter.onPluginInitialization(plugin)
81-
logInfo("=> Executing plugin: ${plugin::class.simpleName} for [${plugin.testConfig.location}]")
81+
logDebug("=> Executing plugin: ${plugin::class.simpleName} for [${plugin.testConfig.location}]")
8282
reporter.onPluginExecutionStart(plugin)
8383
try {
8484
val events = plugin.execute().toList()
8585
if (events.isEmpty()) {
86-
logInfo("No resources discovered for ${plugin::class.simpleName} in [${plugin.testConfig.location}], skipping")
86+
logDebug("No resources discovered for ${plugin::class.simpleName} in [${plugin.testConfig.location}], skipping")
8787
reporter.onPluginExecutionSkip(plugin)
8888
}
8989
events
@@ -93,7 +93,7 @@ class Save(
9393
reporter.onPluginExecutionError(ex)
9494
logError("${plugin::class.simpleName} has crashed: ${ex.message}")
9595
}
96-
logInfo("<= Finished execution of: ${plugin::class.simpleName} for [${plugin.testConfig.location}]")
96+
logDebug("<= Finished execution of: ${plugin::class.simpleName} for [${plugin.testConfig.location}]")
9797
reporter.onPluginExecutionEnd(plugin)
9898
}
9999

save-plugins/fix-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fix/FixPlugin.kt

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.cqfn.save.core.config.TestConfig
44
import org.cqfn.save.core.files.createFile
55
import org.cqfn.save.core.files.readFile
66
import org.cqfn.save.core.files.readLines
7-
import org.cqfn.save.core.logging.logInfo
87
import org.cqfn.save.core.plugin.GeneralConfig
98
import org.cqfn.save.core.plugin.Plugin
109
import org.cqfn.save.core.result.DebugInfo
@@ -15,6 +14,8 @@ import org.cqfn.save.core.utils.ProcessExecutionException
1514

1615
import io.github.petertrr.diffutils.diff
1716
import io.github.petertrr.diffutils.patch.ChangeDelta
17+
import io.github.petertrr.diffutils.patch.Delta
18+
import io.github.petertrr.diffutils.patch.DeltaType
1819
import io.github.petertrr.diffutils.patch.Patch
1920
import io.github.petertrr.diffutils.text.DiffRowGenerator
2021
import okio.FileSystem
@@ -43,12 +44,6 @@ class FixPlugin(
4344

4445
@Suppress("TOO_LONG_FUNCTION")
4546
override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> {
46-
val flattenedResources = files.toList()
47-
if (flattenedResources.isEmpty()) {
48-
return emptySequence()
49-
}
50-
logInfo("Discovered the following file pairs for comparison: $flattenedResources")
51-
5247
testConfig.validateAndSetDefaults()
5348

5449
val fixPluginConfig = testConfig.pluginConfigs.filterIsInstance<FixPluginConfig>().single()
@@ -63,7 +58,7 @@ class FixPlugin(
6358
} catch (ex: ProcessExecutionException) {
6459
return@map TestResult(
6560
listOf(expected, test),
66-
Fail(ex.message!!),
61+
Fail("${ex::class.simpleName}: ${ex.message}", ex::class.simpleName ?: "Unknown exception"),
6762
DebugInfo(null, ex.message, null)
6863
)
6964
}
@@ -76,7 +71,7 @@ class FixPlugin(
7671
if (patch.deltas.isEmpty()) {
7772
Pass(null)
7873
} else {
79-
Fail(patch.formatToString())
74+
Fail(patch.formatToString(), patch.formatToShortString())
8075
}
8176
}
8277
TestResult(
@@ -143,4 +138,13 @@ class FixPlugin(
143138
else -> delta.toString()
144139
}
145140
}
141+
142+
private fun Patch<String>.formatToShortString(): String = deltas.groupingBy {
143+
it.type
144+
}
145+
.aggregate<Delta<String>, DeltaType, Int> { _, acc, delta, _ ->
146+
(acc ?: 0) + delta.source.lines.size
147+
}
148+
.toList()
149+
.joinToString { (type, lines) -> "$type: $lines lines" }
146150
}

save-plugins/warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugin/warn/WarnPlugin.kt

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.cqfn.save.plugin.warn
33
import org.cqfn.save.core.config.TestConfig
44
import org.cqfn.save.core.files.createFile
55
import org.cqfn.save.core.files.readLines
6-
import org.cqfn.save.core.logging.logInfo
76
import org.cqfn.save.core.plugin.GeneralConfig
87
import org.cqfn.save.core.plugin.Plugin
98
import org.cqfn.save.core.result.DebugInfo
@@ -33,14 +32,10 @@ class WarnPlugin(
3332
testFiles,
3433
useInternalRedirections) {
3534
private val fs = FileSystem.SYSTEM
35+
private val expectedAndNotReceived = "Some warnings were expected but not received"
36+
private val unexpected = "Some warnings were unexpected"
3637

3738
override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> {
38-
val flattenedResources = files.toList().flatten()
39-
if (flattenedResources.isEmpty()) {
40-
return emptySequence()
41-
}
42-
logInfo("Discovered the following test resources: $flattenedResources")
43-
4439
testConfig.validateAndSetDefaults()
4540

4641
val warnPluginConfig = testConfig.pluginConfigs.filterIsInstance<WarnPluginConfig>().single()
@@ -112,7 +107,7 @@ class WarnPlugin(
112107
} catch (ex: ProcessExecutionException) {
113108
return listOf(TestResult(
114109
paths,
115-
Fail(ex.message!!),
110+
Fail("${ex::class.simpleName}: ${ex.message}", ex::class.simpleName ?: "Unknown exception"),
116111
DebugInfo(null, ex.message, null)
117112
))
118113
}
@@ -182,18 +177,22 @@ class WarnPlugin(
182177
val unexpectedWarnings = actualWarningsMap.valuesNotIn(expectedWarningsMap)
183178

184179
return when (missingWarnings.isEmpty() to unexpectedWarnings.isEmpty()) {
185-
false to true -> Fail("Some warnings were expected but not received: $missingWarnings")
186-
false to false -> Fail("Some warnings were expected but not received: $missingWarnings, " +
187-
"and others were unexpected: $unexpectedWarnings")
180+
false to true -> createFail(expectedAndNotReceived, missingWarnings)
181+
false to false -> Fail(
182+
"$expectedAndNotReceived: $missingWarnings, and ${unexpected.lowercase()}: $unexpectedWarnings",
183+
"$expectedAndNotReceived (${missingWarnings.size}), and ${unexpected.lowercase()} (${unexpectedWarnings.size})"
184+
)
188185
true to false -> if (warnPluginConfig.exactWarningsMatch == false) {
189-
Pass("Some warnings were unexpected: $unexpectedWarnings")
186+
Pass("$unexpected: $unexpectedWarnings")
190187
} else {
191-
Fail("Some warnings were unexpected: $unexpectedWarnings")
188+
createFail(unexpected, unexpectedWarnings)
192189
}
193190
true to true -> Pass(null)
194-
else -> Fail("")
191+
else -> Fail("", "")
195192
}
196193
}
194+
195+
private fun createFail(baseText: String, warnings: List<Warning>) = Fail("$baseText: $warnings", "$baseText (${warnings.size})")
197196
}
198197

199198
/**

0 commit comments

Comments
 (0)