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

Breakline at the end of the SARIF file. #100

Merged
merged 4 commits into from
Feb 22, 2024
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
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"
TWiStErRob marked this conversation as resolved.
Show resolved Hide resolved

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 @@
}
}
]
}
}