Skip to content

Commit

Permalink
Fix golden tests to work on Windows when git autocrlf=true (#60)
Browse files Browse the repository at this point in the history
+ update tests to run with both \n and \r\n files so any issues/differences are picked up on all platforms.
  • Loading branch information
DanTup authored Feb 8, 2024
1 parent 272e2f8 commit 1b307d2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
70 changes: 44 additions & 26 deletions test/golden_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,34 @@ void main() {
goldenDirectory.path,
'${path.basename(testFile.path)}.golden',
));
test(path.basename(testFile.path), () {
if (!goldenFile.existsSync() && !updateGoldens) {
fail('Missing golden file: ${goldenFile.path}');
}

final content = testFile.readAsStringSync();
final spans = SpanParser.parse(grammar, content);
final actual = _buildGoldenText(content, spans);
if (updateGoldens) {
test('${path.basename(testFile.path)} (update goldens)', () {
final content = testFile.readAsStringSync();
final spans = SpanParser.parse(grammar, content);
final actual = _buildGoldenText(content, spans);

if (updateGoldens) {
goldenFile.writeAsStringSync(actual);
} else {
final expected = goldenFile.readAsStringSync();
expect(_normalize(actual), _normalize(expected));
}
});
});
}

for (final eol in ['\n', '\r\n']) {
test('${path.basename(testFile.path)} (eol=${jsonEncode(eol)})', () {
if (!goldenFile.existsSync()) {
fail('Missing golden file: ${goldenFile.path}');
}

/// Normalises newlines to the set we're comparing.
String normalize(String code) =>
code.replaceAll('\r', '').replaceAll('\n', eol);

final content = normalize(testFile.readAsStringSync());
final spans = SpanParser.parse(grammar, content);
final actual = _buildGoldenText(content, spans);

final expected = normalize(goldenFile.readAsStringSync());
expect(actual, expected);
});
}
}
});
}
Expand All @@ -60,15 +72,23 @@ String _buildGoldenText(String content, List<ScopeSpan> spans) {
final buffer = StringBuffer();
final spansByLine = groupBy(spans, (ScopeSpan s) => s.line - 1);

final lines = content.trim().split('\n');
final lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
final line = lines[i];
// We need the line length to wrap. If this isn't the last line, account for
// the \n we split by.
final newlineLength = (i == lines.length - 1) ? 0 : 1;
final lineLengthWithNewline = line.length + newlineLength;
// We only output characters for the line, excluding any newlines on the
// end.
final line = lines[i].trimRight();
// Track what the eol was (restore the newline, subtract any untrimmed
// part). This may end up as `\r\n` or `\n` depending on whether Windows
// and the git autocrlf setting.
final eol = '${lines[i]}\n'.substring(line.length);
final lineLengthWithNewline = line.length + eol.length;

// If this is the last line and it's blank, skip it.
if (i == lines.length - 1 && line.isEmpty) {
break;
}

buffer.writeln('>$line');
buffer.write('>$line$eol');
final lineSpans = spansByLine[i];
if (lineSpans != null) {
for (final span in lineSpans) {
Expand Down Expand Up @@ -103,21 +123,19 @@ String _buildGoldenText(String content, List<ScopeSpan> spans) {

// If this span just covers the trailing newline, skip it
// as it doesn't produce any useful output.
if (col == line.length) {
if (col >= line.length) {
continue;
}

buffer.write('#');
buffer.write(' ' * col);
buffer.write('^' * length);
buffer.write(' ');
buffer.writeln(span.scopes.join(' '));
buffer.write(span.scopes.join(' '));
buffer.write(eol);
}
}
}

return buffer.toString();
}

/// Normalises newlines in code for comparing.
String _normalize(String code) => code.replaceAll('\r', '');
2 changes: 1 addition & 1 deletion test/support/span_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class _MultilineMatcher extends GrammarMatcher {
}

void _skipLine(LineScanner scanner) {
scanner.scan(RegExp('.*\n'));
scanner.scan(RegExp('.*\r?\n'));
}

@override
Expand Down

0 comments on commit 1b307d2

Please sign in to comment.