Migration: streamline behavior injection in migration_cli_test.dart.

We now store both environmentVariables and injectArtificialException
in the _MigrationCliTestBase class, and refer to them wherever they
are needed, so we reduce the amount of plumbing we need to do.

In a follow up CL I'll be adding more behavior injection using the
same technique.

Change-Id: Ib736373f7a92a46922964d3c5c59d35bf72d618a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151300
Reviewed-by: Janice Collins <jcollins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
This commit is contained in:
Paul Berry 2020-06-16 17:52:10 +00:00 committed by commit-bot@chromium.org
parent 6d4e7d6830
commit d1638508c1

View file

@ -75,21 +75,17 @@ class _ExceptionGeneratingNonNullableFix extends NonNullableFix {
}
class _MigrationCli extends MigrationCli {
/// If `true`, then an artifical exception should be generated when migration
/// encounters a reference to the `print` function.
final bool injectArtificialException;
final _MigrationCliTestBase _test;
_MigrationCli(_MigrationCliTestBase test,
{this.injectArtificialException = false,
Map<String, String> environmentVariables})
_MigrationCli(this._test)
: super(
binaryName: 'nnbd_migration',
loggerFactory: (isVerbose) => test.logger = _TestLogger(isVerbose),
loggerFactory: (isVerbose) => _test.logger = _TestLogger(isVerbose),
defaultSdkPathOverride:
test.resourceProvider.convertPath(mock_sdk.sdkRoot),
resourceProvider: test.resourceProvider,
processManager: test.processManager,
environmentVariables: environmentVariables);
_test.resourceProvider.convertPath(mock_sdk.sdkRoot),
resourceProvider: _test.resourceProvider,
processManager: _test.processManager,
environmentVariables: _test.environmentVariables);
_MigrationCliRunner decodeCommandLineArgs(ArgResults argResults,
{bool isVerbose}) {
@ -122,7 +118,7 @@ class _MigrationCliRunner extends MigrationCliRunner {
{List<String> included = const <String>[],
int preferredPort,
String summaryPath}) {
if (cli.injectArtificialException) {
if (cli._test.injectArtificialException) {
return _ExceptionGeneratingNonNullableFix(
listener, resourceProvider, getLineInfo,
included: included,
@ -146,6 +142,12 @@ class _MigrationCliRunner extends MigrationCliRunner {
}
abstract class _MigrationCliTestBase {
Map<String, String> environmentVariables = {};
/// If `true`, then an artificial exception should be generated when migration
/// encounters a reference to the `print` function.
bool injectArtificialException = false;
void set logger(_TestLogger logger);
_MockProcessManager get processManager;
@ -157,8 +159,6 @@ mixin _MigrationCliTestMethods on _MigrationCliTestBase {
@override
/*late*/ _TestLogger logger;
Map<String, String> environmentVariables = {};
final hasVerboseHelpMessage = contains('for verbose help output');
final hasUsageText = contains('Usage: nnbd_migration');
@ -576,8 +576,8 @@ linter:
test_lifecycle_exception_handling() async {
var projectContents = simpleProject(sourceText: 'main() { print(0); }');
var projectDir = await createProjectDir(projectContents);
var cli = _createCli(injectArtificialException: true);
await assertRunFailure([projectDir], cli: cli);
injectArtificialException = true;
await assertRunFailure([projectDir]);
var errorOutput = logger.stderrBuffer.toString();
expect(errorOutput, contains('Artificial exception triggered'));
expect(
@ -588,7 +588,8 @@ linter:
test_lifecycle_exception_handling_ignore() async {
var projectContents = simpleProject(sourceText: 'main() { print(0); }');
var projectDir = await createProjectDir(projectContents);
var cli = _createCli(injectArtificialException: true);
injectArtificialException = true;
var cli = _createCli();
await runWithPreviewServer(cli, ['--ignore-exceptions', projectDir],
(url) async {
var output = logger.stdoutBuffer.toString();
@ -608,8 +609,8 @@ linter:
var projectContents =
simpleProject(sourceText: 'main() { print(0); print(1); }');
var projectDir = await createProjectDir(projectContents);
var cli = _createCli(injectArtificialException: true);
await assertRunFailure([projectDir], cli: cli);
injectArtificialException = true;
await assertRunFailure([projectDir]);
var errorOutput = logger.stderrBuffer.toString();
expect(
'Artificial exception triggered'.allMatches(errorOutput), hasLength(1));
@ -622,8 +623,8 @@ linter:
var projectContents =
simpleProject(sourceText: 'main() { print(0); unresolved; }');
var projectDir = await createProjectDir(projectContents);
var cli = _createCli(injectArtificialException: true);
await assertRunFailure(['--ignore-errors', projectDir], cli: cli);
injectArtificialException = true;
await assertRunFailure(['--ignore-errors', projectDir]);
var errorOutput = logger.stderrBuffer.toString();
expect(errorOutput, contains('Artificial exception triggered'));
expect(errorOutput, contains('try to fix errors in the source code'));
@ -1594,11 +1595,9 @@ name: test
headers: {'Content-Type': 'application/json; charset=UTF-8'});
}
_MigrationCli _createCli({bool injectArtificialException = false}) {
_MigrationCli _createCli() {
mock_sdk.MockSdk(resourceProvider: resourceProvider);
return _MigrationCli(this,
injectArtificialException: injectArtificialException,
environmentVariables: environmentVariables);
return _MigrationCli(this);
}
String _getHelpText({@required bool verbose}) {