Skip to content

Commit

Permalink
Replace kotlinx io Path class with String
Browse files Browse the repository at this point in the history
A way to abstract away from kotlinx-io classes from the core code base
  • Loading branch information
eyedol committed May 23, 2024
1 parent fd0ad32 commit 14dce92
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.github.takahirom.roborazzi

import com.dropbox.differ.ImageComparator
import kotlinx.io.files.Path

interface RoboCanvas {
val croppedWidth: Int
val croppedHeight: Int
val width: Int
val height: Int
fun save(
path: Path,
path: String,
resizeScale: Double,
contextData: Map<String, Any>,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.dropbox.differ.SimpleImageComparator
import com.github.takahirom.roborazzi.CaptureResults.Companion.json
import kotlinx.serialization.json.encodeToJsonElement
import java.io.File
import kotlinx.io.files.Path


@ExperimentalRoborazziApi
sealed interface RoborazziRecordFilePathStrategy {
Expand Down Expand Up @@ -174,7 +172,7 @@ data class RoborazziOptions(
getReportFileName(absolutePath, captureResult.timestampNs, nameWithoutExtension)

val jsonResult = json.encodeToJsonElement(captureResult)
KotlinxIo.writeText(Path(reportFileName), jsonResult.toString())
KotlinxIo.writeText(reportFileName, jsonResult.toString())
debugLog { "JsonResult file($reportFileName) has been written" }
}

Expand Down Expand Up @@ -221,13 +219,13 @@ expect fun defaultCaptureType(): RoborazziOptions.CaptureType
private fun getAssertErrorOrNull(captureResult: CaptureResult): AssertionError? =
when (captureResult) {
is CaptureResult.Added -> AssertionError(
"Roborazzi: The original file(${captureResult.goldenFile.absolutePath}) was not found.\n" +
"See the actual image at ${captureResult.actualFile.absolutePath}"
"Roborazzi: The original file(${captureResult.goldenFile}) was not found.\n" +
"See the actual image at ${captureResult.actualFile}"
)

is CaptureResult.Changed -> AssertionError(
"Roborazzi: ${captureResult.goldenFile.absolutePath} is changed.\n" +
"See the compare image at ${captureResult.compareFile.absolutePath}"
"Roborazzi: ${captureResult.goldenFile} is changed.\n" +
"See the compare image at ${captureResult.compareFile}"
)

is CaptureResult.Unchanged, is CaptureResult.Recorded -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.github.takahirom.roborazzi

import com.dropbox.differ.ImageComparator
import kotlinx.io.files.Path
import java.io.File

val File.ioPath: Path get() = Path(this.path)

fun interface EmptyCanvasFactory {
operator fun invoke(
width: Int,
Expand Down Expand Up @@ -115,7 +112,7 @@ fun processOutputImageAndReport(
)
comparisonCanvas
.save(
path = comparisonFile.ioPath,
path = comparisonFile.absolutePath,
resizeScale = resizeScale,
contextData = contextData
)
Expand All @@ -136,7 +133,7 @@ fun processOutputImageAndReport(
}
newRoboCanvas
.save(
path = actualFile.ioPath,
path = actualFile.absolutePath,
resizeScale = resizeScale,
contextData = contextData
)
Expand All @@ -146,24 +143,24 @@ fun processOutputImageAndReport(
}
if (goldenFile.exists()) {
CaptureResult.Changed(
compareFile = comparisonFile.ioPath,
actualFile = actualFile.ioPath,
goldenFile = goldenFile.ioPath,
compareFile = comparisonFile.absolutePath,
actualFile = actualFile.absolutePath,
goldenFile = goldenFile.absolutePath,
timestampNs = System.nanoTime(),
contextData = contextData,
)
} else {
CaptureResult.Added(
compareFile = comparisonFile.ioPath,
actualFile = actualFile.ioPath,
goldenFile = goldenFile.ioPath,
compareFile = comparisonFile.absolutePath,
actualFile = actualFile.absolutePath,
goldenFile = goldenFile.absolutePath,
timestampNs = System.nanoTime(),
contextData = contextData,
)
}
} else {
CaptureResult.Unchanged(
goldenFile = goldenFile.ioPath,
goldenFile = goldenFile.absolutePath,
timestampNs = System.nanoTime(),
contextData = contextData,
)
Expand All @@ -181,7 +178,7 @@ fun processOutputImageAndReport(
} else {
// roborazzi.record is checked before
newRoboCanvas.save(
path = goldenFile.ioPath,
path = goldenFile.absolutePath,
resizeScale = resizeScale,
contextData = contextData
)
Expand All @@ -191,7 +188,7 @@ fun processOutputImageAndReport(
}
roborazziOptions.reportOptions.captureResultReporter.report(
captureResult = CaptureResult.Recorded(
goldenFile = goldenFile.ioPath,
goldenFile = goldenFile.absolutePath,
timestampNs = System.nanoTime(),
contextData = contextData,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.takahirom.roborazzi

import com.github.takahirom.roborazzi.CaptureResults.Companion.json
import kotlinx.io.files.Path
import kotlinx.serialization.Contextual
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
Expand All @@ -20,12 +19,12 @@ import kotlinx.serialization.json.jsonPrimitive
sealed interface CaptureResult {
val type: String
val timestampNs: Long
val compareFile: Path?
val actualFile: Path?
val goldenFile: Path?
val compareFile: String?
val actualFile: String?
val goldenFile: String?
val contextData: Map<String,@Contextual Any>

val reportFile: Path
val reportFile: String
get() = when (val result = this) {
is Added -> result.actualFile
is Changed -> result.compareFile
Expand All @@ -36,27 +35,27 @@ sealed interface CaptureResult {
@Serializable
data class Recorded(
@SerialName("golden_file_path")
override val goldenFile:@Contextual Path,
override val goldenFile:@Contextual String,
@SerialName("timestamp")
override val timestampNs: Long,
@SerialName("context_data")
override val contextData: Map<String,@Contextual Any>
) : CaptureResult {
override val type = "recorded"
override val actualFile: Path?
override val actualFile: String?
get() = null
override val compareFile: Path?
override val compareFile: String?
get() = null
}

@Serializable
data class Added(
@SerialName("compare_file_path")
override val compareFile:@Contextual Path,
override val compareFile:@Contextual String,
@SerialName("actual_file_path")
override val actualFile:@Contextual Path,
override val actualFile:@Contextual String,
@SerialName("golden_file_path")
override val goldenFile:@Contextual Path,
override val goldenFile:@Contextual String,
@SerialName("timestamp")
override val timestampNs: Long,
@SerialName("context_data")
Expand All @@ -68,11 +67,11 @@ sealed interface CaptureResult {
@Serializable
data class Changed(
@SerialName("compare_file_path")
override val compareFile:@Contextual Path,
override val compareFile:@Contextual String,
@SerialName("golden_file_path")
override val goldenFile:@Contextual Path,
override val goldenFile:@Contextual String,
@SerialName("actual_file_path")
override val actualFile:@Contextual Path,
override val actualFile:@Contextual String,
@SerialName("timestamp")
override val timestampNs: Long,
@SerialName("context_data")
Expand All @@ -84,22 +83,22 @@ sealed interface CaptureResult {
@Serializable
data class Unchanged(
@SerialName("golden_file_path")
override val goldenFile:@Contextual Path,
override val goldenFile:@Contextual String,
@SerialName("timestamp")
override val timestampNs: Long,
@SerialName("context_data")
override val contextData: Map<String,@Contextual Any>
) : CaptureResult {
override val type = "unchanged"
override val actualFile: Path?
override val actualFile: String?
get() = null
override val compareFile: Path?
override val compareFile: String?
get() = null
}

companion object {
fun fromJsonFile(filePath: String): CaptureResult {
val string = KotlinxIo.readText(Path(filePath))
val string = KotlinxIo.readText(filePath)
val jsonElement = json.parseToJsonElement(string)
return json.decodeFromJsonElement<CaptureResult>(jsonElement)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encodeToString
import kotlinx.serialization.encoding.Decoder
Expand Down Expand Up @@ -111,9 +109,9 @@ data class CaptureResults(
images: List<CaptureResult>,
reportDirectoryPath: String
): String {
fun Path.pathFrom(reportDirectoryPath: String): String {
fun String.pathFrom(reportDirectoryPath: String): String {
val reportDirectory = Path(reportDirectoryPath)
val relativePath = relativeTo(reportDirectory)
val relativePath = Path(this).relativeTo(reportDirectory)
return relativePath.toString()
}
if (images.isEmpty()) return ""
Expand Down Expand Up @@ -166,27 +164,12 @@ data class CaptureResults(
ignoreUnknownKeys = true
classDiscriminator = "#class"
serializersModule = SerializersModule {
contextual(Path::class,
object : KSerializer<Path> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("FileSerializer", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: Path) {
encoder.encodeString(value.absolutePath)
}

override fun deserialize(decoder: Decoder): Path {
val path = decoder.decodeString()
return Path(path)
}
}
)
contextual(Any::class, AnySerializer)
}
}

fun fromJsonFile(inputPath: String): CaptureResults {
val string = KotlinxIo.readText(Path(inputPath))
val string = KotlinxIo.readText(inputPath)
val jsonElement = json.parseToJsonElement(string)
return json.decodeFromJsonElement(jsonElement)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import kotlinx.io.write
val Path.absolutePath: String
get() = this.toString()

val Path.nameWithoutExtension: String
get() = name.substringBeforeLast(".")
val String.nameWithoutExtension: String
get() = Path(this).name.substringBeforeLast(".")

val String.ioPath: Path
get() = Path(this)
val String.name: String
get() = Path(this).name

fun Path.relativeTo(base: Path): Path {
if (this == base) return Path("")
Expand Down Expand Up @@ -45,16 +45,14 @@ private fun String.segments(): List<String> = split("/")
@OptIn(ExperimentalStdlibApi::class)
object KotlinxIo {


fun readText(path: Path): String {

return SystemFileSystem.source(path).buffered().use { source ->
fun readText(path: String): String {
return SystemFileSystem.source(Path(path)).buffered().use { source ->
source.readString()
}
}

fun writeText(path: Path, text: String) {
SystemFileSystem.sink(path).buffered().use { sink ->
fun writeText(path: String, text: String) {
SystemFileSystem.sink(Path(path)).buffered().use { sink ->
sink.write(text.encodeToByteString())
}
}
Expand Down
Loading

0 comments on commit 14dce92

Please sign in to comment.