Skip to content

Commit

Permalink
Merge pull request #19 from diffplug/feat/minor-cleanup
Browse files Browse the repository at this point in the history
Revise `inferDefaultLineEndingIsUnix`
  • Loading branch information
jknack authored Sep 10, 2023
2 parents c8e150e + 0eb1dbd commit adf443e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,4 @@ class CarriageReturnTest : Harness("undertest-junit5") {
gradleWriteSS()
ut_snapshot().assertContent(expectedContent)
}

@Test @Order(5)
fun deleteSelfie() {
ut_snapshot().deleteIfExists()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,4 @@ class ReadWriteTest : Harness("undertest-junit5") {
"""
.trimIndent())
}

@Test @Order(6)
fun deleteSelfie() {
ut_snapshot().deleteIfExists()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diffplug.selfie.junit5
package undertest.junit5
// spotless:off
import com.diffplug.selfie.expectSelfie
import kotlin.test.Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
╔═ git_makes_carriage_returns_unrepresentable ═╗
hard
to
preserve
this

╔═ [end of file] ═╗
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
╔═ shouldPass ═╗
twins
╔═ [end of file] ═╗
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
╔═ selfie ═╗
orange
╔═ [end of file] ═╗
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
╔═ selfie2/leaf ═╗
maple
╔═ [end of file] ═╗

0 comments on commit adf443e

Please sign in to comment.