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

feat: Dump shards #1921

Merged
merged 3 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions corellium/shard/dump/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Shard dump

Allows dumping shards as formatted json file.

## Example

For the example input structure:

```kotlin
val input: List<List<Shard.App>> =
listOf(
listOf(
Shard.App(
name = "app1",
tests = listOf(
Shard.Test(
name = "app1-test1",
cases = listOf(
Shard.Test.Case(
name = "app1.test1.Test1#case1",
duration = 10_000,
),
)
),
)
),
),
listOf(
Shard.App(
name = "app1",
tests = listOf(
Shard.Test(
name = "app1-test1",
cases = listOf(
Shard.Test.Case(
name = "app1.test1.Test1#case2",
duration = 2_000,
),
)
),
)
),
Shard.App(
name = "app2",
tests = listOf(
Shard.Test(
name = "app2-test1",
cases = listOf(
Shard.Test.Case(
name = "app2.test1.Test2#case1",
duration = 1_000,
),
)
),
Shard.Test(
name = "app2-test1",
cases = listOf(
Shard.Test.Case(
name = "app2.test1.Test2#case2",
),
)
),
)
),
)
)
```

The call of:

```kotlin
input.dumpToFile("path/to/file.json")
```

Should create json file under the path `path/to/file.json` that contains:
```json
[
[
{
"name": "app1",
"tests": [
{
"name": "app1-test1",
"cases": [
{
"name": "app1.test1.Test1#case1",
"duration": 10000
}
]
}
]
}
],
[
{
"name": "app1",
"tests": [
{
"name": "app1-test1",
"cases": [
{
"name": "app1.test1.Test1#case2",
"duration": 2000
}
]
}
]
},
{
"name": "app2",
"tests": [
{
"name": "app2-test1",
"cases": [
{
"name": "app2.test1.Test2#case1",
"duration": 1000
}
]
},
{
"name": "app2-test1",
"cases": [
{
"name": "app2.test1.Test2#case2",
"duration": 120
}
]
}
]
}
]
]
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin(Plugins.Kotlin.PLUGIN_JVM)
kotlin(Plugins.Kotlin.PLUGIN_SERIALIZATION) version Versions.KOTLIN
}

repositories {
jcenter()
mavenCentral()
maven(url = "https://kotlin.bintray.com/kotlinx")
}
Expand All @@ -14,10 +14,8 @@ tasks.withType<KotlinCompile> { kotlinOptions.jvmTarget = "1.8" }

dependencies {
implementation(Dependencies.KOTLIN_COROUTINES_CORE)

implementation(Dependencies.JACKSON_KOTLIN)
implementation(Dependencies.JACKSON_YAML)
implementation(Dependencies.JACKSON_XML)
implementation(Dependencies.GSON)
api(project(":corellium:shard"))

testImplementation(Dependencies.JUNIT)
}
18 changes: 18 additions & 0 deletions corellium/shard/dump/src/main/kotlin/flank/corellium/shard/Dump.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package flank.corellium.shard

import java.nio.file.Files.newBufferedWriter
import java.nio.file.Paths.get

/**
* Dump shards as formatted json file.
*
* @receiver List of shards to dump.
* @param filePath Relative or absolut path the file.
*/
fun Shards.dumpToFile(
filePath: String
) {
val writer = newBufferedWriter(get(filePath))
prettyGson.toJson(this, writer)
writer.flush()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package flank.corellium.shard

import com.google.gson.Gson
import com.google.gson.GsonBuilder

internal val prettyGson: Gson = GsonBuilder().setPrettyPrinting().create()
143 changes: 143 additions & 0 deletions corellium/shard/dump/src/test/kotlin/flank/corellium/shard/DumpTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package flank.corellium.shard

import org.junit.After
import org.junit.Assert
import org.junit.Test
import java.io.File

class DumpTest {

private val filePath = "dump-shards.json"
private val file = File(filePath)

private val input: List<List<Shard.App>> =
listOf(
listOf(
Shard.App(
name = "app1",
tests = listOf(
Shard.Test(
name = "app1-test1",
cases = listOf(
Shard.Test.Case(
name = "app1.test1.Test1#case1",
duration = 10_000,
),
)
),
)
),
),
listOf(
Shard.App(
name = "app1",
tests = listOf(
Shard.Test(
name = "app1-test1",
cases = listOf(
Shard.Test.Case(
name = "app1.test1.Test1#case2",
duration = 2_000,
),
)
),
)
),
Shard.App(
name = "app2",
tests = listOf(
Shard.Test(
name = "app2-test1",
cases = listOf(
Shard.Test.Case(
name = "app2.test1.Test2#case1",
duration = 1_000,
),
)
),
Shard.Test(
name = "app2-test1",
cases = listOf(
Shard.Test.Case(
name = "app2.test1.Test2#case2",
),
)
),
)
),
)
)

private val expected = """
[
[
{
"name": "app1",
"tests": [
{
"name": "app1-test1",
"cases": [
{
"name": "app1.test1.Test1#case1",
"duration": 10000
}
]
}
]
}
],
[
{
"name": "app1",
"tests": [
{
"name": "app1-test1",
"cases": [
{
"name": "app1.test1.Test1#case2",
"duration": 2000
}
]
}
]
},
{
"name": "app2",
"tests": [
{
"name": "app2-test1",
"cases": [
{
"name": "app2.test1.Test2#case1",
"duration": 1000
}
]
},
{
"name": "app2-test1",
"cases": [
{
"name": "app2.test1.Test2#case2",
"duration": 120
}
]
}
]
}
]
]
""".trimIndent()

@Test
fun testDumpToFile() {
input.dumpToFile(filePath)

Assert.assertTrue(file.exists())
Assert.assertEquals(expected, file.readText().trim())
}

@After
fun cleanUp() {
file.delete()
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include(
":corellium:api",
":corellium:shard",
":corellium:shard:calculate",
":corellium:shard:dump",
":corellium:adapter",
":corellium:junit",
)
Expand Down