diff --git a/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/junit5/SelfieConfig.kt b/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/junit5/SelfieConfig.kt index 719bb6da..d6f8a0be 100644 --- a/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/junit5/SelfieConfig.kt +++ b/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/junit5/SelfieConfig.kt @@ -22,7 +22,7 @@ import java.nio.file.Paths internal class SnapshotFileLayout( val rootFolder: Path, val snapshotFolderName: String?, - val unixNewlines: Boolean + internal val unixNewlines: Boolean ) { val extension: String = ".ss" fun snapshotPathForClass(className: String): Path { @@ -75,14 +75,27 @@ internal class SnapshotFileLayout( val snapshotRootFolder = rootFolder(properties.getProperty("output-dir")) // it's pretty easy to preserve the line endings of existing snapshot files, but it's // a bit harder to create a fresh snapshot file with the correct line endings. - val cr = - snapshotRootFolder - .resolve(snapshotFolderName!!) - .toFile() - .walkTopDown() - .filter { it.isFile } - .any { it.readText().contains('\r') } - return SnapshotFileLayout(snapshotRootFolder, snapshotFolderName, !cr) + return SnapshotFileLayout( + snapshotRootFolder, snapshotFolderName, inferDefaultLineEndingIsUnix(snapshotRootFolder)) + } + private fun inferDefaultLineEndingIsUnix(rootFolder: Path): Boolean { + return rootFolder + .toFile() + .walkTopDown() + .filter { it.isFile } + .mapNotNull { + try { + val txt = it.readText() + // look for a file that has a newline somewhere in it + if (txt.indexOf('\n') != -1) txt else null + } catch (e: Exception) { + // might be a binary file that throws an encoding exception + null + } + } + .firstOrNull() + ?.let { it.indexOf('\r') == -1 } + ?: true // if we didn't find any files, assume unix } private fun snapshotFolderName(snapshotDir: String?): String? { if (snapshotDir == null) { diff --git a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/CarriageReturnTest.kt b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/CarriageReturnTest.kt index 27d2f932..5091a315 100644 --- a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/CarriageReturnTest.kt +++ b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/CarriageReturnTest.kt @@ -63,9 +63,4 @@ class CarriageReturnTest : Harness("undertest-junit5") { gradleWriteSS() ut_snapshot().assertContent(expectedContent) } - - @Test @Order(5) - fun deleteSelfie() { - ut_snapshot().deleteIfExists() - } } diff --git a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/DuplicateWriteTest.kt b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/DuplicateWriteTest.kt index d65dd3cc..3bc8a60d 100644 --- a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/DuplicateWriteTest.kt +++ b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/DuplicateWriteTest.kt @@ -62,9 +62,4 @@ class DuplicateWriteTest : Harness("undertest-junit5") { gradlew("underTest", "-Pselfie=writeonce")!!.message shouldStartWith "Snapshot was set to the same value multiple times" } - - @Test @Order(6) - fun deleteSelfie() { - ut_snapshot().deleteIfExists() - } } diff --git a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/ReadWriteTest.kt b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/ReadWriteTest.kt index d6e047b6..c4a3f1ad 100644 --- a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/ReadWriteTest.kt +++ b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/ReadWriteTest.kt @@ -81,9 +81,4 @@ class ReadWriteTest : Harness("undertest-junit5") { """ .trimIndent()) } - - @Test @Order(6) - fun deleteSelfie() { - ut_snapshot().deleteIfExists() - } } diff --git a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt index a7ccb84f..cafcbb27 100644 --- a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt +++ b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt @@ -102,9 +102,4 @@ class WithinMethodGCTest : Harness("undertest-junit5") { ut_mirror().lineWith("selfie2()").commentOut() ut_mirror().lineWith("selfie()").uncomment() } - - @Test @Order(7) - fun deleteSelfie() { - ut_snapshot().deleteIfExists() - } } diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt index cf8b7fbe..9a574547 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt @@ -1,4 +1,4 @@ -package com.diffplug.selfie.junit5 +package undertest.junit5 // spotless:off import com.diffplug.selfie.expectSelfie import kotlin.test.Test diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_CarriageReturnTest.ss b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_CarriageReturnTest.ss new file mode 100644 index 00000000..490e872b --- /dev/null +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_CarriageReturnTest.ss @@ -0,0 +1,7 @@ +╔═ git_makes_carriage_returns_unrepresentable ═╗ +hard +to +preserve +this + +╔═ [end of file] ═╗ diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_DuplicateWriteTest.ss b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_DuplicateWriteTest.ss new file mode 100644 index 00000000..7debc001 --- /dev/null +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_DuplicateWriteTest.ss @@ -0,0 +1,3 @@ +╔═ shouldPass ═╗ +twins +╔═ [end of file] ═╗ diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_ReadWriteTest.ss b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_ReadWriteTest.ss new file mode 100644 index 00000000..4ca144c6 --- /dev/null +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_ReadWriteTest.ss @@ -0,0 +1,3 @@ +╔═ selfie ═╗ +orange +╔═ [end of file] ═╗ diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_WithinMethodGCTest.ss b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_WithinMethodGCTest.ss new file mode 100644 index 00000000..1dd2c432 --- /dev/null +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_WithinMethodGCTest.ss @@ -0,0 +1,3 @@ +╔═ selfie2/leaf ═╗ +maple +╔═ [end of file] ═╗