flutter_validator_test.dart 20.3 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:file/file.dart';
6 7 8 9 10 11
import 'package:file/memory.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/user_messages.dart';
import 'package:flutter_tools/src/doctor.dart';
12
import 'package:flutter_tools/src/doctor_validator.dart';
13
import 'package:flutter_tools/src/version.dart';
14
import 'package:test/fake.dart';
15 16

import '../src/common.dart';
17
import '../src/fake_process_manager.dart';
18
import '../src/fakes.dart';
19

20 21
/// Matches a doctor validation result.
Matcher _matchDoctorValidation({
22 23 24
  required ValidationType validationType,
  required String statusInfo,
  required Object messages
25 26 27 28 29 30 31
}) {
  return const TypeMatcher<ValidationResult>()
      .having((ValidationResult result) => result.type, 'type', validationType)
      .having((ValidationResult result) => result.statusInfo, 'statusInfo', statusInfo)
      .having((ValidationResult result) => result.messages, 'messages', messages);
}

32 33 34
void main() {
  testWithoutContext('FlutterValidator shows an error message if gen_snapshot is '
    'downloaded and exits with code 1', () async {
35 36
    final FakeFlutterVersion flutterVersion = FakeFlutterVersion(
      frameworkVersion: '1.0.0',
37
      channel: 'beta',
38
    );
39 40 41 42 43 44 45 46
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final Artifacts artifacts = Artifacts.test();
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(
        localeName: 'en_US.UTF-8',
        environment: <String, String>{},
      ),
      flutterVersion: () => flutterVersion,
47
        devToolsVersion: () => '2.8.0',
48 49 50 51 52 53 54 55 56
      userMessages: UserMessages(),
      artifacts: artifacts,
      fileSystem: fileSystem,
      flutterRoot: () => 'sdk/flutter',
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['Artifact.genSnapshot'],
          exitCode: 1,
57
        ),
58 59 60 61 62
      ])
    );
    fileSystem.file(artifacts.getArtifactPath(Artifact.genSnapshot)).createSync(recursive: true);


63
    expect(await flutterValidator.validate(), _matchDoctorValidation(
64
      validationType: ValidationType.partial,
65
      statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
66 67 68 69 70 71 72 73 74 75 76 77 78
      messages: containsAll(const <ValidationMessage>[
        ValidationMessage.error(
          'Downloaded executables cannot execute on host.\n'
          'See https://github.com/flutter/flutter/issues/6207 for more information\n'
          'On Debian/Ubuntu/Mint: sudo apt-get install lib32stdc++6\n'
          'On Fedora: dnf install libstdc++.i686\n'
          'On Arch: pacman -S lib32-gcc-libs\n',
        ),
      ])),
    );
  });

  testWithoutContext('FlutterValidator does not run gen_snapshot binary check if it is not already downloaded', () async {
79 80
    final FakeFlutterVersion flutterVersion = FakeFlutterVersion(
      frameworkVersion: '1.0.0',
81
      channel: 'beta',
82
    );
83 84 85 86 87 88
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(
        operatingSystem: 'windows',
        localeName: 'en_US.UTF-8',
        environment: <String, String>{},
      ),
89
      flutterVersion: () => flutterVersion,
90
      devToolsVersion: () => '2.8.0',
91 92 93 94
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: MemoryFileSystem.test(),
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Windows'),
95
      processManager: FakeProcessManager.empty(),
96 97 98 99 100
      flutterRoot: () => 'sdk/flutter',
    );

    // gen_snapshot is downloaded on demand, and the doctor should not
    // fail if the gen_snapshot binary is not present.
101
    expect(await flutterValidator.validate(), _matchDoctorValidation(
102
      validationType: ValidationType.installed,
103
      statusInfo: 'Channel beta, 1.0.0, on Windows, locale en_US.UTF-8',
104 105 106 107 108 109 110
      messages: anything,
    ));
  });

  testWithoutContext('FlutterValidator handles exception thrown by version checking', () async {
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(operatingSystem: 'windows', localeName: 'en_US.UTF-8'),
111
      flutterVersion: () => FakeThrowingFlutterVersion(),
112
      devToolsVersion: () => '2.8.0',
113 114 115 116
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: MemoryFileSystem.test(),
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Windows'),
117
      processManager: FakeProcessManager.empty(),
118 119 120
      flutterRoot: () => 'sdk/flutter',
    );

121
    expect(await flutterValidator.validate(), _matchDoctorValidation(
122
      validationType: ValidationType.partial,
123
      statusInfo: 'Channel beta, 0.0.0, on Windows, locale en_US.UTF-8',
124
      messages: containsAll(const <ValidationMessage>[
125
        ValidationMessage('Flutter version 0.0.0 on channel beta at sdk/flutter'),
126 127
        ValidationMessage.error('version error'),
      ]),
128
    ));
129 130 131
  });

  testWithoutContext('FlutterValidator shows mirrors on pub and flutter cloud storage', () async {
132 133
    final FakeFlutterVersion flutterVersion = FakeFlutterVersion(
      frameworkVersion: '1.0.0',
134
      channel: 'beta',
135
    );
136 137 138 139 140 141 142 143 144 145 146 147 148
    final Platform platform = FakePlatform(
      operatingSystem: 'windows',
      localeName: 'en_US.UTF-8',
      environment: <String, String>{
        'PUB_HOSTED_URL': 'https://example.com/pub',
        'FLUTTER_STORAGE_BASE_URL': 'https://example.com/flutter',
      },
    );
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final Artifacts artifacts = Artifacts.test();
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: platform,
      flutterVersion: () => flutterVersion,
149
        devToolsVersion: () => '2.8.0',
150 151 152 153 154 155 156 157
      userMessages: UserMessages(),
      artifacts: artifacts,
      fileSystem: fileSystem,
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Windows'),
      flutterRoot: () => 'sdk/flutter'
    );

158
    expect(await flutterValidator.validate(), _matchDoctorValidation(
159
      validationType: ValidationType.installed,
160
      statusInfo: 'Channel beta, 1.0.0, on Windows, locale en_US.UTF-8',
161 162 163 164 165 166
      messages: containsAll(const <ValidationMessage>[
        ValidationMessage('Pub download mirror https://example.com/pub'),
        ValidationMessage('Flutter download mirror https://example.com/flutter'),
      ])
    ));
  });
167

168
  testWithoutContext('FlutterValidator shows FLUTTER_GIT_URL when set and fails if upstream is not the same', () async {
169 170 171 172 173 174 175
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(
        localeName: 'en_US.UTF-8',
        environment: <String, String> {
          'FLUTTER_GIT_URL': 'https://githubmirror.com/flutter.git',
        },
      ),
176 177 178 179
      flutterVersion: () => FakeFlutterVersion(
        frameworkVersion: '1.0.0',
        channel: 'beta'
      ),
180
      devToolsVersion: () => '2.8.0',
181 182 183 184 185 186 187 188 189
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: MemoryFileSystem.test(),
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
      flutterRoot: () => 'sdk/flutter',
    );

    expect(await flutterValidator.validate(), _matchDoctorValidation(
190 191 192 193 194
      validationType: ValidationType.partial,
      statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
      messages: containsAll(const <ValidationMessage>[
        ValidationMessage.hint('Upstream repository https://github.com/flutter/flutter.git is not the same as FLUTTER_GIT_URL'),
        ValidationMessage('FLUTTER_GIT_URL = https://githubmirror.com/flutter.git'),
195 196 197 198
        ValidationMessage(
          'If those were intentional, you can disregard the above warnings; however it is '
          'recommended to use "git" directly to perform update checks and upgrades.'
        ),
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
      ]),
    ));
  });

  testWithoutContext('FlutterValidator fails when channel is unknown', () async {
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(localeName: 'en_US.UTF-8'),
      flutterVersion: () => FakeFlutterVersion(
        frameworkVersion: '1.0.0',
        // channel is unknown by default
      ),
      devToolsVersion: () => '2.8.0',
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: MemoryFileSystem.test(),
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
      flutterRoot: () => 'sdk/flutter',
    );

    expect(await flutterValidator.validate(), _matchDoctorValidation(
      validationType: ValidationType.partial,
221
      statusInfo: 'Channel unknown, 1.0.0, on Linux, locale en_US.UTF-8',
222 223 224 225 226 227 228 229 230 231 232
      messages: containsAll(<ValidationMessage>[
        const ValidationMessage.hint(
          'Flutter version 1.0.0 on channel unknown at sdk/flutter\n'
          'Currently on an unknown channel. Run `flutter channel` to switch to an official channel.\n'
          "If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install."
        ),
        const ValidationMessage(
          'If those were intentional, you can disregard the above warnings; however it is '
          'recommended to use "git" directly to perform update checks and upgrades.'
        ),
      ]),
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    ));
  });

  testWithoutContext('FlutterValidator fails when framework version is unknown', () async {
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(localeName: 'en_US.UTF-8'),
      flutterVersion: () => FakeFlutterVersion(
        frameworkVersion: '0.0.0-unknown',
        channel: 'beta',
      ),
      devToolsVersion: () => '2.8.0',
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: MemoryFileSystem.test(),
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
      flutterRoot: () => 'sdk/flutter',
    );

    expect(await flutterValidator.validate(), _matchDoctorValidation(
      validationType: ValidationType.partial,
      statusInfo: 'Channel beta, 0.0.0-unknown, on Linux, locale en_US.UTF-8',
255 256 257 258 259 260 261 262 263 264 265
      messages: containsAll(<ValidationMessage>[
        const ValidationMessage.hint(
          'Flutter version 0.0.0-unknown on channel beta at sdk/flutter\n'
          'Cannot resolve current version, possibly due to local changes.\n'
          'Reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.'
        ),
        const ValidationMessage(
          'If those were intentional, you can disregard the above warnings; however it is '
          'recommended to use "git" directly to perform update checks and upgrades.'
        ),
      ]),
266 267 268 269
    ));
  });

  group('FlutterValidator shows flutter upstream remote', () {
270
    testWithoutContext('standard url', () async {
271 272
      final FlutterValidator flutterValidator = FlutterValidator(
        platform: FakePlatform(localeName: 'en_US.UTF-8'),
273 274 275 276
        flutterVersion: () => FakeFlutterVersion(
          frameworkVersion: '1.0.0',
          channel: 'beta'
        ),
277
        devToolsVersion: () => '2.8.0',
278 279 280 281 282 283 284 285 286 287
        userMessages: UserMessages(),
        artifacts: Artifacts.test(),
        fileSystem: MemoryFileSystem.test(),
        processManager: FakeProcessManager.any(),
        operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
        flutterRoot: () => 'sdk/flutter',
      );

      expect(await flutterValidator.validate(), _matchDoctorValidation(
        validationType: ValidationType.installed,
288
        statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
289 290 291 292
        messages: contains(const ValidationMessage('Upstream repository https://github.com/flutter/flutter.git')),
      ));
    });

293
    testWithoutContext('non-standard url', () async {
294 295 296 297
      final FlutterValidator flutterValidator = FlutterValidator(
        platform: FakePlatform(localeName: 'en_US.UTF-8'),
        flutterVersion: () => FakeFlutterVersion(
          frameworkVersion: '1.0.0',
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
          channel: 'beta',
          repositoryUrl: 'https://githubmirror.com/flutter.git'
        ),
        devToolsVersion: () => '2.8.0',
        userMessages: UserMessages(),
        artifacts: Artifacts.test(),
        fileSystem: MemoryFileSystem.test(),
        processManager: FakeProcessManager.any(),
        operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
        flutterRoot: () => 'sdk/flutter',
      );

      expect(await flutterValidator.validate(), _matchDoctorValidation(
        validationType: ValidationType.partial,
        statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
313 314 315 316 317 318 319 320 321 322 323
        messages: containsAll(<ValidationMessage>[
          const ValidationMessage.hint(
            'Upstream repository https://githubmirror.com/flutter.git is not a standard remote.\n'
            'Set environment variable "FLUTTER_GIT_URL" to '
            'https://githubmirror.com/flutter.git to dismiss this error.'
          ),
          const ValidationMessage(
            'If those were intentional, you can disregard the above warnings; however it is '
            'recommended to use "git" directly to perform update checks and upgrades.'
          ),
        ]),
324 325 326 327 328 329 330 331 332
      ));
    });

    testWithoutContext('as unknown if upstream is null', () async {
      final FlutterValidator flutterValidator = FlutterValidator(
        platform: FakePlatform(localeName: 'en_US.UTF-8'),
        flutterVersion: () => FakeFlutterVersion(
          frameworkVersion: '1.0.0',
          channel: 'beta',
333 334
          repositoryUrl: null,
        ),
335
        devToolsVersion: () => '2.8.0',
336 337 338 339 340 341 342 343 344
        userMessages: UserMessages(),
        artifacts: Artifacts.test(),
        fileSystem: MemoryFileSystem.test(),
        processManager: FakeProcessManager.any(),
        operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
        flutterRoot: () => 'sdk/flutter',
      );

      expect(await flutterValidator.validate(), _matchDoctorValidation(
345 346
        validationType: ValidationType.partial,
        statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
347 348 349 350 351 352 353 354 355 356
        messages: containsAll(<ValidationMessage>[
          const ValidationMessage.hint(
            'Unknown upstream repository.\n'
            'Reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.'
          ),
          const ValidationMessage(
            'If those were intentional, you can disregard the above warnings; however it is '
            'recommended to use "git" directly to perform update checks and upgrades.'
          ),
        ]),
357 358 359
      ));
    });
  });
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

  testWithoutContext('Do not show the message for intentional errors if FlutterValidator passes', () async {
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(localeName: 'en_US.UTF-8'),
      flutterVersion: () => FakeFlutterVersion(
        frameworkVersion: '1.0.0',
        channel: 'beta'
      ),
      devToolsVersion: () => '2.8.0',
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: MemoryFileSystem.test(),
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(name: 'Linux'),
      flutterRoot: () => 'sdk/flutter',
    );

    expect(await flutterValidator.validate(), _matchDoctorValidation(
      validationType: ValidationType.installed,
      statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
      messages: isNot(
        contains(const ValidationMessage(
          'If those were intentional, you can disregard the above warnings; however it is '
          'recommended to use "git" directly to perform update checks and upgrades.'
        )),
      ),
    ));
  });
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

  testWithoutContext('detects no flutter and dart on path', () async {
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(localeName: 'en_US.UTF-8'),
      flutterVersion: () => FakeFlutterVersion(
        frameworkVersion: '1.0.0',
        channel: 'beta'
      ),
      devToolsVersion: () => '2.8.0',
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: MemoryFileSystem.test(),
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(
        name: 'Linux',
        whichLookup: const <String, File>{},
      ),
      flutterRoot: () => 'sdk/flutter',
    );

    expect(await flutterValidator.validate(), _matchDoctorValidation(
      validationType: ValidationType.partial,
      statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
      messages: contains(const ValidationMessage.hint(
        'The flutter binary is not on your path. Consider adding sdk/flutter/bin to your path.',
      )),
    ));
  });

  testWithoutContext('detects flutter and dart from outside flutter sdk', () async {
    final FileSystem fs = MemoryFileSystem.test();
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(localeName: 'en_US.UTF-8'),
      flutterVersion: () => FakeFlutterVersion(
        frameworkVersion: '1.0.0',
        channel: 'beta'
      ),
      devToolsVersion: () => '2.8.0',
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: fs,
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(
        name: 'Linux',
        whichLookup: <String, File>{
          'flutter': fs.file('/usr/bin/flutter')..createSync(recursive: true),
          'dart': fs.file('/usr/bin/dart')..createSync(recursive: true),
        },
      ),
      flutterRoot: () => 'sdk/flutter',
    );

    expect(await flutterValidator.validate(), _matchDoctorValidation(
      validationType: ValidationType.partial,
      statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
      messages: contains(const ValidationMessage.hint(
        'Warning: `flutter` on your path resolves to /usr/bin/flutter, which '
        'is not inside your current Flutter SDK checkout at sdk/flutter. '
        'Consider adding sdk/flutter/bin to the front of your path.',
      )),
    ));
  });

  testWithoutContext('no warnings if flutter & dart binaries are inside the Flutter SDK', () async {
    final FileSystem fs = MemoryFileSystem.test();
    final FlutterValidator flutterValidator = FlutterValidator(
      platform: FakePlatform(localeName: 'en_US.UTF-8'),
      flutterVersion: () => FakeFlutterVersion(
        frameworkVersion: '1.0.0',
        channel: 'beta'
      ),
      devToolsVersion: () => '2.8.0',
      userMessages: UserMessages(),
      artifacts: Artifacts.test(),
      fileSystem: fs,
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(
        name: 'Linux',
        whichLookup: <String, File>{
          'flutter': fs.file('/sdk/flutter/bin/flutter')..createSync(recursive: true),
          'dart': fs.file('/sdk/flutter/bin/dart')..createSync(recursive: true),
        },
      ),
      flutterRoot: () => '/sdk/flutter',
    );

    expect(await flutterValidator.validate(), _matchDoctorValidation(
      validationType: ValidationType.installed,
      statusInfo: 'Channel beta, 1.0.0, on Linux, locale en_US.UTF-8',
      messages: isNot(contains(isA<ValidationMessage>().having(
        (ValidationMessage message) => message.message,
        'message',
        contains('Consider adding /sdk/flutter/bin to the front of your path'),
      ))),
    ));
  });
484 485 486
}

class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  FakeOperatingSystemUtils({
    required this.name,
    this.whichLookup,
    FileSystem? fs,
  }) {
    fs ??= MemoryFileSystem.test();
    whichLookup ??= <String, File>{
      'flutter': fs.file('/sdk/flutter/bin/flutter')..createSync(recursive: true),
      'dart': fs.file('/sdk/flutter/bin/dart')..createSync(recursive: true),
    };
  }

  /// A map of [File]s that calls to [which] will return.
  Map<String, File>? whichLookup;

  @override
  File? which(String execName) => whichLookup![execName];
504 505 506 507

  @override
  final String name;
}
508 509

class FakeThrowingFlutterVersion extends FakeFlutterVersion {
510 511 512
  @override
  String get channel => 'beta';

513 514 515 516
  @override
  String get frameworkCommitDate {
    throw VersionCheckError('version error');
  }
517
}