Skip to content

Commit

Permalink
Breakline at the end of the SARIF file. (#100)
Browse files Browse the repository at this point in the history
* Improve tests

* Add new line at the end of the file

* Don't use File

* format
  • Loading branch information
BraisGabin authored Feb 22, 2024
1 parent 0917dc6 commit 0f8ebec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object SarifSerializer {

fun toMinifiedJson(sarif: SarifSchema210): String = Json.encodeToString(sarif)

fun toJson(sarif: SarifSchema210): String = json.encodeToString(sarif)
fun toJson(sarif: SarifSchema210): String = json.encodeToString(sarif) + "\n"

fun fromJson(json: String): SarifSchema210 = Json.decodeFromString(json)
}
}
19 changes: 10 additions & 9 deletions src/jvmTest/kotlin/io/github/detekt/sarif4k/SarifSerializerTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.detekt.sarif4k

import org.junit.jupiter.api.Test
import java.io.File
import java.io.Reader
import kotlin.test.assertEquals

class SarifSerializerTest {

val sarifSchema210 = SarifSchema210(
private val sarifSchema210 = SarifSchema210(
schema = "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
version = Version.The210,
runs = listOf(
Expand Down Expand Up @@ -76,22 +76,23 @@ class SarifSerializerTest {
@Test
fun `serialize kotlin data class to minified json`() {
val actual = SarifSerializer.toMinifiedJson(sarifSchema210)
val expected = File(SarifSerializerTest::class.java.getResource("/test.sarif.json").path).readText()
val expected = getResource("/test.sarif.json").readText()
assertEquals(expected.replace("\\s".toRegex(), ""), actual)
}

@Test
fun `serialize kotlin data class to json`() {
val actual = SarifSerializer.toJson(sarifSchema210).lines()
val expected = File(SarifSerializerTest::class.java.getResource("/test.sarif.json").path).readLines()
val actual = SarifSerializer.toJson(sarifSchema210)
val expected = getResource("/test.sarif.json").readText()
assertEquals(expected, actual)
}

@Test
fun `deserialize json into kotlin data class`() {
val actual = SarifSerializer.fromJson(
File(SarifSerializerTest::class.java.getResource("/test.sarif.json").path).readText()
)
val actual = SarifSerializer.fromJson(getResource("/test.sarif.json").readText())
assertEquals(sarifSchema210, actual)
}
}
}

private fun getResource(path: String): Reader =
SarifSerializerTest::class.java.getResourceAsStream(path)!!.reader()
2 changes: 1 addition & 1 deletion src/jvmTest/resources/test.sarif.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@
}
}
]
}
}

0 comments on commit 0f8ebec

Please sign in to comment.