Skip to content

Commit 993aa62

Browse files
Test tool result can now be read from file (#298)
### What's done: * save can now read actual results from a file
1 parent 0904be7 commit 993aa62

File tree

8 files changed

+69
-10
lines changed

8 files changed

+69
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.cqfn.diktat.test.resources.test.paragraph1.naming.enum_
2+
3+
// ;warn:3:1: [MISSING_KDOC_TOP_LEVEL] all public and internal top-level classes and functions should have Kdoc: EnumTestDetection (cannot be auto-corrected)
4+
// ;warn:30: [WRONG_DECLARATIONS_ORDER] declarations of constants and enum members should be sorted alphabetically: enum entries order is incorrect
5+
// ;warn:10:5: [ENUMS_SEPARATED] enum is incorrectly formatted: enums must end with semicolon
6+
enum class EnumTestDetection {
7+
// ;warn:$line+1:5: [ENUM_VALUE] enum values should be{{ in }}selected UPPER_CASE snake/PascalCase format: paSC_SAl_l
8+
paSC_SAl_l,
9+
10+
// ;warn:5: [ENUM_VALUE] enum values{{ should }}be in selected{{ UPPER_CASE }}snake/PascalCase format: PascAsl_f
11+
PascAsl_f
12+
// ;warn:$line-2:5: [ENUMS_SEPARATED] enum is incorrectly formatted: last enum entry must end with a comma
13+
14+
// ;warn:1:9: {{.*}}[PACKAGE_NAME_INCORRECT_PREFIX] package name should start from company's domain: org.cqfn.save{{.*}}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
C:\Users\vwx1020336\IdeaProjects\save\examples\kotlin-diktat\warn\chapter1\TestsWithRegex\EnumTestDetection.kt:1:9: [PACKAGE_NAME_INCORRECT_PREFIX] package name should start from company's domain: org.cqfn.save
2+
C:\Users\vwx1020336\IdeaProjects\save\examples\kotlin-diktat\warn\chapter1\TestsWithRegex\EnumTestDetection.kt:3:1: [MISSING_KDOC_TOP_LEVEL] all public and internal top-level classes and functions should have Kdoc: EnumTestDetection (cannot be auto-corrected)
3+
C:\Users\vwx1020336\IdeaProjects\save\examples\kotlin-diktat\warn\chapter1\TestsWithRegex\EnumTestDetection.kt:6:30: [WRONG_DECLARATIONS_ORDER] declarations of constants and enum members should be sorted alphabetically: enum entries order is incorrect
4+
C:\Users\vwx1020336\IdeaProjects\save\examples\kotlin-diktat\warn\chapter1\TestsWithRegex\EnumTestDetection.kt:8:5: [ENUM_VALUE] enum values should be in selected UPPER_CASE snake/PascalCase format: paSC_SAl_l
5+
C:\Users\vwx1020336\IdeaProjects\save\examples\kotlin-diktat\warn\chapter1\TestsWithRegex\EnumTestDetection.kt:10:5: [ENUMS_SEPARATED] enum is incorrectly formatted: enums must end with semicolon
6+
C:\Users\vwx1020336\IdeaProjects\save\examples\kotlin-diktat\warn\chapter1\TestsWithRegex\EnumTestDetection.kt:10:5: [ENUMS_SEPARATED] enum is incorrectly formatted: last enum entry must end with a comma
7+
C:\Users\vwx1020336\IdeaProjects\save\examples\kotlin-diktat\warn\chapter1\TestsWithRegex\EnumTestDetection.kt:11:5: [ENUM_VALUE] enum values should be in selected UPPER_CASE snake/PascalCase format: PascAsl_f
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[general]
2+
tags = ["tag3"]
3+
description = "Test suite that check only raised warnings related to general coding practices in files detected by a regular expression."
4+
5+
[warn]
6+
testNameRegex = "^E.*T.*D.*"
7+
testToolResFileOutput = "result.out"

save-core/src/commonNonJsMain/kotlin/org/cqfn/save/core/utils/TomlUtils.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.akuleshov7.ktoml.parsers.node.TomlTable
2222
import okio.FileSystem
2323
import okio.Path
2424

25+
import kotlin.system.exitProcess
2526
import kotlinx.serialization.ExperimentalSerializationApi
2627
import kotlinx.serialization.serializer
2728

@@ -64,7 +65,7 @@ private inline fun <reified T : PluginConfig> Path.createPluginConfig(
6465
" This file has incorrect toml format or missing section [$pluginSectionName]." +
6566
" Valid sections are: ${TestConfigSections.values().map { it.name.lowercase() }}."
6667
)
67-
throw e
68+
exitProcess(2)
6869
}
6970

7071
/**

save-core/src/commonNonJsTest/kotlin/org/cqfn/save/core/integration/ClassicWarnTest.kt

+11-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ClassicWarnTest {
3838
runTestsWithDiktat(
3939
listOf(
4040
"warn/chapter1"
41-
), 5
41+
), 6
4242
)
4343

4444
@Test
@@ -49,6 +49,14 @@ class ClassicWarnTest {
4949
), 1
5050
)
5151

52+
@Test
53+
fun `test output file set`() =
54+
runTestsWithDiktat(
55+
listOf(
56+
"warn/chapter1/TestResultsFileTest"
57+
), 1
58+
)
59+
5260
@Test
5361
@Ignore
5462
// FixMe: this test should be investigated, as resource discovery looks to be buggy
@@ -65,15 +73,15 @@ class ClassicWarnTest {
6573
runTestsWithDiktat(
6674
listOf(
6775
"warn/save.toml"
68-
), 5
76+
), 6
6977
)
7078

7179
@Test
7280
fun `executing warn plugin on parental save-toml file`() =
7381
runTestsWithDiktat(
7482
listOf(
7583
"warn/chapter1/save.toml"
76-
), 5
84+
), 6
7785
)
7886

7987
@Test

save-plugins/warn-plugin/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ linePlaceholder = "$line"
105105
patternForRegexInWarning = ["{{", "}}"]
106106
# if true - the regex created from expected warning will be wrapped with '.*': .*warn.*.
107107
partialWarnTextMatch = false # (default value)
108+
# if not set than stdout will be used as result of warn plugin execution
109+
testToolResFileOutput = "result.out" # (no default value is set)
108110
```
109111

110112
When executed from project root (where `save.propertes` is located), SAVE will cd to `rootDir` and discover all files

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.cqfn.save.plugin.warn.utils.Warning
1919
import org.cqfn.save.plugin.warn.utils.extractWarning
2020
import org.cqfn.save.plugin.warn.utils.getLineNumber
2121

22+
import okio.FileNotFoundException
2223
import okio.FileSystem
2324
import okio.Path
2425

@@ -83,7 +84,8 @@ class WarnPlugin(
8384
@Suppress(
8485
"TOO_LONG_FUNCTION",
8586
"SAY_NO_TO_VAR",
86-
"LongMethod"
87+
"LongMethod",
88+
"SwallowedException",
8789
)
8890
private fun handleTestFile(
8991
paths: List<Path>,
@@ -143,10 +145,23 @@ class WarnPlugin(
143145
)
144146
}.asSequence()
145147
}
146-
val stdout = executionResult.stdout
148+
val stdout =
149+
warnPluginConfig.testToolResFileOutput?.let {
150+
val testToolResFilePath = testConfig.directory / warnPluginConfig.testToolResFileOutput
151+
try {
152+
fs.readLines(testToolResFilePath)
153+
} catch (ex: FileNotFoundException) {
154+
logWarn("Trying to read file \"${warnPluginConfig.testToolResFileOutput}\" that was set as an output for a tested tool with testToolResFileOutput," +
155+
" but no such file found. Will use the stdout as an input.")
156+
executionResult.stdout
157+
}
158+
}
159+
?: run {
160+
executionResult.stdout
161+
}
147162
val stderr = executionResult.stderr
148163

149-
val actualWarningsMap = executionResult.stdout.mapNotNull {
164+
val actualWarningsMap = stdout.mapNotNull {
150165
with(warnPluginConfig) {
151166
val line = it.getLineNumber(actualWarningsPattern!!, lineCaptureGroupOut)
152167
it.extractWarning(

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import kotlinx.serialization.UseSerializers
4848
* For example: for `[warn] my {{[hello|world]}} warn` patternForRegexInWarning = {{.*}}. Opening and closing symbols should be split with '.*' symbol.
4949
* @property partialWarnTextMatch if true - the regex created from expected warning will be wrapped with '.*': .*warn.*.
5050
* That can help a user to write only main information in the warning without any need to add/copy-paste technical info
51+
* @property testToolResFileOutput file with actual warnings
5152
*/
5253
@Serializable
5354
data class WarnPluginConfig(
@@ -69,7 +70,8 @@ data class WarnPluginConfig(
6970
val linePlaceholder: String? = null,
7071
val wildCardInDirectoryMode: String? = null,
7172
val patternForRegexInWarning: List<String>? = null,
72-
val partialWarnTextMatch: Boolean? = null
73+
val partialWarnTextMatch: Boolean? = null,
74+
val testToolResFileOutput: String? = null,
7375
) : PluginConfig {
7476
@Transient
7577
override val type = TestConfigSections.WARN
@@ -109,7 +111,8 @@ data class WarnPluginConfig(
109111
this.linePlaceholder ?: other.linePlaceholder,
110112
this.wildCardInDirectoryMode ?: other.wildCardInDirectoryMode,
111113
this.patternForRegexInWarning ?: other.patternForRegexInWarning,
112-
this.partialWarnTextMatch ?: other.partialWarnTextMatch
114+
this.partialWarnTextMatch ?: other.partialWarnTextMatch,
115+
this.testToolResFileOutput ?: other.testToolResFileOutput,
113116
).also { it.configLocation = this.configLocation }
114117
}
115118

@@ -164,7 +167,8 @@ data class WarnPluginConfig(
164167
linePlaceholder ?: "\$line",
165168
wildCardInDirectoryMode,
166169
patternForRegexInWarning ?: defaultPatternForRegexInWarning,
167-
partialWarnTextMatch ?: false
170+
partialWarnTextMatch ?: false,
171+
testToolResFileOutput,
168172
).also { it.configLocation = this.configLocation }
169173
}
170174

0 commit comments

Comments
 (0)