Skip to content

Commit e205850

Browse files
authored
Replace List<Path> with TestFiles in TestResult (#284)
### What's done: * Changed property of `TestResult` and updated usages
1 parent 703e8ce commit e205850

File tree

12 files changed

+82
-28
lines changed

12 files changed

+82
-28
lines changed

save-cli/src/jvmTest/kotlin/org/cqfn/save/cli/GeneralTest.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ class GeneralTest {
8484
// All result statuses should be Pass
8585
json.forEach { report ->
8686
report.pluginExecutions.forEach { pluginExecution ->
87-
pluginExecution.testResults.forEach { result ->
87+
pluginExecution.testResults.find { result ->
8888
println(result.status)
89-
result.resources.find { it.name == "ThisShouldAlwaysFailTest.kt" }
90-
?: assertTrue(result.status is Pass)
89+
result.resources.test.name != "ThisShouldAlwaysFailTest.kt"
90+
}?.let {
91+
assertTrue(it.status is Pass)
9192
}
9293
}
9394
}

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import org.cqfn.save.core.files.findDescendantDirectoriesBy
77
import org.cqfn.save.core.files.parentsWithSelf
88
import org.cqfn.save.core.logging.logDebug
99
import org.cqfn.save.core.result.TestResult
10+
import org.cqfn.save.core.utils.PathSerializer
1011
import org.cqfn.save.core.utils.ProcessBuilder
1112

1213
import okio.FileSystem
1314
import okio.Path
1415
import okio.Path.Companion.toPath
1516

17+
import kotlinx.serialization.Serializable
18+
1619
/**
1720
* Plugin that can be injected into SAVE during execution. Plugins accept contents of configuration file and then perform some work.
1821
* @property testConfig
@@ -210,10 +213,20 @@ abstract class Plugin(
210213
* path to test file
211214
*/
212215
val test: Path
216+
217+
/**
218+
* @param root path to calculate relative paths
219+
* @return copy of `this` with relativized paths
220+
*/
221+
fun withRelativePaths(root: Path): TestFiles
213222
}
214223

215224
/**
216225
* @property test test file
217226
*/
218-
data class Test(override val test: Path) : TestFiles
227+
@Serializable
228+
data class Test(@Serializable(with = PathSerializer::class) override val test: Path) : TestFiles {
229+
override fun withRelativePaths(root: Path) =
230+
copy(test = test.createRelativePathToTheRoot(root).toPath())
231+
}
219232
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
package org.cqfn.save.core.result
44

5+
import org.cqfn.save.core.plugin.Plugin
56
import org.cqfn.save.core.utils.PathSerializer
67

7-
import okio.Path
8-
98
import kotlinx.serialization.Serializable
109
import kotlinx.serialization.UseSerializers
1110

@@ -18,7 +17,7 @@ import kotlinx.serialization.UseSerializers
1817
*/
1918
@Serializable
2019
data class TestResult(
21-
val resources: Collection<Path>,
20+
val resources: Plugin.TestFiles,
2221
val status: TestStatus,
2322
val debugInfo: DebugInfo? = null,
2423
)

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import org.cqfn.save.core.config.SaveProperties
77
import org.cqfn.save.core.config.isSaveTomlConfig
88
import org.cqfn.save.core.files.ConfigDetector
99
import org.cqfn.save.core.files.StdStreamsSink
10-
import org.cqfn.save.core.files.createRelativePathToTheRoot
1110
import org.cqfn.save.core.logging.logDebug
1211
import org.cqfn.save.core.logging.logError
1312
import org.cqfn.save.core.logging.logInfo
@@ -22,6 +21,7 @@ import org.cqfn.save.core.result.Pass
2221
import org.cqfn.save.core.result.TestResult
2322
import org.cqfn.save.core.utils.buildActivePlugins
2423
import org.cqfn.save.core.utils.processInPlace
24+
import org.cqfn.save.plugins.fix.FixPlugin
2525
import org.cqfn.save.reporter.json.JsonReporter
2626
import org.cqfn.save.reporter.plain.PlainOnlyFailedReporter
2727
import org.cqfn.save.reporter.plain.PlainTextReporter
@@ -135,8 +135,7 @@ class Save(
135135
plugin.execute()
136136
.onEach { event ->
137137
// calculate relative paths, because reporters don't need paths higher than root dir
138-
val resourcesRelative =
139-
event.resources.map { it.createRelativePathToTheRoot(testRepositoryRootPath).toPath() }
138+
val resourcesRelative = event.resources.withRelativePaths(testRepositoryRootPath)
140139
reporter.onEvent(event.copy(resources = resourcesRelative))
141140
}
142141
.forEach(this::handleResult)
@@ -164,7 +163,9 @@ class Save(
164163
return when (saveProperties.reportType) {
165164
ReportType.PLAIN -> PlainTextReporter(out)
166165
ReportType.PLAIN_FAILED -> PlainOnlyFailedReporter(out)
167-
ReportType.JSON -> JsonReporter(out)
166+
ReportType.JSON -> JsonReporter(out) {
167+
FixPlugin.FixTestFiles.register(this)
168+
}
168169
ReportType.TEST -> TestReporter(out)
169170
else -> TODO("Reporter for type ${saveProperties.reportType} is not yet supported")
170171
}

save-core/src/commonNonJsTest/kotlin/org/cqfn/save/core/test/utils/TestUtilsCommon.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fun runTestsWithDiktat(
5353
assertEquals(numberOfTests, testReporter.results.size)
5454
testReporter.results.forEach { test ->
5555
// FixMe: if we will have other failing tests - we will make the logic less hardcoded
56-
test.resources.find { it.name == "ThisShouldAlwaysFailTest.kt" }?.let {
56+
if (test.resources.test.name == "ThisShouldAlwaysFailTest.kt") {
5757
assertEquals(
5858
Fail(
5959
"Some warnings were expected but not received:" +
@@ -62,9 +62,8 @@ fun runTestsWithDiktat(
6262
"Some warnings were expected but not received (1)"
6363
), test.status
6464
)
65+
} else {
66+
assertEquals(Pass(null), test.status)
6567
}
66-
?: run {
67-
assertEquals(Pass(null), test.status)
68-
}
6968
}
7069
}

save-plugins/fix-and-warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fixandwarn/FixAndWarnPlugin.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class FixAndWarnPlugin(
7878
val (fixTestResultsPassed, fixTestResultsFailed) = fixTestResults.partition { it.status is Pass }
7979

8080
val expectedFilesWithPass = expectedFiles.filter { expectedFile ->
81-
fixTestResultsPassed.map { it.resources.toList()[1] }.contains(expectedFile)
81+
fixTestResultsPassed.map { (it.resources as FixPlugin.FixTestFiles).expected }.contains(expectedFile)
8282
}
8383

8484
// Fill back original data with warnings

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.cqfn.save.plugins.fix
22

33
import org.cqfn.save.core.config.TestConfig
44
import org.cqfn.save.core.files.createFile
5+
import org.cqfn.save.core.files.createRelativePathToTheRoot
56
import org.cqfn.save.core.files.readLines
67
import org.cqfn.save.core.logging.describe
78
import org.cqfn.save.core.plugin.ExtraFlags
@@ -13,6 +14,7 @@ import org.cqfn.save.core.result.DebugInfo
1314
import org.cqfn.save.core.result.Fail
1415
import org.cqfn.save.core.result.Pass
1516
import org.cqfn.save.core.result.TestResult
17+
import org.cqfn.save.core.utils.PathSerializer
1618
import org.cqfn.save.core.utils.ProcessExecutionException
1719

1820
import io.github.petertrr.diffutils.diff
@@ -23,6 +25,11 @@ import io.github.petertrr.diffutils.patch.Patch
2325
import io.github.petertrr.diffutils.text.DiffRowGenerator
2426
import okio.FileSystem
2527
import okio.Path
28+
import okio.Path.Companion.toPath
29+
30+
import kotlinx.serialization.Serializable
31+
import kotlinx.serialization.modules.PolymorphicModuleBuilder
32+
import kotlinx.serialization.modules.subclass
2633

2734
/**
2835
* A plugin that runs an executable on a file and compares output with expected output.
@@ -81,7 +88,7 @@ class FixPlugin(
8188
} catch (ex: ProcessExecutionException) {
8289
return@map chunk.map {
8390
TestResult(
84-
pathMap.map { (test, expected) -> listOf(test, expected) }.flatten(),
91+
it,
8592
Fail(ex.describe(), ex.describe()),
8693
DebugInfo(null, ex.message, null)
8794
)
@@ -98,7 +105,7 @@ class FixPlugin(
98105
val test = pathMap.first { (test, _) -> test.name == testCopy.name }.first
99106

100107
TestResult(
101-
listOf(test, expected),
108+
FixTestFiles(test, expected),
102109
checkStatus(expectedLines, fixedLines),
103110
DebugInfo(
104111
stdout.filter { it.contains(testCopy.name) }.joinToString("\n"),
@@ -195,5 +202,22 @@ class FixPlugin(
195202
* @property test test file
196203
* @property expected expected file
197204
*/
198-
data class FixTestFiles(override val test: Path, val expected: Path) : TestFiles
205+
@Serializable
206+
data class FixTestFiles(
207+
@Serializable(with = PathSerializer::class) override val test: Path,
208+
@Serializable(with = PathSerializer::class) val expected: Path
209+
) : TestFiles {
210+
override fun withRelativePaths(root: Path) = copy(
211+
test = test.createRelativePathToTheRoot(root).toPath(),
212+
expected = expected.createRelativePathToTheRoot(root).toPath(),
213+
)
214+
215+
companion object {
216+
/**
217+
* @param builder `PolymorphicModuleBuilder` to which this class should be registered for serialization
218+
*/
219+
fun register(builder: PolymorphicModuleBuilder<TestFiles>): Unit =
220+
builder.subclass(FixTestFiles::class)
221+
}
222+
}
199223
}

save-plugins/fix-plugin/src/commonNonJsTest/kotlin/org/cqfn/save/plugins/fix/FixPluginTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class FixPluginTest {
8888
).execute()
8989

9090
assertEquals(1, results.count(), "Size of results should equal number of pairs")
91-
assertEquals(TestResult(listOf(testFile, expectedFile), Pass(null),
91+
assertEquals(TestResult(FixPlugin.FixTestFiles(testFile, expectedFile), Pass(null),
9292
DebugInfo(results.single().debugInfo?.stdout, results.single().debugInfo?.stderr, null)), results.single())
9393
val tmpDir = (FileSystem.SYSTEM_TEMPORARY_DIRECTORY / FixPlugin::class.simpleName!!)
9494
assertTrue("Files should be identical") {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ class WarnPlugin(
135135
val executionResult = try {
136136
pb.exec(execCmd, testConfig.getRootConfig().directory.toString(), redirectTo)
137137
} catch (ex: ProcessExecutionException) {
138-
return sequenceOf(
138+
return paths.map {
139139
TestResult(
140-
paths,
140+
Test(it),
141141
Fail(ex.describe(), ex.describe()),
142142
DebugInfo(null, ex.message, null)
143143
)
144-
)
144+
}.asSequence()
145145
}
146146
val stdout = executionResult.stdout
147147
val stderr = executionResult.stderr
@@ -169,7 +169,7 @@ class WarnPlugin(
169169

170170
return paths.map { path ->
171171
TestResult(
172-
listOf(path),
172+
Test(path),
173173
resultsChecker.checkResults(path.name),
174174
DebugInfo(
175175
stdout.filter { it.contains(path.name) }.joinToString("\n"),

save-reporters/src/commonMain/kotlin/org/cqfn/save/reporter/json/JsonReporter.kt

+18-2
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,35 @@ import okio.BufferedSink
1010

1111
import kotlinx.serialization.encodeToString
1212
import kotlinx.serialization.json.Json
13+
import kotlinx.serialization.modules.PolymorphicModuleBuilder
14+
import kotlinx.serialization.modules.SerializersModule
15+
import kotlinx.serialization.modules.polymorphic
16+
import kotlinx.serialization.modules.subclass
1317

1418
/**
1519
* Reporter that produces a JSON report as a [Report]
1620
*
1721
* @property out a sink for output
22+
*
23+
* @param builder additional configuration lambda for serializers module
1824
*/
19-
class JsonReporter(override val out: BufferedSink) : Reporter {
25+
class JsonReporter(
26+
override val out: BufferedSink,
27+
builder: PolymorphicModuleBuilder<Plugin.TestFiles>.() -> Unit = {}
28+
) : Reporter {
2029
override val type: ReportType = ReportType.JSON
2130

2231
/**
2332
* Formatter to serialize/deserialize to JSON
2433
*/
25-
internal val json = Json
34+
val json: Json = Json {
35+
serializersModule = SerializersModule {
36+
polymorphic(Plugin.TestFiles::class) {
37+
subclass(Plugin.Test::class)
38+
builder()
39+
}
40+
}
41+
}
2642

2743
private var isFirstEvent = true // todo: use AtomicBoolean
2844

save-reporters/src/commonMain/kotlin/org/cqfn/save/reporter/plain/PlainTextReporter.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ open class PlainTextReporter(override val out: BufferedSink) : Reporter {
7474
}
7575
?: ""
7676
out.write(
77-
"| $currentTestSuite | $currentPlugin | ${event.resources.first()} | ${event.status::class.simpleName} | $shortComment |\n"
77+
"| $currentTestSuite | $currentPlugin | ${event.resources.test} | ${event.status::class.simpleName} | $shortComment |\n"
7878
.encodeToByteArray()
7979
)
8080
}

save-reporters/src/commonTest/kotlin/org/cqfn/save/reporter/json/JsonReporterTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.cqfn.save.reporter.json
22

33
import org.cqfn.save.core.files.readFile
4+
import org.cqfn.save.core.plugin.Plugin
45
import org.cqfn.save.core.result.Crash
56
import org.cqfn.save.core.result.TestResult
67
import org.cqfn.save.plugin.MockPlugin
@@ -59,7 +60,7 @@ class JsonReporterTest {
5960
jsonReporter.onPluginExecutionStart(mockPlugin)
6061
jsonReporter.onEvent(
6162
TestResult(
62-
emptyList(),
63+
Plugin.Test(tmpFile),
6364
Crash("IllegalArgumentException", "foo")
6465
)
6566
)

0 commit comments

Comments
 (0)