Skip to content

Commit 3c0eccf

Browse files
Merge pull request #3353 from dotty-staging/make-vulpix-diff-readable
Make vulpix diff readable
2 parents 174444d + 42c1742 commit 3c0eccf

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

compiler/src/dotty/tools/dotc/util/DiffUtil.scala

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ object DiffUtil {
88
private final val ANSI_DEFAULT = "\u001B[0m"
99
private final val ANSI_RED = "\u001B[31m"
1010
private final val ANSI_GREEN = "\u001B[32m"
11+
private final val ANSI_EOF = "\u001B[2m"
1112

1213
private final val DELETION_COLOR = ANSI_RED
1314
private final val ADDITION_COLOR = ANSI_GREEN
1415

16+
val EOF = new String("EOF") // Unique string up to reference
17+
1518
@tailrec private def splitTokens(str: String, acc: List[String] = Nil): List[String] = {
1619
if (str == "") {
1720
acc.reverse
@@ -58,22 +61,38 @@ object DiffUtil {
5861
(fnd, exp, totalChange.toDouble / (expected.length + found.length))
5962
}
6063

61-
def mkColoredLineDiff(expected: String, actual: String): String = {
62-
val tokens = splitTokens(expected, Nil).toArray
63-
val lastTokens = splitTokens(actual, Nil).toArray
64-
65-
val diff = hirschberg(lastTokens, tokens)
64+
def mkColoredLineDiff(expected: String, actual: String, expectedSize: Int): String = {
65+
lazy val diff = {
66+
val tokens = splitTokens(expected, Nil).toArray
67+
val lastTokens = splitTokens(actual, Nil).toArray
68+
hirschberg(lastTokens, tokens)
69+
}
6670

67-
" |SOF\n" + diff.collect {
68-
case Unmodified(str) =>
69-
" |" + str
70-
case Inserted(str) =>
71-
ADDITION_COLOR + "e |" + str + ANSI_DEFAULT
72-
case Modified(old, str) =>
73-
DELETION_COLOR + "a |" + old + "\ne |" + ADDITION_COLOR + str + ANSI_DEFAULT
74-
case Deleted(str) =>
75-
DELETION_COLOR + "\na |" + str + ANSI_DEFAULT
76-
}.mkString + "\n |EOF"
71+
val expectedDiff =
72+
if (expected eq EOF) ANSI_EOF + expected + ANSI_DEFAULT
73+
else diff.collect {
74+
case Unmodified(str) => str
75+
case Inserted(str) =>
76+
ADDITION_COLOR + str + ANSI_DEFAULT
77+
case Modified(_, str) =>
78+
ADDITION_COLOR + str + ANSI_DEFAULT
79+
case Deleted(_) => ""
80+
}.mkString
81+
82+
val actualDiff =
83+
if (actual eq EOF) ANSI_EOF + actual + ANSI_DEFAULT
84+
else diff.collect {
85+
case Unmodified(str) => str
86+
case Inserted(_) => ""
87+
case Modified(str, _) =>
88+
DELETION_COLOR + str + ANSI_DEFAULT
89+
case Deleted(str) =>
90+
DELETION_COLOR + str + ANSI_DEFAULT
91+
}.mkString
92+
93+
val pad = " " * 0.max(expectedSize - expected.length)
94+
95+
expectedDiff + pad + " | " + actualDiff
7796
}
7897

7998
def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = {

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ trait ParallelTesting extends RunnerOrchestration { self =>
500500
else runMain(testSource.runClassPath) match {
501501
case Success(_) if !checkFile.isDefined || !checkFile.get.exists => // success!
502502
case Success(output) => {
503-
val outputLines = output.lines.toArray
504-
val checkLines: Array[String] = Source.fromFile(checkFile.get).getLines().toArray
503+
val outputLines = output.lines.toArray :+ DiffUtil.EOF
504+
val checkLines: Array[String] = Source.fromFile(checkFile.get).getLines().toArray :+ DiffUtil.EOF
505505
val sourceTitle = testSource.title
506506

507507
def linesMatch =
@@ -511,13 +511,14 @@ trait ParallelTesting extends RunnerOrchestration { self =>
511511

512512
if (outputLines.length != checkLines.length || !linesMatch) {
513513
// Print diff to files and summary:
514-
val diff = outputLines.zip(checkLines).map { case (act, exp) =>
515-
DiffUtil.mkColoredLineDiff(exp, act)
514+
val expectedSize = DiffUtil.EOF.length max checkLines.map(_.length).max
515+
val diff = outputLines.padTo(checkLines.length, "").zip(checkLines.padTo(outputLines.length, "")).map { case (act, exp) =>
516+
DiffUtil.mkColoredLineDiff(exp, act, expectedSize)
516517
}.mkString("\n")
517518

518519
val msg =
519520
s"""|Output from '$sourceTitle' did not match check file.
520-
|Diff ('e' is expected, 'a' is actual):
521+
|Diff (expected on the left, actual right):
521522
|""".stripMargin + diff + "\n"
522523
echo(msg)
523524
addFailureInstruction(msg)

0 commit comments

Comments
 (0)