Skip to content

Commit 92a5367

Browse files
[tool] Fix false positives in update-exceprts (#6950)
When determining whether or not to fail with `--fail-on-change`, only look at .md files. In some cases, running the necessary commands (e.g., `flutter pub get`) may change unrelated files, causing fales positive failures. Only changed documentation files should be flagged. Also log the specific files that were detected as changed, to aid in debugging any future false positives. Fixes flutter/flutter#111592 Fixes flutter/flutter#111590
1 parent be2e3de commit 92a5367

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

script/tool/lib/src/update_excerpts_command.dart

+13-5
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,28 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
206206
.renameSync(package.pubspecFile.path);
207207
}
208208

209-
/// Checks the git state, returning an error string unless nothing has
209+
/// Checks the git state, returning an error string if any .md files have
210210
/// changed.
211211
Future<String?> _validateRepositoryState() async {
212-
final io.ProcessResult modifiedFiles = await processRunner.run(
212+
final io.ProcessResult checkFiles = await processRunner.run(
213213
'git',
214214
<String>['ls-files', '--modified'],
215215
workingDir: packagesDir,
216216
logOnError: true,
217217
);
218-
if (modifiedFiles.exitCode != 0) {
218+
if (checkFiles.exitCode != 0) {
219219
return 'Unable to determine local file state';
220220
}
221221

222-
final String stdout = modifiedFiles.stdout as String;
223-
return stdout.trim().isEmpty ? null : 'Snippets are out of sync';
222+
final String stdout = checkFiles.stdout as String;
223+
final List<String> changedFiles = stdout.trim().split('\n');
224+
final Iterable<String> changedMDFiles =
225+
changedFiles.where((String filePath) => filePath.endsWith('.md'));
226+
if (changedMDFiles.isNotEmpty) {
227+
return 'Snippets are out of sync in the following files: '
228+
'${changedMDFiles.join(', ')}';
229+
}
230+
231+
return null;
224232
}
225233
}

script/tool/test/update_excerpts_command_test.dart

+23-2
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ void main() {
232232
]));
233233
});
234234

235-
test('fails if files are changed with --fail-on-change', () async {
235+
test('fails if READMEs are changed with --fail-on-change', () async {
236236
createFakePlugin('a_plugin', packagesDir,
237237
extraFiles: <String>[kReadmeExcerptConfigPath]);
238238

239-
const String changedFilePath = 'packages/a_plugin/linux/foo_plugin.cc';
239+
const String changedFilePath = 'packages/a_plugin/README.md';
240240
processRunner.mockProcessesForExecutable['git'] = <io.Process>[
241241
MockProcess(stdout: changedFilePath),
242242
];
@@ -253,6 +253,27 @@ void main() {
253253
output,
254254
containsAllInOrder(<Matcher>[
255255
contains('README.md is out of sync with its source excerpts'),
256+
contains('Snippets are out of sync in the following files: '
257+
'packages/a_plugin/README.md'),
258+
]));
259+
});
260+
261+
test('passes if unrelated files are changed with --fail-on-change', () async {
262+
createFakePlugin('a_plugin', packagesDir,
263+
extraFiles: <String>[kReadmeExcerptConfigPath]);
264+
265+
const String changedFilePath = 'packages/a_plugin/linux/CMakeLists.txt';
266+
processRunner.mockProcessesForExecutable['git'] = <io.Process>[
267+
MockProcess(stdout: changedFilePath),
268+
];
269+
270+
final List<String> output = await runCapturingPrint(
271+
runner, <String>['update-excerpts', '--fail-on-change']);
272+
273+
expect(
274+
output,
275+
containsAllInOrder(<Matcher>[
276+
contains('Ran for 1 package(s)'),
256277
]));
257278
});
258279

0 commit comments

Comments
 (0)