-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
. r F highlight error text in command line reporter
- Loading branch information
1 parent
8b49bcf
commit 9c210f4
Showing
19 changed files
with
176 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
part of '../../approval_tests.dart'; | ||
|
||
/// A class `FileComparator` that implements the `Comparator` interface. | ||
/// This class is used to compare the content of two files. | ||
final class FileComparator implements Comparator { | ||
const FileComparator(); | ||
|
||
@override | ||
bool compare({ | ||
required String approvedPath, | ||
required String receivedPath, | ||
bool isLogError = true, | ||
}) { | ||
try { | ||
final approved = ApprovalUtils.readFile(path: approvedPath) | ||
.replaceAll('\r\n', '\n') | ||
.trim(); | ||
final received = ApprovalUtils.readFile(path: receivedPath) | ||
.replaceAll('\r\n', '\n') | ||
.trim(); | ||
|
||
// Return true if contents of both files match exactly | ||
return approved.compareTo(received) == 0; | ||
} catch (e) { | ||
if (isLogError) { | ||
ApprovalLogger.exception(e.toString()); | ||
} | ||
rethrow; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
part of '../../approval_tests.dart'; | ||
|
||
/// `Reporter` is an abstract class for reporting the comparison results. | ||
abstract interface class Reporter { | ||
void report(String approvedPath, String receivedPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
part of '../../../approval_tests.dart'; | ||
|
||
enum _LogColorStyles { red, bgRed } | ||
|
||
class _Colorize { | ||
static const String esc = "\u{1B}["; | ||
final String text; | ||
|
||
const _Colorize(this.text); | ||
|
||
_Colorize apply(_LogColorStyles style) { | ||
final appliedText = | ||
"$esc${style == _LogColorStyles.red ? '38;2;222;121;121' : '41'}m$text${esc}0m"; | ||
return _Colorize(appliedText); | ||
} | ||
|
||
@override | ||
String toString() => text; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
part of '../../../approval_tests.dart'; | ||
|
||
/// `CommandLineReporter` is a class for reporting the comparison results using the command line. | ||
class CommandLineReporter implements Reporter { | ||
const CommandLineReporter(); | ||
|
||
@override | ||
void report(String approvedPath, String receivedPath, {String? message}) { | ||
try { | ||
final String approvedContent = ApprovalUtils.readFile(path: approvedPath); | ||
final String receivedContent = ApprovalUtils.readFile(path: receivedPath); | ||
|
||
final StringBuffer buffer = StringBuffer(message ?? "Differences:\n"); | ||
final List<String> approvedLines = approvedContent.split('\n'); | ||
final List<String> receivedLines = receivedContent.split('\n'); | ||
|
||
final int maxLines = max(approvedLines.length, receivedLines.length); | ||
for (int i = 0; i < maxLines; i++) { | ||
final String approvedLine = | ||
i < approvedLines.length ? approvedLines[i] : ""; | ||
final String receivedLine = | ||
i < receivedLines.length ? receivedLines[i] : ""; | ||
|
||
if (approvedLine != receivedLine) { | ||
buffer.writeln( | ||
'${ApprovalUtils.lines(20)} Difference at line ${i + 1} ${ApprovalUtils.lines(20)}', | ||
); | ||
buffer.writeln( | ||
'Approved file: ${_highlightDifference(approvedLine, receivedLine)}', | ||
); | ||
buffer.writeln( | ||
'Received file: ${_highlightDifference(receivedLine, approvedLine)}', | ||
); | ||
} | ||
} | ||
|
||
if (buffer.isNotEmpty) { | ||
final String reportMessage = buffer.toString(); | ||
ApprovalLogger.exception(reportMessage); | ||
} | ||
} catch (e) { | ||
rethrow; | ||
} | ||
} | ||
|
||
String _highlightDifference(String line1, String line2) { | ||
final int minLength = min(line1.length, line2.length); | ||
final StringBuffer highlighted = StringBuffer(); | ||
|
||
for (int i = 0; i < minLength; i++) { | ||
if (line1[i] != line2[i]) { | ||
highlighted.write(_Colorize(line1[i]).apply(_LogColorStyles.bgRed)); | ||
} else { | ||
highlighted.write(_Colorize(line1[i]).apply(_LogColorStyles.red)); | ||
} | ||
} | ||
|
||
return highlighted.toString(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
lib/src/reporters/diff_info.dart → lib/src/reporters/diff_tool/diff_info.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.