Skip to content

Commit

Permalink
Enables output of comparison as json (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubiratansoares authored Oct 28, 2023
1 parent 1e0586e commit b5a8d81
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
5 changes: 5 additions & 0 deletions scripts/acceptance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ test_compare_baseline_with_artifact() {
echo "$comparison"
echo "$comparison" | grep "Your baseline file does not match the supplied artifact" >/dev/null

echo
echo "As json output:"
echo

"$aaw" compare -a "$fixtures/app-release-changed.apk" -b "$toml" --json | jq
rm -rf "$toml"
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/dotanuki/aaw/Injection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object Injection {
}

private val compareContext by lazy {
CompareContext(terminal, tomlSerializer)
CompareContext(terminal, tomlSerializer, jsonSerializer)
}

private val compareCommand by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package io.dotanuki.aaw.features.comparison

import arrow.core.raise.recover
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.options.switch
import io.dotanuki.aaw.core.android.AndroidArtifactAnalyser
import io.dotanuki.aaw.core.cli.ErrorReporter
import io.dotanuki.aaw.core.cli.ExitCodes
Expand All @@ -19,10 +21,13 @@ class CompareCommand : CliktCommand(
name = "compare"
) {

private val outputFormats = listOf("--json" to "json", "--console" to "console").toTypedArray()

private val pathToArchive: String by option("-a", "--archive").required()
private val pathToBaseline: String by option("-b", "--baseline").required()
private val exitWithFailure by option("--fail").flag(default = false)
private val withStacktraces by option("--stacktrace").flag(default = false)
private val format: String by option().switch(*outputFormats).default("console")

override fun run() {
ErrorReporter.printStackTraces = withStacktraces
Expand All @@ -34,7 +39,7 @@ class CompareCommand : CliktCommand(
val current = AndroidArtifactAnalyser.analyse(ValidatedFile(pathToArchive))
val reference = ValidatedTOML(ValidatedFile(pathToBaseline))
val comparison = ArtifactsComparator.compare(current, reference.asBaseline())
ComparisonReporter.reportChanges(comparison)
ComparisonReporter.reportChanges(comparison, format)

if (exitWithFailure && comparison.isNotEmpty()) {
exitProcess(ExitCodes.FAILURE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.dotanuki.aaw.features.comparison

import com.github.ajalt.mordant.terminal.Terminal
import kotlinx.serialization.json.Json
import net.peanuuutz.tomlkt.Toml

data class CompareContext(
val terminal: Terminal,
val tomlSerializer: Toml
val tomlSerializer: Toml,
val jsonSerializer: Json
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,48 @@ import io.dotanuki.aaw.core.cli.emptyLine

object ComparisonReporter {

private const val OUTCOME_NO_CHANGES = "No changes detected"
private const val OUTCOME_CHANGES_DETECTED = "Your baseline file does not match the supplied artifact"

context (CompareContext)
fun reportChanges(comparison: Set<ComparisonFinding>, format: String) {
when (format) {
"console" -> reportAsText(comparison)
"json" -> reportAsJson(comparison)
}
}

context (CompareContext)
private fun reportAsJson(comparison: Set<ComparisonFinding>) {
val outcome = when {
comparison.isEmpty() -> OUTCOME_NO_CHANGES
else -> OUTCOME_CHANGES_DETECTED
}

val results = comparison.map {
ComparisonResult(
item = it.what,
category = it.category.description,
finding = "Missing at ${it.expectation.description()}"
)
}

val serializable = SerializableComparison(outcome, results)
val jsonContent = jsonSerializer.encodeToString(SerializableComparison.serializer(), serializable)
terminal.println(jsonContent)
}

context (CompareContext)
fun reportChanges(comparison: Set<ComparisonFinding>) {
private fun reportAsText(comparison: Set<ComparisonFinding>) {
if (comparison.isEmpty()) {
terminal.emptyLine()
terminal.println("No changes detected")
terminal.println(OUTCOME_NO_CHANGES)
terminal.emptyLine()
return
}

terminal.emptyLine()
terminal.println("Your baseline file does not match the supplied artifact")
terminal.println(OUTCOME_CHANGES_DETECTED)
terminal.emptyLine()

val changeAsTable = table {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.dotanuki.aaw.features.comparison

import kotlinx.serialization.Serializable

@Serializable
data class SerializableComparison(
val outcome: String,
val results: List<ComparisonResult>
)

@Serializable
data class ComparisonResult(
val item: String,
val category: String,
val finding: String
)

0 comments on commit b5a8d81

Please sign in to comment.