Unverified Commit 6cf554b0 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Make UriMapper and StdoutHandler public and add test cases (#26932)

parent 496c5738
...@@ -63,8 +63,9 @@ class CompilerOutput { ...@@ -63,8 +63,9 @@ class CompilerOutput {
final int errorCount; final int errorCount;
} }
class _StdoutHandler { /// Handles stdin/stdout communication with the frontend server.
_StdoutHandler({this.consumer = printError}) { class StdoutHandler {
StdoutHandler({this.consumer = printError}) {
reset(); reset();
} }
...@@ -90,8 +91,7 @@ class _StdoutHandler { ...@@ -90,8 +91,7 @@ class _StdoutHandler {
CompilerOutput( CompilerOutput(
message.substring(boundaryKey.length + 1, spaceDelimiter), message.substring(boundaryKey.length + 1, spaceDelimiter),
int.parse(message.substring(spaceDelimiter + 1).trim()))); int.parse(message.substring(spaceDelimiter + 1).trim())));
} } else if (!_suppressCompilerMessages) {
else if (!_suppressCompilerMessages) {
if (compilerMessageReceived == false) { if (compilerMessageReceived == false) {
consumer('\nCompiler message:'); consumer('\nCompiler message:');
compilerMessageReceived = true; compilerMessageReceived = true;
...@@ -110,9 +110,9 @@ class _StdoutHandler { ...@@ -110,9 +110,9 @@ class _StdoutHandler {
} }
} }
// Converts filesystem paths to package URIs. /// Converts filesystem paths to package URIs.
class _PackageUriMapper { class PackageUriMapper {
_PackageUriMapper(String scriptPath, String packagesPath) { PackageUriMapper(String scriptPath, String packagesPath) {
final Map<String, Uri> packageMap = PackageMap(fs.path.absolute(packagesPath)).map; final Map<String, Uri> packageMap = PackageMap(fs.path.absolute(packagesPath)).map;
final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString(); final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString();
...@@ -132,7 +132,6 @@ class _PackageUriMapper { ...@@ -132,7 +132,6 @@ class _PackageUriMapper {
Uri map(String scriptPath) { Uri map(String scriptPath) {
if (_packageName == null) if (_packageName == null)
return null; return null;
final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString(); final String scriptUri = Uri.file(scriptPath, windows: platform.isWindows).toString();
if (scriptUri.startsWith(_uriPrefix)) { if (scriptUri.startsWith(_uriPrefix)) {
return Uri.parse('package:$_packageName/${scriptUri.substring(_uriPrefix.length)}'); return Uri.parse('package:$_packageName/${scriptUri.substring(_uriPrefix.length)}');
...@@ -142,7 +141,7 @@ class _PackageUriMapper { ...@@ -142,7 +141,7 @@ class _PackageUriMapper {
} }
static Uri findUri(String scriptPath, String packagesPath) { static Uri findUri(String scriptPath, String packagesPath) {
return _PackageUriMapper(scriptPath, packagesPath).map(scriptPath); return PackageUriMapper(scriptPath, packagesPath).map(scriptPath);
} }
} }
...@@ -224,7 +223,7 @@ class KernelCompiler { ...@@ -224,7 +223,7 @@ class KernelCompiler {
Uri mainUri; Uri mainUri;
if (packagesPath != null) { if (packagesPath != null) {
command.addAll(<String>['--packages', packagesPath]); command.addAll(<String>['--packages', packagesPath]);
mainUri = _PackageUriMapper.findUri(mainPath, packagesPath); mainUri = PackageUriMapper.findUri(mainPath, packagesPath);
} }
if (outputFilePath != null) { if (outputFilePath != null) {
command.addAll(<String>['--output-dill', outputFilePath]); command.addAll(<String>['--output-dill', outputFilePath]);
...@@ -253,7 +252,7 @@ class KernelCompiler { ...@@ -253,7 +252,7 @@ class KernelCompiler {
printError('Failed to start frontend server $error, $stack'); printError('Failed to start frontend server $error, $stack');
}); });
final _StdoutHandler _stdoutHandler = _StdoutHandler(); final StdoutHandler _stdoutHandler = StdoutHandler();
server.stderr server.stderr
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
...@@ -340,7 +339,7 @@ class ResidentCompiler { ...@@ -340,7 +339,7 @@ class ResidentCompiler {
_fileSystemRoots = fileSystemRoots, _fileSystemRoots = fileSystemRoots,
_fileSystemScheme = fileSystemScheme, _fileSystemScheme = fileSystemScheme,
_targetModel = targetModel, _targetModel = targetModel,
_stdoutHandler = _StdoutHandler(consumer: compilerMessageConsumer), _stdoutHandler = StdoutHandler(consumer: compilerMessageConsumer),
_controller = StreamController<_CompilationRequest>(), _controller = StreamController<_CompilationRequest>(),
_initializeFromDill = initializeFromDill, _initializeFromDill = initializeFromDill,
_unsafePackageSerialization = unsafePackageSerialization, _unsafePackageSerialization = unsafePackageSerialization,
...@@ -357,7 +356,7 @@ class ResidentCompiler { ...@@ -357,7 +356,7 @@ class ResidentCompiler {
final String _fileSystemScheme; final String _fileSystemScheme;
String _sdkRoot; String _sdkRoot;
Process _server; Process _server;
final _StdoutHandler _stdoutHandler; final StdoutHandler _stdoutHandler;
String _initializeFromDill; String _initializeFromDill;
bool _unsafePackageSerialization; bool _unsafePackageSerialization;
final List<String> _experimentalFlags; final List<String> _experimentalFlags;
...@@ -390,9 +389,9 @@ class ResidentCompiler { ...@@ -390,9 +389,9 @@ class ResidentCompiler {
// First time recompile is called we actually have to compile the app from // First time recompile is called we actually have to compile the app from
// scratch ignoring list of invalidated files. // scratch ignoring list of invalidated files.
_PackageUriMapper packageUriMapper; PackageUriMapper packageUriMapper;
if (request.packagesFilePath != null) { if (request.packagesFilePath != null) {
packageUriMapper = _PackageUriMapper(request.mainPath, request.packagesFilePath); packageUriMapper = PackageUriMapper(request.mainPath, request.packagesFilePath);
} }
if (_server == null) { if (_server == null) {
...@@ -560,11 +559,11 @@ class ResidentCompiler { ...@@ -560,11 +559,11 @@ class ResidentCompiler {
_server?.stdin?.writeln('reset'); _server?.stdin?.writeln('reset');
} }
String _mapFilename(String filename, _PackageUriMapper packageUriMapper) { String _mapFilename(String filename, PackageUriMapper packageUriMapper) {
return _doMapFilename(filename, packageUriMapper) ?? filename; return _doMapFilename(filename, packageUriMapper) ?? filename;
} }
String _mapFileUri(String fileUri, _PackageUriMapper packageUriMapper) { String _mapFileUri(String fileUri, PackageUriMapper packageUriMapper) {
String filename; String filename;
try { try {
filename = Uri.parse(fileUri).toFilePath(); filename = Uri.parse(fileUri).toFilePath();
...@@ -574,7 +573,7 @@ class ResidentCompiler { ...@@ -574,7 +573,7 @@ class ResidentCompiler {
return _doMapFilename(filename, packageUriMapper) ?? fileUri; return _doMapFilename(filename, packageUriMapper) ?? fileUri;
} }
String _doMapFilename(String filename, _PackageUriMapper packageUriMapper) { String _doMapFilename(String filename, PackageUriMapper packageUriMapper) {
if (packageUriMapper != null) { if (packageUriMapper != null) {
final Uri packageUri = packageUriMapper.map(filename); final Uri packageUri = packageUriMapper.map(filename);
if (packageUri != null) if (packageUri != null)
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/context.dart'; import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/logger.dart';
...@@ -20,6 +21,50 @@ import 'src/context.dart'; ...@@ -20,6 +21,50 @@ import 'src/context.dart';
final Generator _kNoColorTerminalPlatform = () => FakePlatform.fromPlatform(const LocalPlatform())..stdoutSupportsAnsi = false; final Generator _kNoColorTerminalPlatform = () => FakePlatform.fromPlatform(const LocalPlatform())..stdoutSupportsAnsi = false;
void main() { void main() {
group(PackageUriMapper, () {
const String packagesContents = r'''
xml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/xml-3.2.3/lib/
yaml:file:///Users/flutter_user/.pub-cache/hosted/pub.dartlang.org/yaml-2.1.15/lib/
example:file:///example/lib/
''';
final MockFileSystem mockFileSystem = MockFileSystem();
final MockFile mockFile = MockFile();
when(mockFileSystem.path).thenReturn(fs.path);
when(mockFileSystem.file(any)).thenReturn(mockFile);
when(mockFile.readAsBytesSync()).thenReturn(utf8.encode(packagesContents));
testUsingContext('Can map main.dart to correct package', () async {
final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages');
expect(packageUriMapper.map('/example/lib/main.dart').toString(), 'package:example/main.dart');
}, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem,
});
testUsingContext('Maps file from other package to null', () async {
final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages');
expect(packageUriMapper.map('/xml/lib/xml.dart'), null);
}, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem,
});
testUsingContext('Maps non-main file from same package', () async {
final PackageUriMapper packageUriMapper = PackageUriMapper('/example/lib/main.dart', '.packages');
expect(packageUriMapper.map('/example/lib/src/foo.dart').toString(), 'package:example/src/foo.dart');
}, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem,
});
});
test(StdoutHandler, () async {
final StdoutHandler stdoutHandler = StdoutHandler();
stdoutHandler.handler('result 12345');
expect(stdoutHandler.boundaryKey, '12345');
stdoutHandler.handler('12345 message 0');
final CompilerOutput output = await stdoutHandler.compilerOutput.future;
expect(output.errorCount, 0);
expect(output.outputFilename, 'message');
});
group('batch compile', () { group('batch compile', () {
ProcessManager mockProcessManager; ProcessManager mockProcessManager;
MockProcess mockFrontendServer; MockProcess mockFrontendServer;
...@@ -458,3 +503,5 @@ class MockStdIn extends Mock implements IOSink { ...@@ -458,3 +503,5 @@ class MockStdIn extends Mock implements IOSink {
_stdInWrites.writeln(o); _stdInWrites.writeln(o);
} }
} }
class MockFileSystem extends Mock implements FileSystem {}
class MockFile extends Mock implements File {}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment