Skip to content

Commit

Permalink
FormatTests: always test windows lineEndings, too
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 1, 2025
1 parent 2583145 commit 595ee36
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.scalafmt.config._
import org.scalafmt.internal._
import org.scalafmt.rewrite.Rewrite
import org.scalafmt.sysops.FileOps
import org.scalafmt.util.LoggerOps
import org.scalafmt.util.MarkdownParser

import scala.meta.Input
Expand Down Expand Up @@ -90,8 +91,8 @@ object Scalafmt {
val res = MarkdownParser
.transformMdoc(code)(doFormatOne(_, mdocStyle, file, range))
style.lineEndings match {
case Some(LineEndings.unix) => res.map(_.replaceAll("\r*\n", "\n"))
case Some(LineEndings.windows) => res.map(_.replaceAll("\r*\n", "\r\n"))
case Some(LineEndings.unix) => res.map(LoggerOps.lf)
case Some(LineEndings.windows) => res.map(LoggerOps.crlf)
case _ => res
}
} else doFormatOne(code, style, file, range)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,8 @@ object LoggerOps {
logger.debug(s"Total cost: ${finalState.cost}")
}

def lf(str: String): String = str.replaceAll("\r*\n", "\n")
def crlf(str: String): String = str.replaceAll("\r*\n", "\r\n")
def showCR(str: String): String = str.replace("\r", "^M")

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.scalafmt

import org.scalafmt.Error.Incomplete
import org.scalafmt.Error.SearchStateExploded
import org.scalafmt.config.LineEndings
import org.scalafmt.rewrite.FormatTokensRewrite
import org.scalafmt.sysops.FileOps
import org.scalafmt.util._
Expand Down Expand Up @@ -33,7 +34,15 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
override val tests = if (onlyManual) ManualTests.tests else UnitTests.tests

tests.sortBy(x => (x.loc.path, x.loc.line)).withFilter(testShouldRun)
.foreach(runTest(run))
.foreach { t =>
runTest(run)(t)
if (t.style.lineEndings.isEmpty) runTest(run)(t.copy(
name = s"${t.name} [WIN]",
original = crlf(t.original),
expected = crlf(t.expected),
style = t.style.withLineEndings(LineEndings.windows),
))
}

def run(t: DiffTest): Unit = {
// @note munit assertions take an implicit Location generated by macros at compile time
Expand Down Expand Up @@ -80,7 +89,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
}
var debug2Opt: Option[Debug] = None
def assertObtained(implicit loc: munit.Location) = {
assertEquals(obtained, t.expected)
assertEquals(showCR(obtained), showCR(t.expected))
assertVisits(debug, t.stateVisits, debug2Opt, t.stateVisits2)
}
debugResults += saveResult(t, obtained, debug)
Expand Down Expand Up @@ -145,7 +154,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1259874, "total explored")
assertEquals(explored, 2519748, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit 595ee36

Please sign in to comment.