Ignore EOF newline characters and added tests to dir_contents_diff tool (flutter/engine#50805)

No issue filed, but was [discussed on discord](https://discord.com/channels/608014603317936148/608021010377080866/1209568840644567091).
This commit is contained in:
Matan Lurey 2024-02-21 11:05:26 -08:00 committed by GitHub
parent 55da4fb37d
commit 05fedd7c11
10 changed files with 118 additions and 2 deletions

View File

@ -922,6 +922,7 @@ def build_dart_host_test_list(build_dir):
os.path.join(build_dir, 'dart-sdk', 'lib', 'libraries.json'),
],
),
(os.path.join('flutter', 'tools', 'dir_contents_diff'), []),
(os.path.join('flutter', 'tools', 'engine_tool'), []),
(os.path.join('flutter', 'tools', 'githooks'), []),
(os.path.join('flutter', 'tools', 'header_guard_check'), []),

View File

@ -70,8 +70,20 @@ int dirContentsDiff(String goldenPath, String dirPath) {
final String dirListing = _generateDirListing(dirPath);
tempFile.writeAsStringSync(dirListing);
final ProcessResult diffResult = Process.runSync(
'git', <String>['diff', '-p', goldenPath, tempFile.path],
runInShell: true, stdoutEncoding: utf8);
'git',
<String>[
'diff',
// If you manually edit the golden file, many text editors will add
// trailing whitespace. This flag ignores that because honestly it's
// not a significant part of this test.
'--ignore-space-at-eol',
'-p',
goldenPath,
tempFile.path,
],
runInShell: true,
stdoutEncoding: utf8,
);
if (diffResult.exitCode != 0) {
print(
'Unexpected diff in $goldenPath, use `git apply` with the following patch.\n');

View File

@ -1,3 +1,19 @@
name: dir_contents_diff
environment:
sdk: '>=3.2.0-0 <4.0.0'
dev_dependencies:
litetest: any
path: any
dependency_overrides:
async_helper:
path: ../../../third_party/dart/pkg/async_helper
expect:
path: ../../../third_party/dart/pkg/expect
litetest:
path: ../../testing/litetest
meta:
path: ../../../third_party/dart/pkg/meta
path:
path: ../../../third_party/dart/third_party/pkg/path
smith:
path: ../../../third_party/dart/pkg/smith

View File

@ -0,0 +1,77 @@
import 'dart:io' as io;
import 'package:litetest/litetest.dart';
import 'package:path/path.dart' as p;
void main() {
// Find a path to `dir_contents_diff.dart` from the working directory.
final String pkgPath = io.File.fromUri(io.Platform.script).parent.parent.path;
final String binPath = p.join(
pkgPath,
'bin',
'dir_contents_diff.dart',
);
// As a sanity check, ensure that the file exists.
if (!io.File(binPath).existsSync()) {
io.stderr.writeln('Unable to find $binPath');
io.exitCode = 1;
return;
}
// Runs `../bin/dir_contents_diff.dart` with the given arguments.
(int, String) runSync(String goldenPath, String dirPath) {
final io.ProcessResult result = io.Process.runSync(
'dart',
<String>[binPath, goldenPath, dirPath],
);
return (result.exitCode, result.stdout ?? result.stderr);
}
test('lists files and diffs successfully', () {
final String goldenPath = p.join(pkgPath, 'test', 'file_ok.txt');
final String dirPath = p.join(pkgPath, 'test', 'fixtures');
final (int exitCode, String output) = runSync(goldenPath, dirPath);
if (exitCode != 0) {
io.stderr.writeln('Expected exit code 0, got $exitCode');
io.stderr.writeln(output);
}
expect(exitCode, 0);
});
test('lists files and diffs successfully, even with an EOF newline', () {
final String goldenPath = p.join(pkgPath, 'test', 'file_ok_eof_newline.txt');
final String dirPath = p.join(pkgPath, 'test', 'fixtures');
final (int exitCode, String output) = runSync(goldenPath, dirPath);
if (exitCode != 0) {
io.stderr.writeln('Expected exit code 0, got $exitCode');
io.stderr.writeln(output);
}
expect(exitCode, 0);
});
test('diff fails when an expected file is missing', () {
final String goldenPath = p.join(pkgPath, 'test', 'file_bad_missing.txt');
final String dirPath = p.join(pkgPath, 'test', 'fixtures');
final (int exitCode, String output) = runSync(goldenPath, dirPath);
if (exitCode == 0) {
io.stderr.writeln('Expected non-zero exit code, got $exitCode');
io.stderr.writeln(output);
}
expect(exitCode, 1);
expect(output, contains('+a.txt'));
});
test('diff fails when an unexpected file is present', () {
final String goldenPath = p.join(pkgPath, 'test', 'file_bad_unexpected.txt');
final String dirPath = p.join(pkgPath, 'test', 'fixtures');
final (int exitCode, String output) = runSync(goldenPath, dirPath);
if (exitCode == 0) {
io.stderr.writeln('Expected non-zero exit code, got $exitCode');
io.stderr.writeln(output);
}
expect(exitCode, 1);
expect(output, contains('-c.txt'));
});
}

View File

@ -0,0 +1 @@
b.txt

View File

@ -0,0 +1,3 @@
a.txt
b.txt
c.txt

View File

@ -0,0 +1,2 @@
a.txt
b.txt

View File

@ -0,0 +1,2 @@
a.txt
b.txt

View File

@ -0,0 +1 @@
Will be referenced by a test.

View File

@ -0,0 +1 @@
Will be referenced by a test.