Fix document generation, eliminate template support from snippets tool. (#147893)
## Description This fixes the API doc generation that I broke when I moved the snippets tool into the framework. It removes the last of the template support (properly this time), and makes sure all of the tests pass. ## Related Issues - https://github.com/flutter/flutter/issues/144408 - https://github.com/flutter/flutter/issues/147609 - https://github.com/flutter/flutter/pull/147645 ## Tests - Fixed tests, including smoke test of doc generation.
This commit is contained in:
@@ -27,12 +27,6 @@ void main() {
|
||||
matches(RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons')));
|
||||
});
|
||||
test('templates directory is correct', () async {
|
||||
expect(
|
||||
config.templatesDirectory.path,
|
||||
matches(RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]templates')));
|
||||
});
|
||||
test('html skeleton file for sample is correct', () async {
|
||||
expect(
|
||||
config.getHtmlSkeletonFile('snippet').path,
|
||||
|
||||
@@ -37,7 +37,6 @@ void main() {
|
||||
late FlutterRepoSnippetConfiguration configuration;
|
||||
late SnippetGenerator generator;
|
||||
late Directory tmpDir;
|
||||
late File template;
|
||||
late Directory flutterRoot;
|
||||
|
||||
void writeSkeleton(String type) {
|
||||
@@ -69,25 +68,13 @@ void main() {
|
||||
.directory(path.join(tmpDir.absolute.path, 'flutter'));
|
||||
configuration = FlutterRepoSnippetConfiguration(
|
||||
flutterRoot: flutterRoot, filesystem: memoryFileSystem);
|
||||
configuration.templatesDirectory.createSync(recursive: true);
|
||||
configuration.skeletonsDirectory.createSync(recursive: true);
|
||||
template = memoryFileSystem.file(
|
||||
path.join(configuration.templatesDirectory.path, 'template.tmpl'));
|
||||
template.writeAsStringSync('''
|
||||
// Flutter code sample for {{element}}
|
||||
|
||||
{{description}}
|
||||
|
||||
{{code-my-preamble}}
|
||||
|
||||
{{code}}
|
||||
''');
|
||||
<String>['dartpad', 'sample', 'snippet'].forEach(writeSkeleton);
|
||||
FlutterInformation.instance = FakeFlutterInformation(flutterRoot);
|
||||
generator = SnippetGenerator(
|
||||
configuration: configuration,
|
||||
filesystem: memoryFileSystem,
|
||||
flutterRoot: configuration.templatesDirectory.parent);
|
||||
flutterRoot: configuration.skeletonsDirectory.parent);
|
||||
});
|
||||
|
||||
test('parses from comments', () async {
|
||||
@@ -124,35 +111,6 @@ void main() {
|
||||
}
|
||||
expect(sampleCount, equals(8));
|
||||
});
|
||||
test('parses dartpad samples from comments', () async {
|
||||
final File inputFile =
|
||||
_createDartpadSourceFile(tmpDir, memoryFileSystem, flutterRoot);
|
||||
final Iterable<SourceElement> elements = getFileElements(inputFile,
|
||||
resourceProvider: FileSystemResourceProvider(memoryFileSystem));
|
||||
expect(elements, isNotEmpty);
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
sampleParser.parseFromComments(elements);
|
||||
expect(elements.length, equals(1));
|
||||
int sampleCount = 0;
|
||||
for (final SourceElement element in elements) {
|
||||
expect(element.samples.length, greaterThanOrEqualTo(1));
|
||||
sampleCount += element.samples.length;
|
||||
final String code = generator.generateCode(element.samples.first);
|
||||
expect(code, contains('// Description'));
|
||||
expect(
|
||||
code,
|
||||
contains(RegExp('^void ${element.name}Sample\\(\\) \\{.*\$',
|
||||
multiLine: true)));
|
||||
final String html = generator.generateHtml(element.samples.first);
|
||||
expect(
|
||||
html,
|
||||
contains(RegExp(
|
||||
'''^<iframe class="snippet-dartpad" src="https://dartpad.dev/.*sample_id=${element.name}.0.*></iframe>.*\$''',
|
||||
multiLine: true)));
|
||||
}
|
||||
expect(sampleCount, equals(1));
|
||||
});
|
||||
test('parses dartpad samples from linked file', () async {
|
||||
final File inputFile = _createDartpadSourceFile(
|
||||
tmpDir, memoryFileSystem, flutterRoot,
|
||||
|
||||
@@ -41,7 +41,6 @@ void main() {
|
||||
late FlutterRepoSnippetConfiguration configuration;
|
||||
late SnippetGenerator generator;
|
||||
late Directory tmpDir;
|
||||
late File template;
|
||||
|
||||
void writeSkeleton(String type) {
|
||||
switch (type) {
|
||||
@@ -72,26 +71,14 @@ void main() {
|
||||
flutterRoot: memoryFileSystem
|
||||
.directory(path.join(tmpDir.absolute.path, 'flutter')),
|
||||
filesystem: memoryFileSystem);
|
||||
configuration.templatesDirectory.createSync(recursive: true);
|
||||
configuration.skeletonsDirectory.createSync(recursive: true);
|
||||
template = memoryFileSystem.file(
|
||||
path.join(configuration.templatesDirectory.path, 'template.tmpl'));
|
||||
template.writeAsStringSync('''
|
||||
// Flutter code sample for {{element}}
|
||||
|
||||
{{description}}
|
||||
|
||||
{{code-my-preamble}}
|
||||
|
||||
{{code}}
|
||||
''');
|
||||
<String>['dartpad', 'sample', 'snippet'].forEach(writeSkeleton);
|
||||
FlutterInformation.instance =
|
||||
FakeFlutterInformation(configuration.flutterRoot);
|
||||
generator = SnippetGenerator(
|
||||
configuration: configuration,
|
||||
filesystem: memoryFileSystem,
|
||||
flutterRoot: configuration.templatesDirectory.parent);
|
||||
flutterRoot: configuration.skeletonsDirectory.parent);
|
||||
});
|
||||
|
||||
test('generates samples', () async {
|
||||
@@ -99,20 +86,24 @@ void main() {
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
A description of the snippet.
|
||||
A description of the sample.
|
||||
|
||||
On several lines.
|
||||
|
||||
```my-dart_language my-preamble
|
||||
const String name = 'snippet';
|
||||
```
|
||||
|
||||
```dart
|
||||
void main() {
|
||||
print('The actual $name.');
|
||||
}
|
||||
```
|
||||
** See code in examples/api/widgets/foo/foo_example.0.dart **
|
||||
''');
|
||||
final String examplePath = path.join(configuration.flutterRoot.path, 'examples/api/widgets/foo/foo_example.0.dart');
|
||||
memoryFileSystem.file(examplePath)
|
||||
..create(recursive: true)
|
||||
..writeAsStringSync('''
|
||||
// Copyright
|
||||
|
||||
// Flutter code sample for [MyElement].
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(title: 'foo'));
|
||||
}\n'''
|
||||
);
|
||||
final File outputFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_out.txt'));
|
||||
final SnippetDartdocParser sampleParser =
|
||||
@@ -135,27 +126,23 @@ void main() {
|
||||
element.samples.first,
|
||||
output: outputFile,
|
||||
);
|
||||
expect(code, contains('// Flutter code sample for MyElement'));
|
||||
expect(code, contains("runApp(MaterialApp(title: 'foo'));"));
|
||||
final String html = generator.generateHtml(
|
||||
element.samples.first,
|
||||
);
|
||||
expect(html, contains('<div>HTML Bits</div>'));
|
||||
expect(html, contains('<div>More HTML Bits</div>'));
|
||||
expect(html, contains(r'print('The actual $name.');'));
|
||||
expect(html, contains('A description of the snippet.\n'));
|
||||
expect(html, contains(r'''runApp(MaterialApp(title: 'foo'));'''));
|
||||
expect(html, isNot(contains('sample_channel=stable')));
|
||||
expect(
|
||||
html,
|
||||
contains('A description of the snippet.\n'
|
||||
contains('A description of the sample.\n'
|
||||
'\n'
|
||||
'On several lines.{@inject-html}</div>'));
|
||||
expect(html, contains('void main() {'));
|
||||
|
||||
final String outputContents = outputFile.readAsStringSync();
|
||||
expect(outputContents, contains('// Flutter code sample for MyElement'));
|
||||
expect(outputContents, contains('A description of the snippet.'));
|
||||
expect(outputContents, contains('void main() {'));
|
||||
expect(outputContents, contains("const String name = 'snippet';"));
|
||||
});
|
||||
|
||||
test('generates snippets', () async {
|
||||
@@ -212,12 +199,20 @@ A description of the snippet.
|
||||
|
||||
On several lines.
|
||||
|
||||
```code
|
||||
void main() {
|
||||
print('The actual $name.');
|
||||
}
|
||||
```
|
||||
** See code in examples/api/widgets/foo/foo_example.0.dart **
|
||||
''');
|
||||
final String examplePath = path.join(configuration.flutterRoot.path, 'examples/api/widgets/foo/foo_example.0.dart');
|
||||
memoryFileSystem.file(examplePath)
|
||||
..create(recursive: true)
|
||||
..writeAsStringSync('''
|
||||
// Copyright
|
||||
|
||||
// Flutter code sample for [MyElement].
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(title: 'foo'));
|
||||
}\n'''
|
||||
);
|
||||
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
@@ -235,7 +230,7 @@ void main() {
|
||||
'channel': 'stable',
|
||||
});
|
||||
final String code = generator.generateCode(element.samples.first);
|
||||
expect(code, contains('// Flutter code sample for MyElement'));
|
||||
expect(code, contains("runApp(MaterialApp(title: 'foo'));"));
|
||||
final String html = generator.generateHtml(element.samples.first);
|
||||
expect(html, contains('<div>HTML Bits (DartPad-style)</div>'));
|
||||
expect(html, contains('<div>More HTML Bits</div>'));
|
||||
@@ -350,7 +345,7 @@ void main() {
|
||||
.file(tmpDir.childFile('input.snippet'))
|
||||
..writeAsString('/// Test file');
|
||||
snippets_main.main(
|
||||
<String>['--input=${input.absolute.path}', '--template=template']);
|
||||
<String>['--input=${input.absolute.path}']);
|
||||
|
||||
final Map<String, dynamic> metadata =
|
||||
mockSnippetGenerator.sample.metadata;
|
||||
@@ -393,7 +388,7 @@ void main() {
|
||||
|
||||
input.deleteSync();
|
||||
snippets_main.main(
|
||||
<String>['--input=${input.absolute.path}', '--template=template']);
|
||||
<String>['--input=${input.absolute.path}']);
|
||||
expect(errorMessage,
|
||||
equals('The input file ${input.absolute.path} does not exist.'));
|
||||
errorMessage = '';
|
||||
|
||||
Reference in New Issue
Block a user