doctor_test.dart 42.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9 10 11 12
// TODO(gspencergoog): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=20210723"
@Tags(<String>['no-shuffle'])

13 14
import 'dart:async';

15
import 'package:args/command_runner.dart';
16
import 'package:fake_async/fake_async.dart';
17
import 'package:file/memory.dart';
18
import 'package:flutter_tools/src/android/android_studio_validator.dart';
19
import 'package:flutter_tools/src/android/android_workflow.dart';
20
import 'package:flutter_tools/src/base/file_system.dart';
21
import 'package:flutter_tools/src/base/logger.dart';
22
import 'package:flutter_tools/src/base/terminal.dart';
23
import 'package:flutter_tools/src/base/user_messages.dart';
24
import 'package:flutter_tools/src/build_info.dart';
25 26
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/doctor.dart';
27
import 'package:flutter_tools/src/device.dart';
28
import 'package:flutter_tools/src/doctor.dart';
29
import 'package:flutter_tools/src/doctor_validator.dart';
30
import 'package:flutter_tools/src/features.dart';
31
import 'package:flutter_tools/src/globals.dart' as globals;
32
import 'package:flutter_tools/src/reporting/reporting.dart';
33
import 'package:flutter_tools/src/version.dart';
34 35
import 'package:flutter_tools/src/vscode/vscode.dart';
import 'package:flutter_tools/src/vscode/vscode_validator.dart';
36
import 'package:flutter_tools/src/web/workflow.dart';
37
import 'package:test/fake.dart';
38

39 40
import '../../src/common.dart';
import '../../src/context.dart';
41
import '../../src/fakes.dart';
42
import '../../src/test_flutter_command_runner.dart';
43 44

void main() {
45
  FakeFlutterVersion flutterVersion;
46
  BufferLogger logger;
47
  FakeProcessManager fakeProcessManager;
48 49

  setUp(() {
50
    flutterVersion = FakeFlutterVersion();
51
    logger = BufferLogger.test();
52
    fakeProcessManager = FakeProcessManager.empty();
53 54
  });

55 56 57 58 59 60 61 62 63 64
  testWithoutContext('ValidationMessage equality and hashCode includes contextUrl', () {
    const ValidationMessage messageA = ValidationMessage('ab', contextUrl: 'a');
    const ValidationMessage messageB = ValidationMessage('ab', contextUrl: 'b');

    expect(messageB, isNot(messageA));
    expect(messageB.hashCode, isNot(messageA.hashCode));
    expect(messageA, isNot(messageB));
    expect(messageA.hashCode, isNot(messageB.hashCode));
  });

65
  group('doctor', () {
66 67 68 69 70 71 72 73 74 75 76
    testUsingContext('vs code validator when both installed', () async {
      final ValidationResult result = await VsCodeValidatorTestTargets.installedWithExtension.validate();
      expect(result.type, ValidationType.installed);
      expect(result.statusInfo, 'version 1.2.3');
      expect(result.messages, hasLength(2));

      ValidationMessage message = result.messages
          .firstWhere((ValidationMessage m) => m.message.startsWith('VS Code '));
      expect(message.message, 'VS Code at ${VsCodeValidatorTestTargets.validInstall}');

      message = result.messages
77 78
          .firstWhere((ValidationMessage m) => m.message.startsWith('Flutter '));
      expect(message.message, 'Flutter extension version 4.5.6');
79
      expect(message.isError, isFalse);
80
    });
81

82 83
    testUsingContext('No IDE Validator includes expected installation messages', () async {
      final ValidationResult result = await NoIdeValidator().validate();
84
      expect(result.type, ValidationType.notAvailable);
85 86 87 88 89

      expect(
        result.messages.map((ValidationMessage vm) => vm.message),
        UserMessages().noIdeInstallationInfo,
      );
90
    });
91

92 93 94 95 96 97 98 99 100 101 102 103
    testUsingContext('vs code validator when 64bit installed', () async {
      expect(VsCodeValidatorTestTargets.installedWithExtension64bit.title, 'VS Code, 64-bit edition');
      final ValidationResult result = await VsCodeValidatorTestTargets.installedWithExtension64bit.validate();
      expect(result.type, ValidationType.installed);
      expect(result.statusInfo, 'version 1.2.3');
      expect(result.messages, hasLength(2));

      ValidationMessage message = result.messages
          .firstWhere((ValidationMessage m) => m.message.startsWith('VS Code '));
      expect(message.message, 'VS Code at ${VsCodeValidatorTestTargets.validInstall}');

      message = result.messages
104 105
          .firstWhere((ValidationMessage m) => m.message.startsWith('Flutter '));
      expect(message.message, 'Flutter extension version 4.5.6');
106
    });
107

108 109
    testUsingContext('vs code validator when extension missing', () async {
      final ValidationResult result = await VsCodeValidatorTestTargets.installedWithoutExtension.validate();
110
      expect(result.type, ValidationType.installed);
111 112 113 114 115 116 117 118
      expect(result.statusInfo, 'version 1.2.3');
      expect(result.messages, hasLength(2));

      ValidationMessage message = result.messages
          .firstWhere((ValidationMessage m) => m.message.startsWith('VS Code '));
      expect(message.message, 'VS Code at ${VsCodeValidatorTestTargets.validInstall}');

      message = result.messages
119
          .firstWhere((ValidationMessage m) => m.message.startsWith('Flutter '));
120 121 122
      expect(message.message, startsWith('Flutter extension can be installed from'));
      expect(message.contextUrl, 'https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter');
      expect(message.isError, false);
123
    });
124 125 126

    group('device validator', () {
      testWithoutContext('no devices', () async {
127
        final FakeDeviceManager deviceManager = FakeDeviceManager();
128
        final DeviceValidator deviceValidator = DeviceValidator(
129
          deviceManager: deviceManager,
130 131 132 133 134 135 136 137 138 139 140
          userMessages: UserMessages(),
        );
        final ValidationResult result = await deviceValidator.validate();
        expect(result.type, ValidationType.notAvailable);
        expect(result.messages, const <ValidationMessage>[
          ValidationMessage.hint('No devices available'),
        ]);
        expect(result.statusInfo, isNull);
      });

      testWithoutContext('diagnostic message', () async {
141 142
        final FakeDeviceManager deviceManager = FakeDeviceManager()
          ..diagnostics = <String>['Device locked'];
143 144

        final DeviceValidator deviceValidator = DeviceValidator(
145
          deviceManager: deviceManager,
146 147 148 149 150 151 152 153 154 155 156
          userMessages: UserMessages(),
        );
        final ValidationResult result = await deviceValidator.validate();
        expect(result.type, ValidationType.notAvailable);
        expect(result.messages, const <ValidationMessage>[
          ValidationMessage.hint('Device locked'),
        ]);
        expect(result.statusInfo, isNull);
      });

      testWithoutContext('diagnostic message and devices', () async {
157 158 159 160
        final FakeDevice device = FakeDevice();
        final FakeDeviceManager deviceManager = FakeDeviceManager()
          ..devices = <Device>[device]
          ..diagnostics = <String>['Device locked'];
161 162

        final DeviceValidator deviceValidator = DeviceValidator(
163
          deviceManager: deviceManager,
164 165 166 167 168
          userMessages: UserMessages(),
        );
        final ValidationResult result = await deviceValidator.validate();
        expect(result.type, ValidationType.installed);
        expect(result.messages, const <ValidationMessage>[
169
          ValidationMessage('name (mobile) • device-id • android • 1.2.3'),
170 171 172 173 174
          ValidationMessage.hint('Device locked'),
        ]);
        expect(result.statusInfo, '1 available');
      });
    });
175
  });
176

Chris Bracken's avatar
Chris Bracken committed
177
  group('doctor with overridden validators', () {
178
    testUsingContext('validate non-verbose output format for run without issues', () async {
179
      final Doctor doctor = Doctor(logger: logger);
180
      expect(await doctor.diagnose(verbose: false), isTrue);
181
      expect(logger.statusText, equals(
182 183 184 185 186 187 188 189
              'Doctor summary (to see all details, run flutter doctor -v):\n'
              '[✓] Passing Validator (with statusInfo)\n'
              '[✓] Another Passing Validator (with statusInfo)\n'
              '[✓] Providing validators is fun (with statusInfo)\n'
              '\n'
              '• No issues found!\n'
      ));
    }, overrides: <Type, Generator>{
190
      DoctorValidatorsProvider: () => FakeDoctorValidatorsProvider(),
191 192 193
    });
  });

194
  group('doctor usage params', () {
195
    TestUsage testUsage;
196 197

    setUp(() {
198
      testUsage = TestUsage();
199 200 201
    });

    testUsingContext('contains installed', () async {
202
      final Doctor doctor = Doctor(logger: logger);
203 204
      await doctor.diagnose(verbose: false);

205 206 207
      expect(testUsage.events.length, 3);
      expect(testUsage.events, contains(
        const TestUsageEvent(
208 209
          'doctor-result',
          'PassingValidator',
210 211 212
          label: 'installed',
        ),
      ));
213 214
    }, overrides: <Type, Generator>{
      DoctorValidatorsProvider: () => FakeDoctorValidatorsProvider(),
215
      Usage: () => testUsage,
216 217 218
    });

    testUsingContext('contains installed and partial', () async {
219
      await FakePassingDoctor(logger).diagnose(verbose: false);
220

221 222
      expect(testUsage.events, unorderedEquals(<TestUsageEvent>[
        const TestUsageEvent(
223 224
          'doctor-result',
          'PassingValidator',
225 226 227 228 229 230 231 232
          label: 'installed',
        ),
        const TestUsageEvent(
          'doctor-result',
          'PassingValidator',
          label: 'installed',
        ),
        const TestUsageEvent(
233 234
          'doctor-result',
          'PartialValidatorWithHintsOnly',
235 236 237
          label: 'partial',
        ),
        const TestUsageEvent(
238 239
          'doctor-result',
          'PartialValidatorWithErrors',
240 241 242
          label: 'partial',
        ),
      ]));
243
    }, overrides: <Type, Generator>{
244
      Usage: () => testUsage,
245 246 247
    });

    testUsingContext('contains installed, missing and partial', () async {
248
      await FakeDoctor(logger).diagnose(verbose: false);
249

250 251
      expect(testUsage.events, unorderedEquals(<TestUsageEvent>[
        const TestUsageEvent(
252 253
          'doctor-result',
          'PassingValidator',
254 255 256
          label: 'installed',
        ),
        const TestUsageEvent(
257 258
          'doctor-result',
          'MissingValidator',
259 260 261
          label: 'missing',
        ),
        const TestUsageEvent(
262 263
          'doctor-result',
          'NotAvailableValidator',
264 265 266
          label: 'notAvailable',
        ),
        const TestUsageEvent(
267 268
          'doctor-result',
          'PartialValidatorWithHintsOnly',
269 270 271
          label: 'partial',
        ),
        const TestUsageEvent(
272 273
          'doctor-result',
          'PartialValidatorWithErrors',
274 275 276
          label: 'partial',
        ),
      ]));
277
    }, overrides: <Type, Generator>{
278
      Usage: () => testUsage,
279
    });
280 281

    testUsingContext('events for grouped validators are properly decomposed', () async {
282
      await FakeGroupedDoctor(logger).diagnose(verbose: false);
283

284 285
      expect(testUsage.events, unorderedEquals(<TestUsageEvent>[
        const TestUsageEvent(
286 287
          'doctor-result',
          'PassingGroupedValidator',
288 289 290 291 292 293 294 295 296 297 298 299 300
          label: 'installed',
        ),
        const TestUsageEvent(
          'doctor-result',
          'PassingGroupedValidator',
          label: 'installed',
        ),
        const TestUsageEvent(
          'doctor-result',
          'PassingGroupedValidator',
          label: 'installed',
        ),
        const TestUsageEvent(
301 302
          'doctor-result',
          'MissingGroupedValidator',
303 304 305
          label: 'missing',
        ),
      ]));
306
    }, overrides: <Type, Generator>{
307
      Usage: () => testUsage,
308
    });
309 310 311 312 313 314 315 316

    testUsingContext('sending events can be skipped', () async {
      await FakePassingDoctor(logger).diagnose(verbose: false, sendEvent: false);

      expect(testUsage.events, isEmpty);
    }, overrides: <Type, Generator>{
      Usage: () => testUsage,
    });
317
  });
318

319
  group('doctor with fake validators', () {
320
    testUsingContext('validate non-verbose output format for run without issues', () async {
321 322
      expect(await FakeQuietDoctor(logger).diagnose(verbose: false), isTrue);
      expect(logger.statusText, equals(
323 324 325 326 327 328 329 330
              'Doctor summary (to see all details, run flutter doctor -v):\n'
              '[✓] Passing Validator (with statusInfo)\n'
              '[✓] Another Passing Validator (with statusInfo)\n'
              '[✓] Validators are fun (with statusInfo)\n'
              '[✓] Four score and seven validators ago (with statusInfo)\n'
              '\n'
              '• No issues found!\n'
      ));
331
    });
332

333
    testUsingContext('validate non-verbose output format for run with crash', () async {
334 335
      expect(await FakeCrashingDoctor(logger).diagnose(verbose: false), isFalse);
      expect(logger.statusText, equals(
336 337 338 339 340 341
              'Doctor summary (to see all details, run flutter doctor -v):\n'
              '[✓] Passing Validator (with statusInfo)\n'
              '[✓] Another Passing Validator (with statusInfo)\n'
              '[☠] Crashing validator (the doctor check crashed)\n'
              '    ✗ Due to an error, the doctor check did not complete. If the error message below is not helpful, '
              'please let us know about this issue at https://github.com/flutter/flutter/issues.\n'
342
              '    ✗ Bad state: fatal error\n'
343 344 345 346 347
              '[✓] Validators are fun (with statusInfo)\n'
              '[✓] Four score and seven validators ago (with statusInfo)\n'
              '\n'
              '! Doctor found issues in 1 category.\n'
      ));
348
    });
349

350
    testUsingContext('validate verbose output format contains trace for run with crash', () async {
351
      expect(await FakeCrashingDoctor(logger).diagnose(), isFalse);
352
      expect(logger.statusText, contains('#0      CrashingValidator.validate'));
353
    });
354

355 356 357 358 359 360 361 362 363 364
    testUsingContext('validate tool exit when exceeding timeout', () async {
      FakeAsync().run<void>((FakeAsync time) {
        final Doctor doctor = FakeAsyncStuckDoctor(logger);
        doctor.diagnose(verbose: false);
        time.elapse(Doctor.doctorDuration + const Duration(seconds: 1));
        time.flushMicrotasks();
      });

      expect(logger.statusText, contains('Stuck validator that never completes exceeded maximum allowed duration of '));
    });
365 366 367 368

    testUsingContext('validate non-verbose output format for run with an async crash', () async {
      final Completer<void> completer = Completer<void>();
      await FakeAsync().run((FakeAsync time) {
369
        unawaited(FakeAsyncCrashingDoctor(time, logger).diagnose(verbose: false).then((bool r) {
370 371 372 373 374 375 376
          expect(r, isFalse);
          completer.complete(null);
        }));
        time.elapse(const Duration(seconds: 1));
        time.flushMicrotasks();
        return completer.future;
      });
377
      expect(logger.statusText, equals(
378 379 380 381 382 383
              'Doctor summary (to see all details, run flutter doctor -v):\n'
              '[✓] Passing Validator (with statusInfo)\n'
              '[✓] Another Passing Validator (with statusInfo)\n'
              '[☠] Async crashing validator (the doctor check crashed)\n'
              '    ✗ Due to an error, the doctor check did not complete. If the error message below is not helpful, '
              'please let us know about this issue at https://github.com/flutter/flutter/issues.\n'
384
              '    ✗ Bad state: fatal error\n'
385 386 387 388 389
              '[✓] Validators are fun (with statusInfo)\n'
              '[✓] Four score and seven validators ago (with statusInfo)\n'
              '\n'
              '! Doctor found issues in 1 category.\n'
      ));
390
    });
391 392


393
    testUsingContext('validate non-verbose output format when only one category fails', () async {
394 395
      expect(await FakeSinglePassingDoctor(logger).diagnose(verbose: false), isTrue);
      expect(logger.statusText, equals(
396 397 398 399 400 401
              'Doctor summary (to see all details, run flutter doctor -v):\n'
              '[!] Partial Validator with only a Hint\n'
              '    ! There is a hint here\n'
              '\n'
              '! Doctor found issues in 1 category.\n'
      ));
402
    });
403 404

    testUsingContext('validate non-verbose output format for a passing run', () async {
405 406
      expect(await FakePassingDoctor(logger).diagnose(verbose: false), isTrue);
      expect(logger.statusText, equals(
407 408 409 410 411
              'Doctor summary (to see all details, run flutter doctor -v):\n'
              '[✓] Passing Validator (with statusInfo)\n'
              '[!] Partial Validator with only a Hint\n'
              '    ! There is a hint here\n'
              '[!] Partial Validator with Errors\n'
412
              '    ✗ An error message indicating partial installation\n'
413 414 415 416 417
              '    ! Maybe a hint will help the user\n'
              '[✓] Another Passing Validator (with statusInfo)\n'
              '\n'
              '! Doctor found issues in 2 categories.\n'
      ));
418
    });
419 420

    testUsingContext('validate non-verbose output format', () async {
421 422
      expect(await FakeDoctor(logger).diagnose(verbose: false), isFalse);
      expect(logger.statusText, equals(
423 424 425 426 427
              'Doctor summary (to see all details, run flutter doctor -v):\n'
              '[✓] Passing Validator (with statusInfo)\n'
              '[✗] Missing Validator\n'
              '    ✗ A useful error message\n'
              '    ! A hint message\n'
428 429 430
              '[!] Not Available Validator\n'
              '    ✗ A useful error message\n'
              '    ! A hint message\n'
431 432 433
              '[!] Partial Validator with only a Hint\n'
              '    ! There is a hint here\n'
              '[!] Partial Validator with Errors\n'
434
              '    ✗ An error message indicating partial installation\n'
435 436
              '    ! Maybe a hint will help the user\n'
              '\n'
437
              '! Doctor found issues in 4 categories.\n'
438
      ));
439
    });
440 441

    testUsingContext('validate verbose output format', () async {
442
      expect(await FakeDoctor(logger).diagnose(), isFalse);
443
      expect(logger.statusText, equals(
444 445 446 447 448 449 450 451 452
              '[✓] Passing Validator (with statusInfo)\n'
              '    • A helpful message\n'
              '    • A second, somewhat longer helpful message\n'
              '\n'
              '[✗] Missing Validator\n'
              '    ✗ A useful error message\n'
              '    • A message that is not an error\n'
              '    ! A hint message\n'
              '\n'
453 454 455 456 457
              '[!] Not Available Validator\n'
              '    ✗ A useful error message\n'
              '    • A message that is not an error\n'
              '    ! A hint message\n'
              '\n'
458 459 460 461 462
              '[!] Partial Validator with only a Hint\n'
              '    ! There is a hint here\n'
              '    • But there is no error\n'
              '\n'
              '[!] Partial Validator with Errors\n'
463
              '    ✗ An error message indicating partial installation\n'
464 465 466
              '    ! Maybe a hint will help the user\n'
              '    • An extra message with some verbose details\n'
              '\n'
467
              '! Doctor found issues in 4 categories.\n'
468
      ));
469
    });
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

    testUsingContext('validate PII can be hidden', () async {
      expect(await FakePiiDoctor(logger).diagnose(showPii: false), isTrue);
      expect(logger.statusText, equals(
        '[✓] PII Validator\n'
        '    • Does not contain PII\n'
        '\n'
        '• No issues found!\n'
      ));
      logger.clear();
      // PII shown.
      expect(await FakePiiDoctor(logger).diagnose(), isTrue);
      expect(logger.statusText, equals(
          '[✓] PII Validator\n'
              '    • Contains PII path/to/username\n'
              '\n'
              '• No issues found!\n'
      ));
    });
  });

  group('doctor diagnosis wrapper', () {
    TestUsage testUsage;
    BufferLogger logger;

    setUp(() {
      testUsage = TestUsage();
      logger = BufferLogger.test();
    });

    testUsingContext('PII separated, events only sent once', () async {
      final Doctor fakeDoctor = FakePiiDoctor(logger);
      final DoctorText doctorText = DoctorText(logger, doctor: fakeDoctor);
      const String expectedPiiText = '[✓] PII Validator\n'
          '    • Contains PII path/to/username\n'
          '\n'
          '• No issues found!\n';
      const String expectedPiiStrippedText =
          '[✓] PII Validator\n'
          '    • Does not contain PII\n'
          '\n'
          '• No issues found!\n';

      // Run each multiple times to make sure the logger buffer is being cleared,
      // and that events are only sent once.
      expect(await doctorText.text, expectedPiiText);
      expect(await doctorText.text, expectedPiiText);

      expect(await doctorText.piiStrippedText, expectedPiiStrippedText);
      expect(await doctorText.piiStrippedText, expectedPiiStrippedText);

      // Only one event sent.
      expect(testUsage.events, <TestUsageEvent>[
        const TestUsageEvent(
          'doctor-result',
          'PiiValidator',
          label: 'installed',
        ),
      ]);
    }, overrides: <Type, Generator>{
      Usage: () => testUsage,
    });

    testUsingContext('without PII has same text and PII-stripped text', () async {
      final Doctor fakeDoctor = FakePassingDoctor(logger);
      final DoctorText doctorText = DoctorText(logger, doctor: fakeDoctor);
      final String piiText = await doctorText.text;
      expect(piiText, isNotEmpty);
      expect(piiText, await doctorText.piiStrippedText);
    }, overrides: <Type, Generator>{
      Usage: () => testUsage,
    });
542
  });
543

544
  testUsingContext('validate non-verbose output wrapping', () async {
545 546 547 548 549
    final BufferLogger wrapLogger = BufferLogger.test(
      outputPreferences: OutputPreferences(wrapText: true, wrapColumn: 30),
    );
    expect(await FakeDoctor(wrapLogger).diagnose(verbose: false), isFalse);
    expect(wrapLogger.statusText, equals(
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
        'Doctor summary (to see all\n'
        'details, run flutter doctor\n'
        '-v):\n'
        '[✓] Passing Validator (with\n'
        '    statusInfo)\n'
        '[✗] Missing Validator\n'
        '    ✗ A useful error message\n'
        '    ! A hint message\n'
        '[!] Not Available Validator\n'
        '    ✗ A useful error message\n'
        '    ! A hint message\n'
        '[!] Partial Validator with\n'
        '    only a Hint\n'
        '    ! There is a hint here\n'
        '[!] Partial Validator with\n'
        '    Errors\n'
        '    ✗ An error message\n'
        '      indicating partial\n'
        '      installation\n'
        '    ! Maybe a hint will help\n'
        '      the user\n'
        '\n'
        '! Doctor found issues in 4\n'
        '  categories.\n'
    ));
  });

  testUsingContext('validate verbose output wrapping', () async {
578 579 580
    final BufferLogger wrapLogger = BufferLogger.test(
      outputPreferences: OutputPreferences(wrapText: true, wrapColumn: 30),
    );
581
    expect(await FakeDoctor(wrapLogger).diagnose(), isFalse);
582
    expect(wrapLogger.statusText, equals(
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
        '[✓] Passing Validator (with\n'
        '    statusInfo)\n'
        '    • A helpful message\n'
        '    • A second, somewhat\n'
        '      longer helpful message\n'
        '\n'
        '[✗] Missing Validator\n'
        '    ✗ A useful error message\n'
        '    • A message that is not an\n'
        '      error\n'
        '    ! A hint message\n'
        '\n'
        '[!] Not Available Validator\n'
        '    ✗ A useful error message\n'
        '    • A message that is not an\n'
        '      error\n'
        '    ! A hint message\n'
        '\n'
        '[!] Partial Validator with\n'
        '    only a Hint\n'
        '    ! There is a hint here\n'
        '    • But there is no error\n'
        '\n'
        '[!] Partial Validator with\n'
        '    Errors\n'
        '    ✗ An error message\n'
        '      indicating partial\n'
        '      installation\n'
        '    ! Maybe a hint will help\n'
        '      the user\n'
        '    • An extra message with\n'
        '      some verbose details\n'
        '\n'
        '! Doctor found issues in 4\n'
        '  categories.\n'
    ));
  });

621 622
  group('doctor with grouped validators', () {
    testUsingContext('validate diagnose combines validator output', () async {
623 624
      expect(await FakeGroupedDoctor(logger).diagnose(), isTrue);
      expect(logger.statusText, equals(
625 626 627 628 629 630 631 632 633 634
              '[✓] Category 1\n'
              '    • A helpful message\n'
              '    • A helpful message\n'
              '\n'
              '[!] Category 2\n'
              '    • A helpful message\n'
              '    ✗ A useful error message\n'
              '\n'
              '! Doctor found issues in 1 category.\n'
      ));
635
    });
636

637 638
    testUsingContext('validate merging assigns statusInfo and title', () async {
      // There are two subvalidators. Only the second contains statusInfo.
639 640
      expect(await FakeGroupedDoctorWithStatus(logger).diagnose(), isTrue);
      expect(logger.statusText, equals(
641 642 643
              '[✓] First validator title (A status message)\n'
              '    • A helpful message\n'
              '    • A different message\n'
644
              '\n'
645
              '• No issues found!\n'
646
      ));
647
    });
648 649
  });

650 651 652 653
  group('grouped validator merging results', () {
    final PassingGroupedValidator installed = PassingGroupedValidator('Category');
    final PartialGroupedValidator partial = PartialGroupedValidator('Category');
    final MissingGroupedValidator missing = MissingGroupedValidator('Category');
654 655

    testUsingContext('validate installed + installed = installed', () async {
656 657
      expect(await FakeSmallGroupDoctor(logger, installed, installed).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[✓]'));
658
    });
659 660

    testUsingContext('validate installed + partial = partial', () async {
661 662
      expect(await FakeSmallGroupDoctor(logger, installed, partial).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[!]'));
663
    });
664 665

    testUsingContext('validate installed + missing = partial', () async {
666 667
      expect(await FakeSmallGroupDoctor(logger, installed, missing).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[!]'));
668
    });
669 670

    testUsingContext('validate partial + installed = partial', () async {
671 672
      expect(await FakeSmallGroupDoctor(logger, partial, installed).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[!]'));
673
    });
674 675

    testUsingContext('validate partial + partial = partial', () async {
676 677
      expect(await FakeSmallGroupDoctor(logger, partial, partial).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[!]'));
678
    });
679 680

    testUsingContext('validate partial + missing = partial', () async {
681 682
      expect(await FakeSmallGroupDoctor(logger, partial, missing).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[!]'));
683
    });
684 685

    testUsingContext('validate missing + installed = partial', () async {
686 687
      expect(await FakeSmallGroupDoctor(logger, missing, installed).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[!]'));
688
    });
689 690

    testUsingContext('validate missing + partial = partial', () async {
691 692
      expect(await FakeSmallGroupDoctor(logger, missing, partial).diagnose(), isTrue);
      expect(logger.statusText, startsWith('[!]'));
693
    });
694 695

    testUsingContext('validate missing + missing = missing', () async {
696 697
      expect(await FakeSmallGroupDoctor(logger, missing, missing).diagnose(), isFalse);
      expect(logger.statusText, startsWith('[✗]'));
698
    });
699
  });
700 701

  testUsingContext('WebWorkflow is a part of validator workflows if enabled', () async {
702 703
    expect(DoctorValidatorsProvider.defaultInstance.workflows,
      contains(isA<WebWorkflow>()));
704 705
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
706
    FileSystem: () => MemoryFileSystem.test(),
707
    ProcessManager: () => fakeProcessManager,
708
  });
709 710 711 712 713 714 715 716 717

  testUsingContext('Fetches tags to get the right version', () async {
    Cache.disableLocking();

    final DoctorCommand doctorCommand = DoctorCommand();
    final CommandRunner<void> commandRunner = createTestCommandRunner(doctorCommand);

    await commandRunner.run(<String>['doctor']);

718
    expect(flutterVersion.didFetchTagsAndUpdate, true);
719 720 721 722
    Cache.enableLocking();
  }, overrides: <Type, Generator>{
    ProcessManager: () => FakeProcessManager.any(),
    FileSystem: () => MemoryFileSystem.test(),
723
    FlutterVersion: () => flutterVersion,
724 725
    Doctor: () => NoOpDoctor(),
  }, initializeFlutterRoot: false);
726 727 728 729 730 731 732

  testUsingContext('If android workflow is disabled, AndroidStudio validator is not included', () {
    expect(DoctorValidatorsProvider.defaultInstance.validators, isNot(contains(isA<AndroidStudioValidator>())));
    expect(DoctorValidatorsProvider.defaultInstance.validators, isNot(contains(isA<NoAndroidStudioValidator>())));
  }, overrides: <Type, Generator>{
    FeatureFlags: () => TestFeatureFlags(isAndroidEnabled: false),
  });
733 734 735 736 737 738 739 740 741 742 743 744 745
}

class NoOpDoctor implements Doctor {
  @override
  bool get canLaunchAnything => true;

  @override
  bool get canListAnything => true;

  @override
  Future<bool> checkRemoteArtifacts(String engineRevision) async => true;

  @override
746 747 748 749 750
  Future<bool> diagnose({
    bool androidLicenses = false,
    bool verbose = true,
    bool showColor = true,
    AndroidLicenseValidator androidLicenseValidator,
751 752 753
    bool showPii = true,
    List<ValidatorTask> startedValidatorTasks,
    bool sendEvent = true,
754
  }) async => true;
755 756 757 758 759

  @override
  List<ValidatorTask> startValidatorTasks() => <ValidatorTask>[];

  @override
760
  Future<void> summary() async { }
761 762 763 764 765 766

  @override
  List<DoctorValidator> get validators => <DoctorValidator>[];

  @override
  List<Workflow> get workflows => <Workflow>[];
767 768
}

769 770 771 772 773
class PassingValidator extends DoctorValidator {
  PassingValidator(String name) : super(name);

  @override
  Future<ValidationResult> validate() async {
774
    const List<ValidationMessage> messages = <ValidationMessage>[
775 776 777
      ValidationMessage('A helpful message'),
      ValidationMessage('A second, somewhat longer helpful message'),
    ];
778
    return const ValidationResult(ValidationType.installed, messages, statusInfo: 'with statusInfo');
779 780 781
  }
}

782 783 784 785 786 787 788 789 790 791 792 793
class PiiValidator extends DoctorValidator {
  PiiValidator() : super('PII Validator');

  @override
  Future<ValidationResult> validate() async {
    const List<ValidationMessage> messages = <ValidationMessage>[
      ValidationMessage('Contains PII path/to/username', piiStrippedMessage: 'Does not contain PII'),
    ];
    return const ValidationResult(ValidationType.installed, messages);
  }
}

794
class MissingValidator extends DoctorValidator {
795
  MissingValidator() : super('Missing Validator');
796 797 798

  @override
  Future<ValidationResult> validate() async {
799
    const List<ValidationMessage> messages = <ValidationMessage>[
800 801 802 803
      ValidationMessage.error('A useful error message'),
      ValidationMessage('A message that is not an error'),
      ValidationMessage.hint('A hint message'),
    ];
804
    return const ValidationResult(ValidationType.missing, messages);
805 806 807
  }
}

808
class NotAvailableValidator extends DoctorValidator {
809
  NotAvailableValidator() : super('Not Available Validator');
810 811 812

  @override
  Future<ValidationResult> validate() async {
813
    const List<ValidationMessage> messages = <ValidationMessage>[
814 815 816 817
      ValidationMessage.error('A useful error message'),
      ValidationMessage('A message that is not an error'),
      ValidationMessage.hint('A hint message'),
    ];
818
    return const ValidationResult(ValidationType.notAvailable, messages);
819 820 821
  }
}

822 823 824 825 826 827 828 829 830 831 832 833
class StuckValidator extends DoctorValidator {
  StuckValidator() : super('Stuck validator that never completes');

  @override
  Future<ValidationResult> validate() {
    final Completer<ValidationResult> completer = Completer<ValidationResult>();

    // This future will never complete
    return completer.future;
  }
}

834 835 836 837 838
class PartialValidatorWithErrors extends DoctorValidator {
  PartialValidatorWithErrors() : super('Partial Validator with Errors');

  @override
  Future<ValidationResult> validate() async {
839
    const List<ValidationMessage> messages = <ValidationMessage>[
840 841 842 843
      ValidationMessage.error('An error message indicating partial installation'),
      ValidationMessage.hint('Maybe a hint will help the user'),
      ValidationMessage('An extra message with some verbose details'),
    ];
844
    return const ValidationResult(ValidationType.partial, messages);
845 846 847 848 849 850 851 852
  }
}

class PartialValidatorWithHintsOnly extends DoctorValidator {
  PartialValidatorWithHintsOnly() : super('Partial Validator with only a Hint');

  @override
  Future<ValidationResult> validate() async {
853
    const List<ValidationMessage> messages = <ValidationMessage>[
854 855 856
      ValidationMessage.hint('There is a hint here'),
      ValidationMessage('But there is no error'),
    ];
857
    return const ValidationResult(ValidationType.partial, messages);
858 859 860
  }
}

861 862 863 864 865
class CrashingValidator extends DoctorValidator {
  CrashingValidator() : super('Crashing validator');

  @override
  Future<ValidationResult> validate() async {
866
    throw StateError('fatal error');
867 868 869
  }
}

870 871 872 873 874 875 876 877
class AsyncCrashingValidator extends DoctorValidator {
  AsyncCrashingValidator(this._time) : super('Async crashing validator');

  final FakeAsync _time;

  @override
  Future<ValidationResult> validate() {
    const Duration delay = Duration(seconds: 1);
878 879
    final Future<ValidationResult> result = Future<ValidationResult>.delayed(delay)
      .then((_) {
880
        throw StateError('fatal error');
881
      });
882 883 884 885 886 887
    _time.elapse(const Duration(seconds: 1));
    _time.flushMicrotasks();
    return result;
  }
}

888 889
/// A doctor that fails with a missing [ValidationResult].
class FakeDoctor extends Doctor {
890 891
  FakeDoctor(Logger logger) : super(logger: logger);

892 893 894 895
  List<DoctorValidator> _validators;

  @override
  List<DoctorValidator> get validators {
896 897 898 899 900 901 902
    return _validators ??= <DoctorValidator>[
      PassingValidator('Passing Validator'),
      MissingValidator(),
      NotAvailableValidator(),
      PartialValidatorWithHintsOnly(),
      PartialValidatorWithErrors(),
    ];
903 904 905 906 907
  }
}

/// A doctor that should pass, but still has issues in some categories.
class FakePassingDoctor extends Doctor {
908 909
  FakePassingDoctor(Logger logger) : super(logger: logger);

910 911 912
  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
913 914 915 916 917 918
    return _validators ??= <DoctorValidator>[
      PassingValidator('Passing Validator'),
      PartialValidatorWithHintsOnly(),
      PartialValidatorWithErrors(),
      PassingValidator('Another Passing Validator'),
    ];
919 920 921 922 923 924
  }
}

/// A doctor that should pass, but still has 1 issue to test the singular of
/// categories.
class FakeSinglePassingDoctor extends Doctor {
925 926
  FakeSinglePassingDoctor(Logger logger) : super(logger: logger);

927 928 929
  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
930 931 932
    return _validators ??= <DoctorValidator>[
      PartialValidatorWithHintsOnly(),
    ];
933 934 935 936 937
  }
}

/// A doctor that passes and has no issues anywhere.
class FakeQuietDoctor extends Doctor {
938 939
  FakeQuietDoctor(Logger logger) : super(logger: logger);

940 941 942
  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
943 944 945 946 947 948
    return _validators ??= <DoctorValidator>[
      PassingValidator('Passing Validator'),
      PassingValidator('Another Passing Validator'),
      PassingValidator('Validators are fun'),
      PassingValidator('Four score and seven validators ago'),
    ];
949
  }
950 951
}

952 953 954 955 956 957 958 959 960 961 962 963 964
/// A doctor that passes and contains PII that can be hidden.
class FakePiiDoctor extends Doctor {
  FakePiiDoctor(Logger logger) : super(logger: logger);

  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
    return _validators ??= <DoctorValidator>[
      PiiValidator(),
    ];
  }
}

965 966
/// A doctor with a validator that throws an exception.
class FakeCrashingDoctor extends Doctor {
967 968
  FakeCrashingDoctor(Logger logger) : super(logger: logger);

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
    if (_validators == null) {
      _validators = <DoctorValidator>[];
      _validators.add(PassingValidator('Passing Validator'));
      _validators.add(PassingValidator('Another Passing Validator'));
      _validators.add(CrashingValidator());
      _validators.add(PassingValidator('Validators are fun'));
      _validators.add(PassingValidator('Four score and seven validators ago'));
    }
    return _validators;
  }
}

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
/// A doctor with a validator that will never finish.
class FakeAsyncStuckDoctor extends Doctor {
  FakeAsyncStuckDoctor(Logger logger) : super(logger: logger);

  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
    if (_validators == null) {
      _validators = <DoctorValidator>[];
      _validators.add(PassingValidator('Passing Validator'));
      _validators.add(PassingValidator('Another Passing Validator'));
      _validators.add(StuckValidator());
      _validators.add(PassingValidator('Validators are fun'));
      _validators.add(PassingValidator('Four score and seven validators ago'));
    }
    return _validators;
  }
}

1003 1004
/// A doctor with a validator that throws an exception.
class FakeAsyncCrashingDoctor extends Doctor {
1005
  FakeAsyncCrashingDoctor(this._time, Logger logger) : super(logger: logger);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

  final FakeAsync _time;

  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
    if (_validators == null) {
      _validators = <DoctorValidator>[];
      _validators.add(PassingValidator('Passing Validator'));
      _validators.add(PassingValidator('Another Passing Validator'));
      _validators.add(AsyncCrashingValidator(_time));
      _validators.add(PassingValidator('Validators are fun'));
      _validators.add(PassingValidator('Four score and seven validators ago'));
    }
    return _validators;
  }
}

1024 1025 1026 1027 1028 1029
/// A DoctorValidatorsProvider that overrides the default validators without
/// overriding the doctor.
class FakeDoctorValidatorsProvider implements DoctorValidatorsProvider {
  @override
  List<DoctorValidator> get validators {
    return <DoctorValidator>[
1030 1031
      PassingValidator('Passing Validator'),
      PassingValidator('Another Passing Validator'),
1032
      PassingValidator('Providing validators is fun'),
1033 1034
    ];
  }
1035 1036 1037 1038 1039 1040

  @override
  List<Workflow> get workflows => <Workflow>[];
}

class PassingGroupedValidator extends DoctorValidator {
1041
  PassingGroupedValidator(String name) : super(name);
1042 1043 1044

  @override
  Future<ValidationResult> validate() async {
1045
    const List<ValidationMessage> messages = <ValidationMessage>[
1046 1047
      ValidationMessage('A helpful message'),
    ];
1048
    return const ValidationResult(ValidationType.installed, messages);
1049 1050 1051 1052
  }
}

class MissingGroupedValidator extends DoctorValidator {
1053
  MissingGroupedValidator(String name) : super(name);
1054 1055 1056

  @override
  Future<ValidationResult> validate() async {
1057
    const List<ValidationMessage> messages = <ValidationMessage>[
1058 1059
      ValidationMessage.error('A useful error message'),
    ];
1060
    return const ValidationResult(ValidationType.missing, messages);
1061 1062 1063 1064
  }
}

class PartialGroupedValidator extends DoctorValidator {
1065
  PartialGroupedValidator(String name) : super(name);
1066 1067 1068

  @override
  Future<ValidationResult> validate() async {
1069
    const List<ValidationMessage> messages = <ValidationMessage>[
1070 1071
      ValidationMessage.error('An error message for partial installation'),
    ];
1072
    return const ValidationResult(ValidationType.partial, messages);
1073 1074 1075
  }
}

1076 1077 1078 1079 1080
class PassingGroupedValidatorWithStatus extends DoctorValidator {
  PassingGroupedValidatorWithStatus(String name) : super(name);

  @override
  Future<ValidationResult> validate() async {
1081
    const List<ValidationMessage> messages = <ValidationMessage>[
1082 1083
      ValidationMessage('A different message'),
    ];
1084
    return const ValidationResult(ValidationType.installed, messages, statusInfo: 'A status message');
1085 1086 1087 1088
  }
}

/// A doctor that has two groups of two validators each.
1089
class FakeGroupedDoctor extends Doctor {
1090 1091
  FakeGroupedDoctor(Logger logger) : super(logger: logger);

1092 1093 1094
  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
1095 1096
    return _validators ??= <DoctorValidator>[
      GroupedValidator(<DoctorValidator>[
1097
        PassingGroupedValidator('Category 1'),
1098
        PassingGroupedValidator('Category 1'),
1099 1100
      ]),
      GroupedValidator(<DoctorValidator>[
1101
        PassingGroupedValidator('Category 2'),
1102
        MissingGroupedValidator('Category 2'),
1103 1104
      ]),
    ];
1105 1106 1107
  }
}

1108
class FakeGroupedDoctorWithStatus extends Doctor {
1109 1110
  FakeGroupedDoctorWithStatus(Logger logger) : super(logger: logger);

1111 1112 1113
  List<DoctorValidator> _validators;
  @override
  List<DoctorValidator> get validators {
1114
    return _validators ??= <DoctorValidator>[
1115 1116 1117
      GroupedValidator(<DoctorValidator>[
        PassingGroupedValidator('First validator title'),
        PassingGroupedValidatorWithStatus('Second validator title'),
1118 1119
      ]),
    ];
1120 1121 1122
  }
}

1123 1124 1125
/// A doctor that takes any two validators. Used to check behavior when
/// merging ValidationTypes (installed, missing, partial).
class FakeSmallGroupDoctor extends Doctor {
1126
  FakeSmallGroupDoctor(Logger logger, DoctorValidator val1, DoctorValidator val2) : super(logger: logger) {
1127
    _validators = <DoctorValidator>[GroupedValidator(<DoctorValidator>[val1, val2])];
1128
  }
1129 1130 1131

  List<DoctorValidator> _validators;

1132 1133
  @override
  List<DoctorValidator> get validators => _validators;
1134 1135
}

1136
class VsCodeValidatorTestTargets extends VsCodeValidator {
1137
  VsCodeValidatorTestTargets._(String installDirectory, String extensionDirectory, {String edition})
1138
    : super(VsCode.fromDirectory(installDirectory, extensionDirectory, edition: edition, fileSystem: globals.fs));
1139 1140

  static VsCodeValidatorTestTargets get installedWithExtension =>
1141
      VsCodeValidatorTestTargets._(validInstall, validExtensions);
1142

1143
  static VsCodeValidatorTestTargets get installedWithExtension64bit =>
1144
      VsCodeValidatorTestTargets._(validInstall, validExtensions, edition: '64-bit edition');
1145

1146
  static VsCodeValidatorTestTargets get installedWithoutExtension =>
1147
      VsCodeValidatorTestTargets._(validInstall, missingExtensions);
1148

1149 1150 1151
  static final String validInstall = globals.fs.path.join('test', 'data', 'vscode', 'application');
  static final String validExtensions = globals.fs.path.join('test', 'data', 'vscode', 'extensions');
  static final String missingExtensions = globals.fs.path.join('test', 'data', 'vscode', 'notExtensions');
1152
}
1153

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
class FakeDeviceManager extends Fake implements DeviceManager {
  List<String> diagnostics = <String>[];
  List<Device> devices = <Device>[];

  @override
  Future<List<Device>> getAllConnectedDevices() async => devices;

  @override
  Future<List<String>> getDeviceDiagnostics() async => diagnostics;
}

1165 1166 1167
// Unfortunately Device, despite not being immutable, has an `operator ==`.
// Until we fix that, we have to also ignore related lints here.
// ignore: avoid_implementing_value_types
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
class FakeDevice extends Fake implements Device {
  @override
  String get name => 'name';

  @override
  String get id => 'device-id';

  @override
  Category get category => Category.mobile;

  @override
  bool isSupported() => true;

  @override
  Future<bool> get isLocalEmulator async => false;

  @override
  Future<String> get targetPlatformDisplayName async => 'android';

  @override
  Future<String> get sdkNameAndVersion async => '1.2.3';

  @override
  Future<TargetPlatform> get targetPlatform =>  Future<TargetPlatform>.value(TargetPlatform.android);
1192
}