Auto-format Framework (#160545)
This auto-formats all *.dart files in the repository outside of the `engine` subdirectory and enforces that these files stay formatted with a presubmit check. **Reviewers:** Please carefully review all the commits except for the one titled "formatted". The "formatted" commit was auto-generated by running `dev/tools/format.sh -a -f`. The other commits were hand-crafted to prepare the repo for the formatting change. I recommend reviewing the commits one-by-one via the "Commits" tab and avoiding Github's "Files changed" tab as it will likely slow down your browser because of the size of this PR. --------- Co-authored-by: Kate Lovett <katelovett@google.com> Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
8e0993eda8
commit
5491c8c146
@@ -18,32 +18,44 @@ void main() {
|
||||
);
|
||||
});
|
||||
test('config directory is correct', () async {
|
||||
expect(config.configDirectory.path,
|
||||
matches(RegExp(r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config')));
|
||||
expect(
|
||||
config.configDirectory.path,
|
||||
matches(RegExp(r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config')),
|
||||
);
|
||||
});
|
||||
test('skeleton directory is correct', () async {
|
||||
expect(
|
||||
config.skeletonsDirectory.path,
|
||||
matches(RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons')));
|
||||
config.skeletonsDirectory.path,
|
||||
matches(RegExp(r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons')),
|
||||
);
|
||||
});
|
||||
test('html skeleton file for sample is correct', () async {
|
||||
expect(
|
||||
config.getHtmlSkeletonFile('snippet').path,
|
||||
matches(RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons[/\\]snippet.html')));
|
||||
config.getHtmlSkeletonFile('snippet').path,
|
||||
matches(
|
||||
RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons[/\\]snippet.html',
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
test('html skeleton file for app with no dartpad is correct', () async {
|
||||
expect(
|
||||
config.getHtmlSkeletonFile('sample').path,
|
||||
matches(RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons[/\\]sample.html')));
|
||||
config.getHtmlSkeletonFile('sample').path,
|
||||
matches(
|
||||
RegExp(r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons[/\\]sample.html'),
|
||||
),
|
||||
);
|
||||
});
|
||||
test('html skeleton file for app with dartpad is correct', () async {
|
||||
expect(
|
||||
config.getHtmlSkeletonFile('dartpad').path,
|
||||
matches(RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons[/\\]dartpad-sample.html')));
|
||||
config.getHtmlSkeletonFile('dartpad').path,
|
||||
matches(
|
||||
RegExp(
|
||||
r'[/\\]flutter sdk[/\\]dev[/\\]snippets[/\\]config[/\\]skeletons[/\\]dartpad-sample.html',
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ import 'dart:io';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
class FakeProcessManager extends LocalProcessManager {
|
||||
FakeProcessManager(
|
||||
{this.stdout = '', this.stderr = '', this.exitCode = 0, this.pid = 1});
|
||||
FakeProcessManager({this.stdout = '', this.stderr = '', this.exitCode = 0, this.pid = 1});
|
||||
|
||||
int runs = 0;
|
||||
String stdout;
|
||||
|
||||
@@ -29,20 +29,18 @@ String? _getStandardStateLocation() {
|
||||
return env['ANALYZER_STATE_LOCATION_OVERRIDE'];
|
||||
}
|
||||
|
||||
final String? home =
|
||||
io.Platform.isWindows ? env['LOCALAPPDATA'] : env['HOME'];
|
||||
return home != null && io.FileSystemEntity.isDirectorySync(home)
|
||||
? join(home, _SERVER_DIR)
|
||||
: null;
|
||||
final String? home = io.Platform.isWindows ? env['LOCALAPPDATA'] : env['HOME'];
|
||||
return home != null && io.FileSystemEntity.isDirectorySync(home) ? join(home, _SERVER_DIR) : null;
|
||||
}
|
||||
|
||||
/// A `dart:io` based implementation of [ResourceProvider].
|
||||
class FileSystemResourceProvider implements ResourceProvider {
|
||||
FileSystemResourceProvider(this.filesystem, {String? stateLocation})
|
||||
: _stateLocation = stateLocation ?? _getStandardStateLocation();
|
||||
: _stateLocation = stateLocation ?? _getStandardStateLocation();
|
||||
|
||||
static final FileSystemResourceProvider instance =
|
||||
FileSystemResourceProvider(const file.LocalFileSystem());
|
||||
static final FileSystemResourceProvider instance = FileSystemResourceProvider(
|
||||
const file.LocalFileSystem(),
|
||||
);
|
||||
|
||||
/// The path to the base folder where state is stored.
|
||||
final String? _stateLocation;
|
||||
@@ -77,8 +75,7 @@ class FileSystemResourceProvider implements ResourceProvider {
|
||||
@override
|
||||
Folder? getStateLocation(String pluginId) {
|
||||
if (_stateLocation != null) {
|
||||
final file.Directory directory =
|
||||
filesystem.directory(join(_stateLocation, pluginId));
|
||||
final file.Directory directory = filesystem.directory(join(_stateLocation, pluginId));
|
||||
directory.createSync(recursive: true);
|
||||
return _PhysicalFolder(directory);
|
||||
}
|
||||
@@ -221,14 +218,15 @@ class _PhysicalFolder extends _PhysicalResource implements Folder {
|
||||
const _PhysicalFolder(io.Directory super.directory);
|
||||
|
||||
@override
|
||||
Stream<WatchEvent> get changes =>
|
||||
DirectoryWatcher(_entry.path).events.handleError((Object error) {},
|
||||
test: (dynamic error) =>
|
||||
error is io.FileSystemException &&
|
||||
// Don't suppress "Directory watcher closed," so the outer
|
||||
// listener can see the interruption & act on it.
|
||||
!error.message
|
||||
.startsWith('Directory watcher closed unexpectedly'));
|
||||
Stream<WatchEvent> get changes => DirectoryWatcher(_entry.path).events.handleError(
|
||||
(Object error) {},
|
||||
test:
|
||||
(dynamic error) =>
|
||||
error is io.FileSystemException &&
|
||||
// Don't suppress "Directory watcher closed," so the outer
|
||||
// listener can see the interruption & act on it.
|
||||
!error.message.startsWith('Directory watcher closed unexpectedly'),
|
||||
);
|
||||
|
||||
@override
|
||||
bool get isRoot {
|
||||
@@ -317,8 +315,7 @@ class _PhysicalFolder extends _PhysicalResource implements Folder {
|
||||
@override
|
||||
Folder resolveSymbolicLinksSync() {
|
||||
try {
|
||||
return _PhysicalFolder(
|
||||
io.Directory(_directory.resolveSymbolicLinksSync()));
|
||||
return _PhysicalFolder(io.Directory(_directory.resolveSymbolicLinksSync()));
|
||||
} on io.FileSystemException catch (exception) {
|
||||
throw _wrapException(exception);
|
||||
}
|
||||
@@ -416,8 +413,7 @@ abstract class _PhysicalResource implements Resource {
|
||||
shortName == r'COM2' ||
|
||||
shortName == r'COM3' ||
|
||||
shortName == r'COM4') {
|
||||
throw FileSystemException(
|
||||
path, 'Windows device drivers cannot be read.');
|
||||
throw FileSystemException(path, 'Windows device drivers cannot be read.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,10 +31,10 @@ void main() {
|
||||
setUp(() {
|
||||
// Create a new filesystem.
|
||||
memoryFileSystem = MemoryFileSystem();
|
||||
tmpDir = memoryFileSystem.systemTempDirectory
|
||||
.createTempSync('flutter_snippets_test.');
|
||||
final Directory flutterRoot =
|
||||
memoryFileSystem.directory(path.join(tmpDir.absolute.path, 'flutter'));
|
||||
tmpDir = memoryFileSystem.systemTempDirectory.createTempSync('flutter_snippets_test.');
|
||||
final Directory flutterRoot = memoryFileSystem.directory(
|
||||
path.join(tmpDir.absolute.path, 'flutter'),
|
||||
);
|
||||
FlutterInformation.instance = FakeFlutterInformation(flutterRoot);
|
||||
});
|
||||
|
||||
@@ -53,7 +53,9 @@ import 'packages:alpha/alpha.dart'; // first
|
||||
|
||||
void main() {}
|
||||
''');
|
||||
expect(result, equals('''
|
||||
expect(
|
||||
result,
|
||||
equals('''
|
||||
// Unit comment
|
||||
|
||||
// first import
|
||||
@@ -64,7 +66,8 @@ import 'packages:beta/beta.dart'; // second
|
||||
import 'packages:gamma/gamma.dart'; // third
|
||||
|
||||
void main() {}
|
||||
'''));
|
||||
'''),
|
||||
);
|
||||
});
|
||||
test('Sorting dart and packages works', () async {
|
||||
final String result = sortImports('''
|
||||
@@ -84,7 +87,9 @@ import 'dart:async';
|
||||
|
||||
void main() {}
|
||||
''');
|
||||
expect(result, equals('''
|
||||
expect(
|
||||
result,
|
||||
equals('''
|
||||
// Unit comment
|
||||
|
||||
// first dart
|
||||
@@ -98,6 +103,7 @@ import 'packages:beta/beta.dart'; // second
|
||||
import 'packages:gamma/gamma.dart'; // third
|
||||
|
||||
void main() {}
|
||||
'''));
|
||||
'''),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -62,28 +62,30 @@ void main() {
|
||||
setUp(() {
|
||||
// Create a new filesystem.
|
||||
memoryFileSystem = MemoryFileSystem();
|
||||
tmpDir = memoryFileSystem.systemTempDirectory
|
||||
.createTempSync('flutter_snippets_test.');
|
||||
flutterRoot = memoryFileSystem
|
||||
.directory(path.join(tmpDir.absolute.path, 'flutter'));
|
||||
tmpDir = memoryFileSystem.systemTempDirectory.createTempSync('flutter_snippets_test.');
|
||||
flutterRoot = memoryFileSystem.directory(path.join(tmpDir.absolute.path, 'flutter'));
|
||||
configuration = FlutterRepoSnippetConfiguration(
|
||||
flutterRoot: flutterRoot, filesystem: memoryFileSystem);
|
||||
flutterRoot: flutterRoot,
|
||||
filesystem: memoryFileSystem,
|
||||
);
|
||||
configuration.skeletonsDirectory.createSync(recursive: true);
|
||||
<String>['dartpad', 'sample', 'snippet'].forEach(writeSkeleton);
|
||||
FlutterInformation.instance = FakeFlutterInformation(flutterRoot);
|
||||
generator = SnippetGenerator(
|
||||
configuration: configuration,
|
||||
filesystem: memoryFileSystem,
|
||||
flutterRoot: configuration.skeletonsDirectory.parent);
|
||||
configuration: configuration,
|
||||
filesystem: memoryFileSystem,
|
||||
flutterRoot: configuration.skeletonsDirectory.parent,
|
||||
);
|
||||
});
|
||||
|
||||
test('parses from comments', () async {
|
||||
final File inputFile = _createSnippetSourceFile(tmpDir, memoryFileSystem);
|
||||
final Iterable<SourceElement> elements = getFileElements(inputFile,
|
||||
resourceProvider: FileSystemResourceProvider(memoryFileSystem));
|
||||
final Iterable<SourceElement> elements = getFileElements(
|
||||
inputFile,
|
||||
resourceProvider: FileSystemResourceProvider(memoryFileSystem),
|
||||
);
|
||||
expect(elements, isNotEmpty);
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
|
||||
sampleParser.parseFromComments(elements);
|
||||
sampleParser.parseAndAddAssumptions(elements, inputFile);
|
||||
expect(elements.length, equals(7));
|
||||
@@ -94,60 +96,72 @@ void main() {
|
||||
final String code = generator.generateCode(element.samples.first);
|
||||
expect(code, contains('// Description'));
|
||||
expect(
|
||||
code,
|
||||
contains(RegExp(
|
||||
'''^String elementName = '${element.elementName}';\$''',
|
||||
multiLine: true)));
|
||||
code,
|
||||
contains(
|
||||
RegExp('''^String elementName = '${element.elementName}';\$''', multiLine: true),
|
||||
),
|
||||
);
|
||||
final String html = generator.generateHtml(element.samples.first);
|
||||
expect(
|
||||
html,
|
||||
contains(RegExp(
|
||||
'''^<pre>String elementName = '${element.elementName}';.*\$''',
|
||||
multiLine: true)));
|
||||
html,
|
||||
contains(
|
||||
RegExp(
|
||||
'''^<pre>String elementName = '${element.elementName}';.*\$''',
|
||||
multiLine: true,
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(
|
||||
html,
|
||||
contains(
|
||||
'<div class="snippet-description">{@end-inject-html}Description{@inject-html}</div>\n'));
|
||||
html,
|
||||
contains(
|
||||
'<div class="snippet-description">{@end-inject-html}Description{@inject-html}</div>\n',
|
||||
),
|
||||
);
|
||||
}
|
||||
expect(sampleCount, equals(8));
|
||||
});
|
||||
test('parses dartpad samples from linked file', () async {
|
||||
final File inputFile = _createDartpadSourceFile(
|
||||
tmpDir, memoryFileSystem, flutterRoot,
|
||||
linked: true);
|
||||
final Iterable<SourceElement> elements = getFileElements(inputFile,
|
||||
resourceProvider: FileSystemResourceProvider(memoryFileSystem));
|
||||
tmpDir,
|
||||
memoryFileSystem,
|
||||
flutterRoot,
|
||||
linked: true,
|
||||
);
|
||||
final Iterable<SourceElement> elements = getFileElements(
|
||||
inputFile,
|
||||
resourceProvider: FileSystemResourceProvider(memoryFileSystem),
|
||||
);
|
||||
expect(elements, isNotEmpty);
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
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, formatOutput: false);
|
||||
final String code = generator.generateCode(element.samples.first, formatOutput: false);
|
||||
expect(code, contains('// Description'));
|
||||
expect(
|
||||
code,
|
||||
contains(RegExp('^void ${element.name}Sample\\(\\) \\{.*\$',
|
||||
multiLine: true)));
|
||||
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)));
|
||||
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 assumptions', () async {
|
||||
final File inputFile = _createSnippetSourceFile(tmpDir, memoryFileSystem);
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
final List<SourceLine> assumptions =
|
||||
sampleParser.parseAssumptions(inputFile);
|
||||
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
|
||||
final List<SourceLine> assumptions = sampleParser.parseAssumptions(inputFile);
|
||||
expect(assumptions.length, equals(1));
|
||||
expect(assumptions.first.text, equals('int integer = 3;'));
|
||||
});
|
||||
@@ -245,8 +259,11 @@ class DocumentedClass {
|
||||
}
|
||||
|
||||
File _createDartpadSourceFile(
|
||||
Directory tmpDir, FileSystem filesystem, Directory flutterRoot,
|
||||
{bool linked = false}) {
|
||||
Directory tmpDir,
|
||||
FileSystem filesystem,
|
||||
Directory flutterRoot, {
|
||||
bool linked = false,
|
||||
}) {
|
||||
final File linkedFile =
|
||||
filesystem.file(path.join(flutterRoot.absolute.path, 'linked_file.dart'))
|
||||
..createSync(recursive: true)
|
||||
@@ -262,10 +279,11 @@ void DocumentedClassSample() {
|
||||
}
|
||||
''');
|
||||
|
||||
final String source = linked
|
||||
? '''
|
||||
final String source =
|
||||
linked
|
||||
? '''
|
||||
/// ** See code in ${path.relative(linkedFile.path, from: flutterRoot.absolute.path)} **'''
|
||||
: '''
|
||||
: '''
|
||||
/// ```dart
|
||||
/// void DocumentedClassSample() {
|
||||
/// String elementName = 'DocumentedClass';
|
||||
|
||||
@@ -65,34 +65,36 @@ void main() {
|
||||
setUp(() {
|
||||
// Create a new filesystem.
|
||||
memoryFileSystem = MemoryFileSystem();
|
||||
tmpDir = memoryFileSystem.systemTempDirectory
|
||||
.createTempSync('flutter_snippets_test.');
|
||||
tmpDir = memoryFileSystem.systemTempDirectory.createTempSync('flutter_snippets_test.');
|
||||
configuration = FlutterRepoSnippetConfiguration(
|
||||
flutterRoot: memoryFileSystem
|
||||
.directory(path.join(tmpDir.absolute.path, 'flutter')),
|
||||
filesystem: memoryFileSystem);
|
||||
flutterRoot: memoryFileSystem.directory(path.join(tmpDir.absolute.path, 'flutter')),
|
||||
filesystem: memoryFileSystem,
|
||||
);
|
||||
configuration.skeletonsDirectory.createSync(recursive: true);
|
||||
<String>['dartpad', 'sample', 'snippet'].forEach(writeSkeleton);
|
||||
FlutterInformation.instance =
|
||||
FakeFlutterInformation(configuration.flutterRoot);
|
||||
FlutterInformation.instance = FakeFlutterInformation(configuration.flutterRoot);
|
||||
generator = SnippetGenerator(
|
||||
configuration: configuration,
|
||||
filesystem: memoryFileSystem,
|
||||
flutterRoot: configuration.skeletonsDirectory.parent);
|
||||
configuration: configuration,
|
||||
filesystem: memoryFileSystem,
|
||||
flutterRoot: configuration.skeletonsDirectory.parent,
|
||||
);
|
||||
});
|
||||
|
||||
test('generates samples', () async {
|
||||
final File inputFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
final File inputFile =
|
||||
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
A description of the sample.
|
||||
|
||||
On several lines.
|
||||
|
||||
** 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');
|
||||
final String examplePath = path.join(
|
||||
configuration.flutterRoot.path,
|
||||
'examples/api/widgets/foo/foo_example.0.dart',
|
||||
);
|
||||
memoryFileSystem.file(examplePath)
|
||||
..create(recursive: true)
|
||||
..writeAsStringSync('''
|
||||
@@ -102,12 +104,11 @@ On several lines.
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(title: 'foo'));
|
||||
}\n'''
|
||||
);
|
||||
final File outputFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_out.txt'));
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
}\n''');
|
||||
final File outputFile = memoryFileSystem.file(
|
||||
path.join(tmpDir.absolute.path, 'snippet_out.txt'),
|
||||
);
|
||||
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
|
||||
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
|
||||
const int sourceLine = 222;
|
||||
final SourceElement element = sampleParser.parseFromDartdocToolFile(
|
||||
@@ -119,26 +120,22 @@ void main() {
|
||||
);
|
||||
|
||||
expect(element.samples, isNotEmpty);
|
||||
element.samples.first.metadata.addAll(<String, Object?>{
|
||||
'channel': 'stable',
|
||||
});
|
||||
final String code = generator.generateCode(
|
||||
element.samples.first,
|
||||
output: outputFile,
|
||||
);
|
||||
element.samples.first.metadata.addAll(<String, Object?>{'channel': 'stable'});
|
||||
final String code = generator.generateCode(element.samples.first, output: outputFile);
|
||||
expect(code, contains("runApp(MaterialApp(title: 'foo'));"));
|
||||
final String html = generator.generateHtml(
|
||||
element.samples.first,
|
||||
);
|
||||
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'''runApp(MaterialApp(title: 'foo'));'''));
|
||||
expect(html, isNot(contains('sample_channel=stable')));
|
||||
expect(
|
||||
html,
|
||||
contains('A description of the sample.\n'
|
||||
'\n'
|
||||
'On several lines.{@inject-html}</div>'));
|
||||
html,
|
||||
contains(
|
||||
'A description of the sample.\n'
|
||||
'\n'
|
||||
'On several lines.{@inject-html}</div>',
|
||||
),
|
||||
);
|
||||
expect(html, contains('void main() {'));
|
||||
|
||||
final String outputContents = outputFile.readAsStringSync();
|
||||
@@ -146,10 +143,10 @@ void main() {
|
||||
});
|
||||
|
||||
test('generates snippets', () async {
|
||||
final File inputFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
final File inputFile =
|
||||
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
A description of the snippet.
|
||||
|
||||
On several lines.
|
||||
@@ -161,8 +158,7 @@ void main() {
|
||||
```
|
||||
''');
|
||||
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
|
||||
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
|
||||
const int sourceLine = 222;
|
||||
final SourceElement element = sampleParser.parseFromDartdocToolFile(
|
||||
@@ -173,9 +169,7 @@ void main() {
|
||||
type: 'snippet',
|
||||
);
|
||||
expect(element.samples, isNotEmpty);
|
||||
element.samples.first.metadata.addAll(<String, Object>{
|
||||
'channel': 'stable',
|
||||
});
|
||||
element.samples.first.metadata.addAll(<String, Object>{'channel': 'stable'});
|
||||
final String code = generator.generateCode(element.samples.first);
|
||||
expect(code, contains('// A description of the snippet.'));
|
||||
final String html = generator.generateHtml(element.samples.first);
|
||||
@@ -183,25 +177,30 @@ void main() {
|
||||
expect(html, contains('<div>More HTML Bits</div>'));
|
||||
expect(html, contains(r' print('The actual $name.');'));
|
||||
expect(
|
||||
html,
|
||||
contains(
|
||||
'<div class="snippet-description">{@end-inject-html}A description of the snippet.\n\n'
|
||||
'On several lines.{@inject-html}</div>\n'));
|
||||
html,
|
||||
contains(
|
||||
'<div class="snippet-description">{@end-inject-html}A description of the snippet.\n\n'
|
||||
'On several lines.{@inject-html}</div>\n',
|
||||
),
|
||||
);
|
||||
expect(html, contains('main() {'));
|
||||
});
|
||||
|
||||
test('generates dartpad samples', () async {
|
||||
final File inputFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
final File inputFile =
|
||||
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
A description of the snippet.
|
||||
|
||||
On several lines.
|
||||
|
||||
** 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');
|
||||
final String examplePath = path.join(
|
||||
configuration.flutterRoot.path,
|
||||
'examples/api/widgets/foo/foo_example.0.dart',
|
||||
);
|
||||
memoryFileSystem.file(examplePath)
|
||||
..create(recursive: true)
|
||||
..writeAsStringSync('''
|
||||
@@ -211,11 +210,9 @@ On several lines.
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(title: 'foo'));
|
||||
}\n'''
|
||||
);
|
||||
}\n''');
|
||||
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
|
||||
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
|
||||
const int sourceLine = 222;
|
||||
final SourceElement element = sampleParser.parseFromDartdocToolFile(
|
||||
@@ -226,25 +223,25 @@ void main() {
|
||||
type: 'dartpad',
|
||||
);
|
||||
expect(element.samples, isNotEmpty);
|
||||
element.samples.first.metadata.addAll(<String, Object>{
|
||||
'channel': 'stable',
|
||||
});
|
||||
element.samples.first.metadata.addAll(<String, Object>{'channel': 'stable'});
|
||||
final String code = generator.generateCode(element.samples.first);
|
||||
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>'));
|
||||
expect(
|
||||
html,
|
||||
contains(
|
||||
'<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id=MyElement.0&sample_channel=stable"></iframe>\n'));
|
||||
html,
|
||||
contains(
|
||||
'<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id=MyElement.0&sample_channel=stable"></iframe>\n',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('generates sample metadata', () async {
|
||||
final File inputFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
final File inputFile =
|
||||
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync(r'''
|
||||
A description of the snippet.
|
||||
|
||||
On several lines.
|
||||
@@ -256,13 +253,14 @@ void main() {
|
||||
```
|
||||
''');
|
||||
|
||||
final File outputFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_out.dart'));
|
||||
final File expectedMetadataFile = memoryFileSystem
|
||||
.file(path.join(tmpDir.absolute.path, 'snippet_out.json'));
|
||||
final File outputFile = memoryFileSystem.file(
|
||||
path.join(tmpDir.absolute.path, 'snippet_out.dart'),
|
||||
);
|
||||
final File expectedMetadataFile = memoryFileSystem.file(
|
||||
path.join(tmpDir.absolute.path, 'snippet_out.json'),
|
||||
);
|
||||
|
||||
final SnippetDartdocParser sampleParser =
|
||||
SnippetDartdocParser(memoryFileSystem);
|
||||
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
|
||||
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
|
||||
const int sourceLine = 222;
|
||||
final SourceElement element = sampleParser.parseFromDartdocToolFile(
|
||||
@@ -273,20 +271,16 @@ void main() {
|
||||
type: 'sample',
|
||||
);
|
||||
expect(element.samples, isNotEmpty);
|
||||
element.samples.first.metadata
|
||||
.addAll(<String, Object>{'channel': 'stable'});
|
||||
element.samples.first.metadata.addAll(<String, Object>{'channel': 'stable'});
|
||||
generator.generateCode(element.samples.first, output: outputFile);
|
||||
expect(expectedMetadataFile.existsSync(), isTrue);
|
||||
final Map<String, dynamic> json =
|
||||
jsonDecode(expectedMetadataFile.readAsStringSync())
|
||||
as Map<String, dynamic>;
|
||||
jsonDecode(expectedMetadataFile.readAsStringSync()) as Map<String, dynamic>;
|
||||
expect(json['id'], equals('MyElement.0'));
|
||||
expect(json['channel'], equals('stable'));
|
||||
expect(json['file'], equals('snippet_out.dart'));
|
||||
expect(json['description'],
|
||||
equals('A description of the snippet.\n\nOn several lines.'));
|
||||
expect(json['sourcePath'],
|
||||
equals('packages/flutter/lib/src/widgets/foo.dart'));
|
||||
expect(json['description'], equals('A description of the snippet.\n\nOn several lines.'));
|
||||
expect(json['sourcePath'], equals('packages/flutter/lib/src/widgets/foo.dart'));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -299,21 +293,20 @@ void main() {
|
||||
setUp(() {
|
||||
fakeProcessManager = FakeProcessManager();
|
||||
memoryFileSystem = MemoryFileSystem();
|
||||
tmpDir = memoryFileSystem.systemTempDirectory
|
||||
.createTempSync('flutter_snippets_test.');
|
||||
flutterRoot = memoryFileSystem
|
||||
.directory(path.join(tmpDir.absolute.path, 'flutter'))
|
||||
tmpDir = memoryFileSystem.systemTempDirectory.createTempSync('flutter_snippets_test.');
|
||||
flutterRoot = memoryFileSystem.directory(path.join(tmpDir.absolute.path, 'flutter'))
|
||||
..createSync(recursive: true);
|
||||
});
|
||||
|
||||
test('command line arguments are parsed and passed to generator', () {
|
||||
final FakePlatform platform = FakePlatform(environment: <String, String>{
|
||||
'PACKAGE_NAME': 'dart:ui',
|
||||
'LIBRARY_NAME': 'library',
|
||||
'ELEMENT_NAME': 'element',
|
||||
'FLUTTER_ROOT': flutterRoot.absolute.path,
|
||||
// The details here don't really matter other than the flutter root.
|
||||
'FLUTTER_VERSION': '''
|
||||
final FakePlatform platform = FakePlatform(
|
||||
environment: <String, String>{
|
||||
'PACKAGE_NAME': 'dart:ui',
|
||||
'LIBRARY_NAME': 'library',
|
||||
'ELEMENT_NAME': 'element',
|
||||
'FLUTTER_ROOT': flutterRoot.absolute.path,
|
||||
// The details here don't really matter other than the flutter root.
|
||||
'FLUTTER_VERSION': '''
|
||||
{
|
||||
"frameworkVersion": "2.5.0-6.0.pre.55",
|
||||
"channel": "use_snippets_pkg",
|
||||
@@ -324,7 +317,8 @@ void main() {
|
||||
"dartSdkVersion": "2.14.0 (build 2.14.0-393.0.dev)",
|
||||
"flutterRoot": "${flutterRoot.absolute.path}"
|
||||
}''',
|
||||
});
|
||||
},
|
||||
);
|
||||
final FlutterInformation flutterInformation = FlutterInformation(
|
||||
filesystem: memoryFileSystem,
|
||||
processManager: fakeProcessManager,
|
||||
@@ -341,38 +335,37 @@ void main() {
|
||||
snippets_main.platform = platform;
|
||||
snippets_main.filesystem = memoryFileSystem;
|
||||
snippets_main.processManager = fakeProcessManager;
|
||||
final File input = memoryFileSystem
|
||||
.file(tmpDir.childFile('input.snippet'))
|
||||
final File input = memoryFileSystem.file(tmpDir.childFile('input.snippet'))
|
||||
..writeAsString('/// Test file');
|
||||
snippets_main.main(
|
||||
<String>['--input=${input.absolute.path}']);
|
||||
snippets_main.main(<String>['--input=${input.absolute.path}']);
|
||||
|
||||
final Map<String, dynamic> metadata =
|
||||
mockSnippetGenerator.sample.metadata;
|
||||
final Map<String, dynamic> metadata = mockSnippetGenerator.sample.metadata;
|
||||
// Ignore the channel, because channel is really just the branch, and will be
|
||||
// different on development workstations.
|
||||
metadata.remove('channel');
|
||||
expect(
|
||||
metadata,
|
||||
equals(<String, dynamic>{
|
||||
'id': 'dart_ui.library.element',
|
||||
'element': 'element',
|
||||
'sourcePath': 'unknown.dart',
|
||||
'sourceLine': 1,
|
||||
'serial': '',
|
||||
'package': 'dart:ui',
|
||||
'library': 'library',
|
||||
}));
|
||||
metadata,
|
||||
equals(<String, dynamic>{
|
||||
'id': 'dart_ui.library.element',
|
||||
'element': 'element',
|
||||
'sourcePath': 'unknown.dart',
|
||||
'sourceLine': 1,
|
||||
'serial': '',
|
||||
'package': 'dart:ui',
|
||||
'library': 'library',
|
||||
}),
|
||||
);
|
||||
|
||||
snippets_main.main(<String>[]);
|
||||
expect(
|
||||
errorMessage,
|
||||
equals(
|
||||
'The --input option must be specified, either on the command line, or in the INPUT environment variable.'));
|
||||
errorMessage,
|
||||
equals(
|
||||
'The --input option must be specified, either on the command line, or in the INPUT environment variable.',
|
||||
),
|
||||
);
|
||||
errorMessage = '';
|
||||
|
||||
snippets_main
|
||||
.main(<String>['--input=${input.absolute.path}', '--type=snippet']);
|
||||
snippets_main.main(<String>['--input=${input.absolute.path}', '--type=snippet']);
|
||||
expect(errorMessage, equals(''));
|
||||
errorMessage = '';
|
||||
|
||||
@@ -381,16 +374,14 @@ void main() {
|
||||
snippets_main.main(<String>[
|
||||
'--input=${input.absolute.path}',
|
||||
'--type=snippet',
|
||||
'--no-format-output'
|
||||
'--no-format-output',
|
||||
]);
|
||||
expect(mockSnippetGenerator.formatOutput, equals(false));
|
||||
errorMessage = '';
|
||||
|
||||
input.deleteSync();
|
||||
snippets_main.main(
|
||||
<String>['--input=${input.absolute.path}']);
|
||||
expect(errorMessage,
|
||||
equals('The input file ${input.absolute.path} does not exist.'));
|
||||
snippets_main.main(<String>['--input=${input.absolute.path}']);
|
||||
expect(errorMessage, equals('The input file ${input.absolute.path} does not exist.'));
|
||||
errorMessage = '';
|
||||
});
|
||||
});
|
||||
|
||||
@@ -45,29 +45,23 @@ void main() {
|
||||
|
||||
test('calls out to flutter if FLUTTER_VERSION is not set', () async {
|
||||
fakeProcessManager.stdout = testVersionInfo;
|
||||
final Map<String, dynamic> info =
|
||||
flutterInformation.getFlutterInformation();
|
||||
final Map<String, dynamic> info = flutterInformation.getFlutterInformation();
|
||||
expect(fakeProcessManager.runs, equals(1));
|
||||
expect(
|
||||
info['frameworkVersion'], equals(Version.parse('2.5.0-2.0.pre.63')));
|
||||
expect(info['frameworkVersion'], equals(Version.parse('2.5.0-2.0.pre.63')));
|
||||
});
|
||||
test("doesn't call out to flutter if FLUTTER_VERSION is set", () async {
|
||||
fakePlatform.environment['FLUTTER_VERSION'] = testVersionInfo;
|
||||
final Map<String, dynamic> info =
|
||||
flutterInformation.getFlutterInformation();
|
||||
final Map<String, dynamic> info = flutterInformation.getFlutterInformation();
|
||||
expect(fakeProcessManager.runs, equals(0));
|
||||
expect(
|
||||
info['frameworkVersion'], equals(Version.parse('2.5.0-2.0.pre.63')));
|
||||
expect(info['frameworkVersion'], equals(Version.parse('2.5.0-2.0.pre.63')));
|
||||
});
|
||||
test('getFlutterRoot calls out to flutter if FLUTTER_ROOT is not set',
|
||||
() async {
|
||||
test('getFlutterRoot calls out to flutter if FLUTTER_ROOT is not set', () async {
|
||||
fakeProcessManager.stdout = testVersionInfo;
|
||||
final Directory root = flutterInformation.getFlutterRoot();
|
||||
expect(fakeProcessManager.runs, equals(1));
|
||||
expect(root.path, equals('/home/user/flutter'));
|
||||
});
|
||||
test("getFlutterRoot doesn't call out to flutter if FLUTTER_ROOT is set",
|
||||
() async {
|
||||
test("getFlutterRoot doesn't call out to flutter if FLUTTER_ROOT is set", () async {
|
||||
fakePlatform.environment['FLUTTER_ROOT'] = '/home/user/flutter';
|
||||
final Directory root = flutterInformation.getFlutterRoot();
|
||||
expect(fakeProcessManager.runs, equals(0));
|
||||
@@ -75,11 +69,9 @@ void main() {
|
||||
});
|
||||
test('parses version properly', () async {
|
||||
fakePlatform.environment['FLUTTER_VERSION'] = testVersionInfo;
|
||||
final Map<String, dynamic> info =
|
||||
flutterInformation.getFlutterInformation();
|
||||
final Map<String, dynamic> info = flutterInformation.getFlutterInformation();
|
||||
expect(info['frameworkVersion'], isNotNull);
|
||||
expect(
|
||||
info['frameworkVersion'], equals(Version.parse('2.5.0-2.0.pre.63')));
|
||||
expect(info['frameworkVersion'], equals(Version.parse('2.5.0-2.0.pre.63')));
|
||||
expect(info['dartSdkVersion'], isNotNull);
|
||||
expect(info['dartSdkVersion'], equals(Version.parse('2.14.0-360.0.dev')));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user