Skip to content

Commit

Permalink
FormatTests: improve idempotency and other errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Meltzer authored and kitbellew committed Feb 18, 2024
1 parent 05b7202 commit 0d7584f
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.scalafmt
import java.io.File

import munit.FunSuite
import munit.internal.console.AnsiColors
import munit.internal.difflib.Diff

import org.scalafmt.Error.{Incomplete, SearchStateExploded}
import org.scalafmt.rewrite.FormatTokensRewrite
import org.scalafmt.sysops.FileOps
Expand Down Expand Up @@ -45,19 +48,22 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
filename = t.filename
)
debug.printTest()
val obtained = result.formatted match {
case Formatted.Failure(e)
if t.style.onTestFailure.nonEmpty &&
e.getMessage.contains(t.style.onTestFailure) =>
t.expected
case Formatted.Failure(e: Incomplete) => e.formattedCode
case Formatted.Failure(e: SearchStateExploded) =>
logger.elem(e)
e.partialOutput
case Formatted.Failure(e) => throw FormatException(e, t.original)
case Formatted.Success(code) => code
val resultEither = result.formatted.toEither
val err = t.style.onTestFailure
val obtained = resultEither match {
case Left(e) if err.nonEmpty && e.getMessage.contains(err) => t.expected
case Left(e: Incomplete) => e.formattedCode
case Left(e: SearchStateExploded) => logger.elem(e); e.partialOutput
case Left(e) => throw FormatException(e, t.original)
case Right(code) => code
}
def assertObtained(implicit loc: munit.Location) =
assertEquals(obtained, t.expected)
debugResults += saveResult(t, obtained, debug)
if (resultEither.isLeft) {
assertObtained
return
}
val debug2 = new Debug(onlyOne)
val result2 = Scalafmt.formatCode(
obtained,
Expand All @@ -66,29 +72,38 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
)
debug2.printTest()
val result2Either = result2.formatted.toEither
def getFormattedAgain(failOK: Boolean) = result2Either match {
case Left(e: ParseException) if !failOK =>
"test does not parse: " + parseException2Message(e, obtained)
result2Either match {
case Left(e: ParseException) if !onlyManual =>
assertEquals(
"test does not parse: " + parseException2Message(e, obtained),
t.expected
)
case Left(e) => throw FormatException(e, obtained)
case Right(code) => code
}
val formattedAgain = getFormattedAgain(onlyManual)
if (onlyManual)
assertEquals(formattedAgain, obtained, "Idempotency violated")
else {
assertEquals(
if (formattedAgain == obtained || result2Either.isLeft) formattedAgain
else "Idempotency violated\n" + formattedAgain,
t.expected
)
case Right(code) =>
if (onlyManual) {
assertEquals(code, obtained, "Idempotency violated")
assertObtained
} else if (code == obtained)
assertObtained
else {
val diff = new Diff(code, obtained)
if (diff.isEmpty) assertObtained
else {
val report = AnsiColors.filterAnsi(diff.createDiffOnlyReport())
val eol = if (report.last == '\n') "" else "\n"
val error = "Idempotency violated\n" + report + eol
if (error != t.expected)
failComparison(error, code, obtained)
}
}
}
if (
result2Either.isRight &&
t.style.rewrite.rules.isEmpty &&
FormatTokensRewrite.getEnabledFactories(t.style).isEmpty &&
!t.style.assumeStandardLibraryStripMargin &&
!FileOps.isMarkdown(t.filename) &&
t.style.onTestFailure.isEmpty
err.isEmpty
)
assertFormatPreservesAst(
t.filename,
Expand Down

0 comments on commit 0d7584f

Please # to comment.