Add script to check format of changed dart files (#160007)
The script is modeled after a similar script in the engine (see engine's [format.dart](https://github.com/flutter/engine/blob/main/ci/bin/format.dart)). It identifies the files that have been changed and checks if their formatting is correct. It also offers an option to correct formatting (`--fix`) and an option to check the formatting of all files in the repro (not just changed ones, `--all-files`). When we are enforcing dart format this script will be called as part of presubmit.
This commit is contained in:
committed by
GitHub
parent
2f9e2d9bc9
commit
c4dc2c9890
94
dev/tools/test/format_test.dart
Normal file
94
dev/tools/test/format_test.dart
Normal file
@@ -0,0 +1,94 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io' as io;
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:test/test.dart';
|
||||
|
||||
class FileContentPair {
|
||||
FileContentPair({required this.name, required this.original, required this.formatted});
|
||||
|
||||
final String name;
|
||||
final String original;
|
||||
final String formatted;
|
||||
}
|
||||
|
||||
final FileContentPair dartContentPair = FileContentPair(
|
||||
name: 'format_test.dart',
|
||||
original: 'enum \n\nfoo {\n entry1,\n entry2,\n}',
|
||||
formatted: 'enum foo { entry1, entry2 }\n',
|
||||
);
|
||||
|
||||
class TestFileFixture {
|
||||
TestFileFixture(this.filePairs, this.baseDir) {
|
||||
for (final FileContentPair filePair in filePairs) {
|
||||
final io.File file = io.File(path.join(baseDir.path, filePair.name));
|
||||
file.writeAsStringSync(filePair.original);
|
||||
files.add(file);
|
||||
}
|
||||
}
|
||||
|
||||
final List<io.File> files = <io.File>[];
|
||||
final io.Directory baseDir;
|
||||
final List<FileContentPair> filePairs;
|
||||
|
||||
void gitAdd() {
|
||||
final List<String> args = <String>['add'];
|
||||
for (final io.File file in files) {
|
||||
args.add(file.path);
|
||||
}
|
||||
|
||||
io.Process.runSync('git', args);
|
||||
}
|
||||
|
||||
void gitRemove() {
|
||||
final List<String> args = <String>['rm', '-f'];
|
||||
for (final io.File file in files) {
|
||||
args.add(file.path);
|
||||
}
|
||||
io.Process.runSync('git', args);
|
||||
}
|
||||
|
||||
Iterable<FileContentPair> getFileContents() {
|
||||
final List<FileContentPair> results = <FileContentPair>[];
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
final io.File file = files[i];
|
||||
final FileContentPair filePair = filePairs[i];
|
||||
final String content = file.readAsStringSync().replaceAll('\r\n', '\n');
|
||||
results.add(
|
||||
FileContentPair(name: filePair.name, original: content, formatted: filePair.formatted),
|
||||
);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
final io.File script = io.File(path.current).absolute;
|
||||
final io.Directory flutterRoot = script.parent.parent;
|
||||
final String formatterPath = path.join(
|
||||
flutterRoot.path,
|
||||
'dev',
|
||||
'tools',
|
||||
'format.${io.Platform.isWindows ? 'bat' : 'sh'}',
|
||||
);
|
||||
|
||||
test('Can fix Dart formatting errors', () {
|
||||
final TestFileFixture fixture = TestFileFixture(<FileContentPair>[
|
||||
dartContentPair,
|
||||
], flutterRoot);
|
||||
try {
|
||||
fixture.gitAdd();
|
||||
io.Process.runSync(formatterPath, <String>['--fix'], workingDirectory: flutterRoot.path);
|
||||
|
||||
final Iterable<FileContentPair> files = fixture.getFileContents();
|
||||
for (final FileContentPair pair in files) {
|
||||
expect(pair.original, equals(pair.formatted));
|
||||
}
|
||||
} finally {
|
||||
fixture.gitRemove();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user