Skip to content

Commit

Permalink
Implement FSOkio, templated from FSJava (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Feb 1, 2024
2 parents d102aee + 47b43b3 commit 1268da1
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interface FS {
fun fileRead(typedPath: TypedPath): String
fun fileWrite(typedPath: TypedPath, content: String)
/** Creates an assertion failed exception to throw. */
fun assertFailed(message: String, expected: Any? = null, actual: Any? = null): Error
fun assertFailed(message: String, expected: Any? = null, actual: Any? = null): Throwable
}

/** NOT FOR ENDUSERS. Implemented by Selfie to integrate with various test frameworks. */
Expand Down
1 change: 1 addition & 0 deletions jvm/selfie-runner-kotest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ kotlin {
dependencies {
api project(":selfie-lib")
api "io.kotest:kotest-framework-engine:$ver_KOTEST"
api "io.kotest:kotest-assertions-shared:$ver_KOTEST"
implementation "com.squareup.okio:okio:$ver_OKIO"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.selfie.kotest

import com.diffplug.selfie.guts.FS
import com.diffplug.selfie.guts.TypedPath
import io.kotest.assertions.Actual
import io.kotest.assertions.Exceptions
import io.kotest.assertions.Expected
import io.kotest.assertions.print.Printed
import okio.FileSystem
import okio.Path.Companion.toPath

expect internal val FS_SYSTEM: FileSystem
fun TypedPath.toPath(): okio.Path = absolutePath.toPath()

object FSOkio : FS {
/** Walks the files (not directories) which are children and grandchildren of the given path. */
override fun <T> fileWalk(typedPath: TypedPath, walk: (Sequence<TypedPath>) -> T): T =
walk(
FS_SYSTEM.listRecursively(typedPath.toPath()).mapNotNull {
if (FS_SYSTEM.metadata(it).isRegularFile) TypedPath.ofFile(it.toString()) else null
})
override fun fileRead(typedPath: TypedPath): String =
FS_SYSTEM.read(typedPath.toPath()) { readUtf8() }
override fun fileWrite(typedPath: TypedPath, content: String): Unit =
FS_SYSTEM.write(typedPath.toPath()) { writeUtf8(content) }
/** Creates an assertion failed exception to throw. */
override fun assertFailed(message: String, expected: Any?, actual: Any?): Throwable =
if (expected == null && actual == null) Exceptions.createAssertionError(message, null)
else {
val expectedStr = nullableToString(expected, "")
val actualStr = nullableToString(actual, "")
if (expectedStr.isEmpty() && actualStr.isEmpty() && (expected == null || actual == null)) {
// make sure that nulls are not ambiguous
val onNull = "(null)"
comparisonAssertion(
message, nullableToString(expected, onNull), nullableToString(actual, onNull))
} else {
comparisonAssertion(message, expectedStr, actualStr)
}
}
private fun nullableToString(any: Any?, onNull: String): String =
any?.let { it.toString() } ?: onNull
private fun comparisonAssertion(message: String, expected: String, actual: String): Throwable {
return Exceptions.createAssertionError(
message, null, Expected(Printed((expected))), Actual(Printed((actual))))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ import com.diffplug.selfie.guts.TypedPath
import io.kotest.core.spec.style.StringSpec
import io.kotest.core.test.TestCaseOrder
import io.kotest.matchers.shouldBe
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath

expect val FS_SYSTEM: FileSystem
expect val IS_WINDOWS: Boolean

expect fun exec(cwd: TypedPath, vararg args: String): String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.selfie.kotest

import okio.FileSystem

internal actual val FS_SYSTEM: FileSystem
get() = okio.NodeJsFileSystem
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
package com.diffplug.selfie.kotest

import com.diffplug.selfie.guts.TypedPath
import okio.FileSystem
actual val FS_SYSTEM: FileSystem
get() = okio.NodeJsFileSystem
actual val IS_WINDOWS: Boolean
get() = TODO("Not yet implemented")
actual fun exec(cwd: TypedPath, vararg args: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.selfie.kotest

import okio.FileSystem

internal actual val FS_SYSTEM: FileSystem
get() = FileSystem.SYSTEM
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import com.diffplug.selfie.guts.TypedPath
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import okio.FileSystem
actual val FS_SYSTEM: FileSystem
get() = FileSystem.SYSTEM
actual val IS_WINDOWS: Boolean
get() = System.getProperty("os.name").lowercase().contains("windows")
actual fun exec(cwd: TypedPath, vararg args: String): String {
Expand Down

0 comments on commit 1268da1

Please sign in to comment.