drive_test.dart 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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.

// @dart = 2.8

import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
11
import 'package:flutter_tools/src/cache.dart';
12
import 'package:flutter_tools/src/commands/drive.dart';
13
import 'package:flutter_tools/src/dart/pub.dart';
14 15 16 17
import 'package:flutter_tools/src/device.dart';
import 'package:test/fake.dart';

import '../../src/common.dart';
18
import '../../src/context.dart';
19
import '../../src/test_flutter_command_runner.dart';
20 21 22 23 24 25 26 27 28 29 30 31

void main() {
  FileSystem fileSystem;
  BufferLogger logger;
  Platform platform;

  setUp(() {
    fileSystem = MemoryFileSystem.test();
    logger = BufferLogger.test();
    platform = FakePlatform(operatingSystem: 'linux');
  });

32 33 34 35 36 37 38 39 40
  setUpAll(() {
    Cache.disableLocking();
  });

  tearDownAll(() {
    Cache.enableLocking();
  });


41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  testWithoutContext('drive --screenshot writes to expected output', () async {
    final Device screenshotDevice = ScreenshotDevice();

    await takeScreenshot(
      screenshotDevice,
      'drive_screenshots',
      fileSystem,
      logger,
      FileSystemUtils(
        fileSystem: fileSystem,
        platform: platform,
      ),
    );

    expect(logger.statusText, contains('Screenshot written to drive_screenshots/drive_01.png'));
  });

  testWithoutContext('drive --screenshot errors but does not fail if screenshot fails', () async {
    final Device screenshotDevice = ScreenshotDevice();
    fileSystem.file('drive_screenshots').createSync();

    await takeScreenshot(
      screenshotDevice,
      'drive_screenshots',
      fileSystem,
      logger,
      FileSystemUtils(
        fileSystem: fileSystem,
        platform: platform,
      ),
    );

    expect(logger.statusText, isEmpty);
    expect(logger.errorText, contains('Error taking screenshot: FileSystemException: Not a directory'));
  });
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

  testUsingContext('shouldRunPub is true unless user specifies --no-pub', () async {
    final DriveCommand command = DriveCommand(fileSystem: fileSystem, logger: logger, platform: platform);
    fileSystem.file('lib/main.dart').createSync(recursive: true);
    fileSystem.file('test_driver/main_test.dart').createSync(recursive: true);
    fileSystem.file('pubspec.yaml').createSync();

    try {
      await createTestCommandRunner(command).run(const <String>['drive', '--no-pub']);
    } on Exception {
      // Expected to throw
    }

    expect(command.shouldRunPub, false);

    try {
      await createTestCommandRunner(command).run(const <String>['drive']);
    } on Exception {
      // Expected to throw
    }

    expect(command.shouldRunPub, true);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
    Pub: () => FakePub(),
  });
103 104 105 106 107 108
}

class ScreenshotDevice extends Fake implements Device {
  @override
  Future<void> takeScreenshot(File outputFile) async {}
}
109 110 111 112 113 114 115 116 117 118 119 120 121 122

class FakePub extends Fake implements Pub {
  @override
  Future<void> get({
    PubContext context,
    String directory,
    bool skipIfAbsent = false,
    bool upgrade = false,
    bool offline = false,
    bool generateSyntheticPackage = false,
    String flutterRootOverride,
    bool checkUpToDate = false,
  }) async { }
}