Skip to content

Commit

Permalink
[Tool] [Code Excerpt] allow excerpts in example readme (flutter#3758)
Browse files Browse the repository at this point in the history
Adds the ability to add code excerpts to README files within the example directory.
  • Loading branch information
tarrinneal authored Apr 19, 2023
1 parent 8d3bf84 commit 84ebba2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
4 changes: 4 additions & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.4+4

* Allows code excerpts in `example/README.md`.

## 0.13.4+3

* Moves source to flutter/packages.
Expand Down
4 changes: 4 additions & 0 deletions script/tool/lib/src/update_excerpts_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
if (!await _injectSnippets(example, targetPackage: package)) {
return PackageResult.fail(<String>['Unable to inject excerpts']);
}
if (!await _injectSnippets(example, targetPackage: example)) {
return PackageResult.fail(
<String>['Unable to inject example excerpts']);
}
} finally {
// Clean up the pubspec changes and extracted excerpts directory.
_undoPubspecChanges(example);
Expand Down
2 changes: 1 addition & 1 deletion script/tool/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_plugin_tools
description: Productivity and CI utils for flutter/packages
repository: https://github.com/flutter/packages/tree/main/script/tool
version: 0.13.4+3
version: 0.13.4+4

dependencies:
args: ^2.1.0
Expand Down
72 changes: 72 additions & 0 deletions script/tool/test/update_excerpts_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,50 @@ void main() {
]));
});

test('updates example readme when config is present', () async {
final RepositoryPackage package = createFakePlugin('a_package', packagesDir,
extraFiles: <String>[kReadmeExcerptConfigPath]);
final Directory example = getExampleDir(package);

final List<String> output =
await runCapturingPrint(runner, <String>['update-excerpts']);

expect(
processRunner.recordedCalls,
containsAll(<ProcessCall>[
ProcessCall(
'dart',
const <String>[
'run',
'build_runner',
'build',
'--config',
'excerpt',
'--output',
'excerpts',
'--delete-conflicting-outputs',
],
example.path),
ProcessCall(
'dart',
const <String>[
'run',
'code_excerpt_updater',
'--write-in-place',
'--yaml',
'--no-escape-ng-interpolation',
'README.md',
],
example.path),
]));

expect(
output,
containsAllInOrder(<Matcher>[
contains('Ran for 1 package(s)'),
]));
});

test('skips when no config is present', () async {
createFakePlugin('a_package', packagesDir);

Expand Down Expand Up @@ -231,6 +275,34 @@ void main() {
]));
});

test('fails if example injection fails', () async {
createFakePlugin('a_package', packagesDir,
extraFiles: <String>[kReadmeExcerptConfigPath]);

processRunner.mockProcessesForExecutable['dart'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(), <String>['pub', 'get']),
FakeProcessInfo(MockProcess(), <String>['run', 'build_runner']),
FakeProcessInfo(MockProcess(), <String>['run', 'code_excerpt_updater']),
FakeProcessInfo(
MockProcess(exitCode: 1), <String>['run', 'code_excerpt_updater']),
];

Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['update-excerpts'], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('The following packages had errors:'),
contains('a_package:\n'
' Unable to inject example excerpts')
]));
});

test('fails if READMEs are changed with --fail-on-change', () async {
createFakePlugin('a_plugin', packagesDir,
extraFiles: <String>[kReadmeExcerptConfigPath]);
Expand Down

0 comments on commit 84ebba2

Please sign in to comment.