Update package prep script to do async process execution and emit output as it happens. (#13918)
- Switches to async process execution, which now shows output as it happens instead of in chunks when the process completes - Now uses ProcessManager so that it may be mocked for the test. - Adds in the download and install of mingit on Windows. - Updated package dependencies because of added dependency on process package.
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:process/process.dart';
|
||||
|
||||
import '../prepare_package.dart';
|
||||
|
||||
@@ -17,131 +18,100 @@ void main() {
|
||||
Directory tmpDir;
|
||||
Directory flutterDir;
|
||||
File outputFile;
|
||||
MockProcessRunner runner;
|
||||
List<MockProcessResult> results;
|
||||
MockProcessManager processManager;
|
||||
List<MockProcess> results = <MockProcess>[];
|
||||
final List<List<String>> args = <List<String>>[];
|
||||
final List<Map<Symbol, dynamic>> namedArgs = <Map<Symbol, dynamic>>[];
|
||||
final String zipExe = Platform.isWindows ? '7za.exe' : 'zip';
|
||||
final String tarExe = Platform.isWindows ? 'tar.exe' : 'tar';
|
||||
final String gitExe = Platform.isWindows ? 'git.bat' : 'git';
|
||||
String flutterExe;
|
||||
|
||||
void _verifyCommand(List<dynamic> args, String expected) {
|
||||
final List<String> expectedList = expected.split(' ');
|
||||
final String executable = expectedList.removeAt(0);
|
||||
expect(args[0], executable);
|
||||
expect(args[1], orderedEquals(expectedList));
|
||||
expect(args[0], orderedEquals(expectedList));
|
||||
}
|
||||
|
||||
ProcessResult _nextResult(Invocation invocation) {
|
||||
Future<Process> _nextResult(Invocation invocation) async {
|
||||
args.add(invocation.positionalArguments);
|
||||
namedArgs.add(invocation.namedArguments);
|
||||
return results.isEmpty ? new MockProcessResult('', '', 0) : results.removeAt(0);
|
||||
final Process result = results.isEmpty ? new MockProcess('', '', 0) : results.removeAt(0);
|
||||
return new Future<Process>.value(result);
|
||||
}
|
||||
|
||||
void _answerWithResults() {
|
||||
when(
|
||||
runner.call(
|
||||
typed(captureAny),
|
||||
processManager.start(
|
||||
typed(captureAny),
|
||||
environment: typed(captureAny, named: 'environment'),
|
||||
workingDirectory: typed(captureAny, named: 'workingDirectory'),
|
||||
includeParentEnvironment: typed(captureAny, named: 'includeParentEnvironment'),
|
||||
),
|
||||
).thenAnswer(_nextResult);
|
||||
}
|
||||
|
||||
setUp(() async {
|
||||
runner = new MockProcessRunner();
|
||||
processManager = new MockProcessManager();
|
||||
args.clear();
|
||||
namedArgs.clear();
|
||||
tmpDir = await Directory.systemTemp.createTemp('flutter_');
|
||||
outputFile =
|
||||
new File(path.join(tmpDir.absolute.path, ArchiveCreator.defaultArchiveName('master')));
|
||||
flutterDir = new Directory(path.join(tmpDir.path, 'flutter'));
|
||||
flutterDir.createSync(recursive: true);
|
||||
flutterExe =
|
||||
path.join(flutterDir.path, 'bin', Platform.isWindows ? 'flutter.bat' : 'flutter');
|
||||
path.join(flutterDir.path, 'bin', 'flutter');
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await tmpDir.delete(recursive: true);
|
||||
// On Windows, the directory is locked and not able to be deleted, because it is a
|
||||
// temporary directory. So we just leave some (very small, because we're not actually
|
||||
// building archives here) trash around to be deleted at the next reboot.
|
||||
if (!Platform.isWindows) {
|
||||
await tmpDir.delete(recursive: true);
|
||||
}
|
||||
});
|
||||
|
||||
test('sets PUB_CACHE properly', () async {
|
||||
outputFile = new File(path.join(tmpDir.absolute.path, 'flutter_master.tar.xz'));
|
||||
preparer = new ArchiveCreator(tmpDir, outputFile, runner: runner);
|
||||
preparer =
|
||||
new ArchiveCreator(tmpDir, processManager: processManager, subprocessOutput: false);
|
||||
_answerWithResults();
|
||||
results = <MockProcessResult>[new MockProcessResult('deadbeef\n', '', 0)];
|
||||
preparer.checkoutFlutter('master');
|
||||
preparer.prepareArchive();
|
||||
preparer.createArchive();
|
||||
await preparer.createArchive('master', outputFile);
|
||||
expect(
|
||||
verify(runner.call(
|
||||
captureAny,
|
||||
verify(processManager.start(
|
||||
captureAny,
|
||||
workingDirectory: captureAny,
|
||||
environment: captureAny,
|
||||
includeParentEnvironment: typed(captureAny, named: 'includeParentEnvironment'),
|
||||
)).captured[2]['PUB_CACHE'],
|
||||
)).captured[1]['PUB_CACHE'],
|
||||
endsWith(path.join('flutter', '.pub-cache')),
|
||||
);
|
||||
});
|
||||
|
||||
test('calls the right commands for tar output', () async {
|
||||
outputFile = new File(path.join(tmpDir.absolute.path, 'flutter_master.tar.xz'));
|
||||
preparer = new ArchiveCreator(tmpDir, outputFile, runner: runner);
|
||||
test('calls the right commands for archive output', () async {
|
||||
preparer =
|
||||
new ArchiveCreator(tmpDir, processManager: processManager, subprocessOutput: false);
|
||||
_answerWithResults();
|
||||
results = <MockProcessResult>[new MockProcessResult('deadbeef\n', '', 0)];
|
||||
preparer.checkoutFlutter('master');
|
||||
preparer.prepareArchive();
|
||||
preparer.createArchive();
|
||||
await preparer.createArchive('master', outputFile);
|
||||
final List<String> commands = <String>[
|
||||
'$gitExe clone -b master https://chromium.googlesource.com/external/github.com/flutter/flutter',
|
||||
'$gitExe reset --hard master',
|
||||
'$gitExe remote remove origin',
|
||||
'$gitExe remote add origin https://github.com/flutter/flutter.git',
|
||||
'$flutterExe doctor',
|
||||
'$flutterExe update-packages',
|
||||
'$flutterExe precache',
|
||||
'$flutterExe ide-config',
|
||||
'$flutterExe create --template=app ${path.join(tmpDir.path, 'create_app')}',
|
||||
'$flutterExe create --template=package ${path.join(tmpDir.path, 'create_package')}',
|
||||
'$flutterExe create --template=plugin ${path.join(tmpDir.path, 'create_plugin')}',
|
||||
'$gitExe clean -f -X **/.packages',
|
||||
'$tarExe cJf ${path.join(tmpDir.path, 'flutter_master.tar.xz')} flutter',
|
||||
];
|
||||
int step = 0;
|
||||
for (String command in commands) {
|
||||
_verifyCommand(args[step++], command);
|
||||
}
|
||||
});
|
||||
|
||||
test('calls the right commands for zip output', () async {
|
||||
outputFile = new File(path.join(tmpDir.absolute.path, 'flutter_master.zip'));
|
||||
preparer = new ArchiveCreator(tmpDir, outputFile, runner: runner);
|
||||
_answerWithResults();
|
||||
results = <MockProcessResult>[new MockProcessResult('deadbeef\n', '', 0)];
|
||||
preparer.checkoutFlutter('master');
|
||||
preparer.prepareArchive();
|
||||
preparer.createArchive();
|
||||
final List<String> commands = <String>[
|
||||
'$gitExe clone -b master https://chromium.googlesource.com/external/github.com/flutter/flutter',
|
||||
'$gitExe reset --hard master',
|
||||
'$gitExe remote remove origin',
|
||||
'$gitExe remote add origin https://github.com/flutter/flutter.git',
|
||||
'$flutterExe doctor',
|
||||
'$flutterExe update-packages',
|
||||
'$flutterExe precache',
|
||||
'$flutterExe ide-config',
|
||||
'$flutterExe create --template=app ${path.join(tmpDir.path, 'create_app')}',
|
||||
'$flutterExe create --template=package ${path.join(tmpDir.path, 'create_package')}',
|
||||
'$flutterExe create --template=plugin ${path.join(tmpDir.path, 'create_plugin')}',
|
||||
'$gitExe clean -f -X **/.packages',
|
||||
'git clone -b master https://chromium.googlesource.com/external/github.com/flutter/flutter',
|
||||
'git reset --hard master',
|
||||
'git remote remove origin',
|
||||
'git remote add origin https://github.com/flutter/flutter.git',
|
||||
];
|
||||
if (Platform.isWindows) {
|
||||
commands.add('$zipExe a -tzip -mx=9 ${path.join(tmpDir.path, 'flutter_master.zip')} flutter');
|
||||
} else {
|
||||
commands.add('$zipExe -r -9 -q ${path.join(tmpDir.path, 'flutter_master.zip')} flutter');
|
||||
commands.add('7za x ${path.join(tmpDir.path, 'mingit.zip')}');
|
||||
}
|
||||
commands.addAll(<String>[
|
||||
'$flutterExe doctor',
|
||||
'$flutterExe update-packages',
|
||||
'$flutterExe precache',
|
||||
'$flutterExe ide-config',
|
||||
'$flutterExe create --template=app ${path.join(tmpDir.path, 'create_app')}',
|
||||
'$flutterExe create --template=package ${path.join(tmpDir.path, 'create_package')}',
|
||||
'$flutterExe create --template=plugin ${path.join(tmpDir.path, 'create_plugin')}',
|
||||
'git clean -f -X **/.packages',
|
||||
]);
|
||||
if (Platform.isWindows) {
|
||||
commands.add('7za a -tzip -mx=9 ${outputFile.absolute.path} flutter');
|
||||
} else {
|
||||
commands.add('tar cJf ${outputFile.absolute.path} flutter');
|
||||
}
|
||||
|
||||
int step = 0;
|
||||
for (String command in commands) {
|
||||
_verifyCommand(args[step++], command);
|
||||
@@ -149,50 +119,37 @@ void main() {
|
||||
});
|
||||
|
||||
test('throws when a command errors out', () async {
|
||||
outputFile = new File(path.join(tmpDir.absolute.path, 'flutter.tar.xz'));
|
||||
preparer = new ArchiveCreator(
|
||||
tmpDir,
|
||||
outputFile,
|
||||
runner: runner,
|
||||
);
|
||||
preparer =
|
||||
new ArchiveCreator(tmpDir, processManager: processManager, subprocessOutput: false);
|
||||
|
||||
results = <MockProcessResult>[
|
||||
new MockProcessResult('', '', 0),
|
||||
new MockProcessResult('OMG! OMG! an ERROR!\n', '', -1)
|
||||
results = <MockProcess>[
|
||||
new MockProcess('', '', 0),
|
||||
new MockProcess('', "Don't panic.\n", -1)
|
||||
];
|
||||
_answerWithResults();
|
||||
expect(() => preparer.checkoutFlutter('master'),
|
||||
expect(expectAsync2<Null, String, File>(preparer.createArchive)('master', new File('foo')),
|
||||
throwsA(const isInstanceOf<ProcessFailedException>()));
|
||||
expect(args.length, 2);
|
||||
_verifyCommand(args[0],
|
||||
'$gitExe clone -b master https://chromium.googlesource.com/external/github.com/flutter/flutter');
|
||||
_verifyCommand(args[1], '$gitExe reset --hard master');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcessRunner extends Mock implements Function {
|
||||
ProcessResult call(
|
||||
String executable,
|
||||
List<String> arguments, {
|
||||
String workingDirectory,
|
||||
Map<String, String> environment,
|
||||
bool includeParentEnvironment,
|
||||
bool runInShell,
|
||||
Encoding stdoutEncoding,
|
||||
Encoding stderrEncoding,
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcessResult extends Mock implements ProcessResult {
|
||||
MockProcessResult(this.stdout, [this.stderr = '', this.exitCode = 0]);
|
||||
|
||||
@override
|
||||
dynamic stdout = '';
|
||||
|
||||
@override
|
||||
dynamic stderr;
|
||||
|
||||
@override
|
||||
int exitCode;
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
|
||||
class MockProcess extends Mock implements Process {
|
||||
MockProcess(this._stdout, [this._stderr, this._exitCode]);
|
||||
|
||||
String _stdout;
|
||||
String _stderr;
|
||||
int _exitCode;
|
||||
|
||||
@override
|
||||
Stream<List<int>> get stdout =>
|
||||
new Stream<List<int>>.fromIterable(<List<int>>[_stdout.codeUnits]);
|
||||
|
||||
@override
|
||||
Stream<List<int>> get stderr =>
|
||||
new Stream<List<int>>.fromIterable(<List<int>>[_stderr.codeUnits]);
|
||||
|
||||
@override
|
||||
Future<int> get exitCode => new Future<int>.value(_exitCode);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user