Skip to content

Commit c206a47

Browse files
authored
[tool] make testUsingContext provide a Stdio (with hasTerminal unset) override by default (flutter#151357)
While exploring flutter#107607, I noticed that flutter_tools test results change based on whether `dart test` is run from a terminal or from a process (such as a Dart program). I also ran into this while writing tests for flutter#150667. This is due to tests that rely on the global `Stdio` instance, on which the `hasTerminal` property depends on whether the tool is being invoked from a terminal. Ideally, `testUsingContext` would require any tests that depend on `globals.stdio` to define an override for `Stdio`, but this is not the case. Until a solution to this more general problem is figured out, I think we should have `testUsingContext` always provide a `Stdio` override by default.
1 parent f194cd3 commit c206a47

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

packages/flutter_tools/test/commands.shard/hermetic/run_test.dart

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ void main() {
469469
Usage: () => usage,
470470
});
471471

472-
testUsingContext('passes device target platform to usage', () async {
472+
testUsingContext('passes device target platform to analytics', () async {
473473
final RunCommand command = RunCommand();
474474
final FakeDevice mockDevice = FakeDevice(sdkNameAndVersion: 'iOS 13')
475475
..startAppSuccess = false;
@@ -485,14 +485,6 @@ void main() {
485485
'--no-hot',
486486
]), isNull);
487487

488-
expect(usage.commands, contains(
489-
TestUsageCommand('run', parameters: CustomDimensions.fromMap(<String, String>{
490-
'cd3': 'false', 'cd4': 'ios', 'cd22': 'iOS 13',
491-
'cd23': 'debug', 'cd18': 'false', 'cd15': 'swift', 'cd31': 'true',
492-
'cd57': 'usb',
493-
'cd58': 'false',
494-
})
495-
)));
496488
expect(
497489
fakeAnalytics.sentEvents,
498490
contains(
@@ -522,7 +514,7 @@ void main() {
522514
analytics.Analytics: () => fakeAnalytics,
523515
});
524516

525-
testUsingContext('correctly reports tests to usage', () async {
517+
testUsingContext('correctly reports tests to analytics', () async {
526518
fs.currentDirectory.childDirectory('test').childFile('widget_test.dart').createSync(recursive: true);
527519
fs.currentDirectory.childDirectory('ios').childFile('AppDelegate.swift').createSync(recursive: true);
528520
final RunCommand command = RunCommand();
@@ -538,14 +530,6 @@ void main() {
538530
'test/widget_test.dart',
539531
]), isNull);
540532

541-
expect(usage.commands, contains(
542-
TestUsageCommand('run', parameters: CustomDimensions.fromMap(<String, String>{
543-
'cd3': 'false', 'cd4': 'ios', 'cd22': 'iOS 13',
544-
'cd23': 'debug', 'cd18': 'false', 'cd15': 'swift', 'cd31': 'true',
545-
'cd57': 'usb',
546-
'cd58': 'true',
547-
})),
548-
));
549533
expect(
550534
fakeAnalytics.sentEvents,
551535
contains(

packages/flutter_tools/test/src/context.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void testUsingContext(
126126
TemplateRenderer: () => const MustacheTemplateRenderer(),
127127
BuildTargets: () => const BuildTargetsImpl(),
128128
Analytics: () => const NoOpAnalytics(),
129+
Stdio: () => FakeStdio(),
129130
},
130131
body: () {
131132
// To catch all errors thrown by the test, even uncaught async errors, we use a zone.

packages/flutter_tools/test/src/fakes.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class FakeStdio extends Stdio {
254254
}
255255

256256
@override
257-
bool hasTerminal = true;
257+
bool hasTerminal = false;
258258

259259
List<String> get writtenToStdout => _stdout.writes.map<String>(_stdout.encoding.decode).toList();
260260
List<String> get writtenToStderr => _stderr.writes.map<String>(_stderr.encoding.decode).toList();
@@ -281,6 +281,9 @@ class FakeStdin extends Fake implements Stdin {
281281
@override
282282
bool lineMode = true;
283283

284+
@override
285+
bool hasTerminal = false;
286+
284287
@override
285288
Stream<S> transform<S>(StreamTransformer<List<int>, S> transformer) {
286289
return controller.stream.transform(transformer);

0 commit comments

Comments
 (0)