From f8058d7fb280499de931533f9a2bbfc553a6669e Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Wed, 28 Mar 2018 15:17:29 -0700 Subject: [PATCH] Make AppContext API not be prone to Future oddities (#16034) Follow-up comments to #15984 --- packages/flutter_tools/lib/runner.dart | 2 +- .../flutter_tools/lib/src/base/context.dart | 10 +-- .../lib/src/commands/daemon.dart | 2 +- .../flutter_tools/lib/src/context_runner.dart | 4 +- .../lib/src/runner/flutter_command.dart | 2 +- .../src/runner/flutter_command_runner.dart | 2 +- .../flutter_tools/test/base/build_test.dart | 2 +- .../flutter_tools/test/base/context_test.dart | 62 +++++++++---------- packages/flutter_tools/test/src/context.dart | 4 +- 9 files changed, 45 insertions(+), 45 deletions(-) diff --git a/packages/flutter_tools/lib/runner.dart b/packages/flutter_tools/lib/runner.dart index 85934c6da4..b755ffb0a3 100644 --- a/packages/flutter_tools/lib/runner.dart +++ b/packages/flutter_tools/lib/runner.dart @@ -196,7 +196,7 @@ Future _doctorText() async { try { final BufferLogger logger = new BufferLogger(); - await context.run>( + await context.run( body: () => doctor.diagnose(verbose: true), overrides: { Logger: () => logger, diff --git a/packages/flutter_tools/lib/src/base/context.dart b/packages/flutter_tools/lib/src/base/context.dart index 459adc4806..fff6975b32 100644 --- a/packages/flutter_tools/lib/src/base/context.dart +++ b/packages/flutter_tools/lib/src/base/context.dart @@ -126,20 +126,20 @@ class AppContext { /// If [name] is specified, the child context will be assigned the given /// name. This is useful for debugging purposes and is analogous to naming a /// thread in Java. - V run({ - @required V body(), + Future run({ + @required FutureOr body(), String name, Map overrides, Map fallbacks, - }) { + }) async { final AppContext child = new AppContext._( this, name, new Map.unmodifiable(overrides ?? const {}), new Map.unmodifiable(fallbacks ?? const {}), ); - return runZoned( - body, + return await runZoned>( + () async => await body(), zoneValues: <_Key, AppContext>{_Key.key: child}, ); } diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 4cd9bb27c0..112bb1a84d 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -54,7 +54,7 @@ class DaemonCommand extends FlutterCommand { Cache.releaseLockEarly(); - return context.run>( + return context.run( body: () async { final Daemon daemon = new Daemon( stdinCommandStream, stdoutCommandResponse, diff --git a/packages/flutter_tools/lib/src/context_runner.dart b/packages/flutter_tools/lib/src/context_runner.dart index 9abaec3b62..ed3f48be1b 100644 --- a/packages/flutter_tools/lib/src/context_runner.dart +++ b/packages/flutter_tools/lib/src/context_runner.dart @@ -38,9 +38,9 @@ Future runInContext( FutureOr runner(), { Map overrides, }) async { - return await context.run>( + return await context.run( name: 'global fallbacks', - body: () async => await runner(), + body: runner, overrides: overrides, fallbacks: { AndroidSdk: AndroidSdk.locateAndroidSdk, diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index bd9ca253fa..560b2c22f5 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -225,7 +225,7 @@ abstract class FlutterCommand extends Command { Future run() { final DateTime startTime = clock.now(); - return context.run>( + return context.run( name: 'command', overrides: {FlutterCommand: () => this}, body: () async { diff --git a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart index 940309af4c..6517e86fdb 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart @@ -246,7 +246,7 @@ class FlutterCommandRunner extends CommandRunner { }); } - await context.run>( + await context.run( overrides: contextOverrides.map((Type type, dynamic value) { return new MapEntry(type, () => value); }), diff --git a/packages/flutter_tools/test/base/build_test.dart b/packages/flutter_tools/test/base/build_test.dart index 14a98d1a01..99ceae6566 100644 --- a/packages/flutter_tools/test/base/build_test.dart +++ b/packages/flutter_tools/test/base/build_test.dart @@ -433,7 +433,7 @@ void main() { depfileContent: 'output.snapshot : main.dart other.dart', ); - await context.run( + await context.run( overrides: {GenSnapshot: () => genSnapshot}, body: () async { await fs.file('main.dart').writeAsString('import "other.dart";\nvoid main() {}'); diff --git a/packages/flutter_tools/test/base/context_test.dart b/packages/flutter_tools/test/base/context_test.dart index 72dde9e42f..a58abf7d50 100644 --- a/packages/flutter_tools/test/base/context_test.dart +++ b/packages/flutter_tools/test/base/context_test.dart @@ -32,9 +32,9 @@ void main() { expect(called, isTrue); }); - test('returns child context after run', () { + test('returns child context after run', () async { final AppContext rootContext = context; - rootContext.run(name: 'child', body: () { + await rootContext.run(name: 'child', body: () { expect(context, isNot(rootContext)); expect(context.name, 'child'); called = true; @@ -42,11 +42,11 @@ void main() { expect(called, isTrue); }); - test('returns grandchild context after nested run', () { + test('returns grandchild context after nested run', () async { final AppContext rootContext = context; - rootContext.run(name: 'child', body: () { + await rootContext.run(name: 'child', body: () async { final AppContext childContext = context; - childContext.run(name: 'grandchild', body: () { + await childContext.run(name: 'grandchild', body: () { expect(context, isNot(rootContext)); expect(context, isNot(childContext)); expect(context.name, 'grandchild'); @@ -56,9 +56,9 @@ void main() { expect(called, isTrue); }); - test('scans up zone hierarchy for first context', () { + test('scans up zone hierarchy for first context', () async { final AppContext rootContext = context; - rootContext.run(name: 'child', body: () { + await rootContext.run(name: 'child', body: () { final AppContext childContext = context; runZoned(() { expect(context, isNot(rootContext)); @@ -76,7 +76,7 @@ void main() { final Completer outer = new Completer(); final Completer inner = new Completer(); String value; - context.run( + await context.run( body: () { outer.future.then((_) { value = context[String]; @@ -93,14 +93,14 @@ void main() { expect(value, 'value'); }); - test('caches generated override values', () { + test('caches generated override values', () async { int consultationCount = 0; String value; - context.run( - body: () { + await context.run( + body: () async { final StringBuffer buf = new StringBuffer(context[String]); buf.write(context[String]); - context.run(body: () { + await context.run(body: () { buf.write(context[String]); }); value = buf.toString(); @@ -116,14 +116,14 @@ void main() { expect(consultationCount, 1); }); - test('caches generated fallback values', () { + test('caches generated fallback values', () async { int consultationCount = 0; String value; - context.run( - body: () { + await context.run( + body: () async { final StringBuffer buf = new StringBuffer(context[String]); buf.write(context[String]); - context.run(body: () { + await context.run(body: () { buf.write(context[String]); }); value = buf.toString(); @@ -139,8 +139,8 @@ void main() { expect(consultationCount, 1); }); - test('returns null if generated value is null', () { - final String value = context.run( + test('returns null if generated value is null', () async { + final String value = await context.run( body: () => context[String], overrides: { String: () => null, @@ -150,7 +150,7 @@ void main() { }); test('throws if generator has dependency cycle', () async { - final Future value = context.run>( + final Future value = context.run( body: () async { return context[String]; }, @@ -172,13 +172,13 @@ void main() { group('run', () { test('returns the value returned by body', () async { - expect(context.run(body: () => 123), 123); - expect(context.run(body: () => 'value'), 'value'); + expect(await context.run(body: () => 123), 123); + expect(await context.run(body: () => 'value'), 'value'); expect(await context.run>(body: () async => 456), 456); }); - test('passes name to child context', () { - context.run(name: 'child', body: () { + test('passes name to child context', () async { + await context.run(name: 'child', body: () { expect(context.name, 'child'); }); }); @@ -190,8 +190,8 @@ void main() { called = false; }); - test('are applied after parent context is consulted', () { - final String value = context.run( + test('are applied after parent context is consulted', () async { + final String value = await context.run( body: () { return context.run( body: () { @@ -208,9 +208,9 @@ void main() { expect(value, 'child'); }); - test('are not applied if parent context supplies value', () { + test('are not applied if parent context supplies value', () async { bool childConsulted = false; - final String value = context.run( + final String value = await context.run( body: () { return context.run( body: () { @@ -234,8 +234,8 @@ void main() { expect(childConsulted, isFalse); }); - test('may depend on one another', () { - final String value = context.run( + test('may depend on one another', () async { + final String value = await context.run( body: () { return context[String]; }, @@ -249,9 +249,9 @@ void main() { }); group('overrides', () { - test('intercept consultation of parent context', () { + test('intercept consultation of parent context', () async { bool parentConsulted = false; - final String value = context.run( + final String value = await context.run( body: () { return context.run( body: () => context[String], diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index eabb5638b0..8cfb1c72e9 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -59,7 +59,7 @@ void testUsingContext(String description, dynamic testMethod(), { test(description, () async { await runInContext(() { - return context.run>( + return context.run( name: 'mocks', overrides: { Config: () => buildConfig(fs), @@ -84,7 +84,7 @@ void testUsingContext(String description, dynamic testMethod(), { return runZoned(() { try { - return context.run>( + return context.run( // Apply the overrides to the test context in the zone since their // instantiation may reference items already stored on the context. overrides: overrides,