testbed.dart 6.24 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
import 'dart:async';
8
import 'dart:io';
9 10 11 12 13

import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
14
import 'package:flutter_tools/src/base/os.dart';
Dan Field's avatar
Dan Field committed
15
import 'package:flutter_tools/src/base/process.dart';
16

17
import 'package:flutter_tools/src/base/signals.dart';
18
import 'package:flutter_tools/src/base/terminal.dart';
19 20
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/context_runner.dart';
21
import 'package:flutter_tools/src/dart/pub.dart';
22
import 'package:flutter_tools/src/reporting/reporting.dart';
23
import 'package:flutter_tools/src/version.dart';
24
import 'package:flutter_tools/src/globals.dart' as globals;
25
import 'package:meta/meta.dart';
26
import 'package:process/process.dart';
27

28
import 'common.dart' as tester;
29
import 'context.dart';
30
import 'fake_http_client.dart';
31
import 'fakes.dart';
32
import 'throwing_pub.dart';
33

34 35
export 'package:flutter_tools/src/base/context.dart' show Generator;

36 37
// A default value should be provided if the vast majority of tests should use
// this provider. For example, [BufferLogger], [MemoryFileSystem].
38
final Map<Type, Generator> _testbedDefaults = <Type, Generator>{
39
  // Keeps tests fast by avoiding the actual file system.
40
  FileSystem: () => MemoryFileSystem(style: globals.platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix),
41
  ProcessManager: () => FakeProcessManager.any(),
42 43 44 45
  Logger: () => BufferLogger(
    terminal: AnsiTerminal(stdio: globals.stdio, platform: globals.platform),  // Danger, using real stdio.
    outputPreferences: OutputPreferences.test(),
  ), // Allows reading logs and prevents stdout.
46
  OperatingSystemUtils: () => FakeOperatingSystemUtils(),
47
  OutputPreferences: () => OutputPreferences.test(), // configures BufferLogger to avoid color codes.
48
  Usage: () => TestUsage(), // prevent addition of analytics from burdening test mocks
49
  FlutterVersion: () => FakeFlutterVersion(), // prevent requirement to mock git for test runner.
50
  Signals: () => FakeSignals(),  // prevent registering actual signal handlers.
51
  Pub: () => ThrowingPub(), // prevent accidental invocations of pub.
52 53 54 55 56 57 58 59 60
};

/// Manages interaction with the tool injection and runner system.
///
/// The Testbed automatically injects reasonable defaults through the context
/// DI system such as a [BufferLogger] and a [MemoryFileSytem].
///
/// Example:
///
61
/// Testing that a filesystem operation works as expected:
62 63 64 65 66 67 68
///
///     void main() {
///       group('Example', () {
///         Testbed testbed;
///
///         setUp(() {
///           testbed = Testbed(setUp: () {
69
///             globals.fs.file('foo').createSync()
70 71 72
///           });
///         })
///
73
///         test('Can delete a file', () => testbed.run(() {
74 75 76
///           expect(globals.fs.file('foo').existsSync(), true);
///           globals.fs.file('foo').deleteSync();
///           expect(globals.fs.file('foo').existsSync(), false);
77 78 79 80 81 82 83 84 85 86 87
///         }));
///       });
///     }
///
/// For a more detailed example, see the code in test_compiler_test.dart.
class Testbed {
  /// Creates a new [TestBed]
  ///
  /// `overrides` provides more overrides in addition to the test defaults.
  /// `setup` may be provided to apply mocks within the tool managed zone,
  /// including any specified overrides.
88
  Testbed({FutureOr<void> Function() setup, Map<Type, Generator> overrides})
89 90
      : _setup = setup,
        _overrides = overrides;
91

92
  final FutureOr<void> Function() _setup;
93 94
  final Map<Type, Generator> _overrides;

95 96 97 98 99 100 101 102 103 104
  /// Runs the `test` within a tool zone.
  ///
  /// Unlike [run], this sets up a test group on its own.
  @isTest
  void test<T>(String name, FutureOr<T> Function() test, {Map<Type, Generator> overrides}) {
    tester.test(name, () {
      return run(test, overrides: overrides);
    });
  }

105
  /// Runs `test` within a tool zone.
106 107 108
  ///
  /// `overrides` may be used to provide new context values for the single test
  /// case or override any context values from the setup.
109
  Future<T> run<T>(FutureOr<T> Function() test, {Map<Type, Generator> overrides}) {
110 111 112 113 114 115 116
    final Map<Type, Generator> testOverrides = <Type, Generator>{
      ..._testbedDefaults,
      // Add the initial setUp overrides
      ...?_overrides,
      // Add the test-specific overrides
      ...?overrides,
    };
Dan Field's avatar
Dan Field committed
117 118 119
    if (testOverrides.containsKey(ProcessUtils)) {
      throw StateError('Do not inject ProcessUtils for testing, use ProcessManager instead.');
    }
120 121
    // Cache the original flutter root to restore after the test case.
    final String originalFlutterRoot = Cache.flutterRoot;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    // Track pending timers to verify that they were correctly cleaned up.
    final Map<Timer, StackTrace> timers = <Timer, StackTrace>{};

    return HttpOverrides.runZoned(() {
      return runInContext<T>(() {
        return context.run<T>(
          name: 'testbed',
          overrides: testOverrides,
          zoneSpecification: ZoneSpecification(
            createTimer: (Zone self, ZoneDelegate parent, Zone zone, Duration duration, void Function() timer) {
              final Timer result = parent.createTimer(zone, duration, timer);
              timers[result] = StackTrace.current;
              return result;
            },
            createPeriodicTimer: (Zone self, ZoneDelegate parent, Zone zone, Duration period, void Function(Timer) timer) {
              final Timer result = parent.createPeriodicTimer(zone, period, timer);
              timers[result] = StackTrace.current;
              return result;
140
            },
141 142 143 144 145 146 147 148
          ),
          body: () async {
            Cache.flutterRoot = '';
            if (_setup != null) {
              await _setup();
            }
            await test();
            Cache.flutterRoot = originalFlutterRoot;
149
            for (final MapEntry<Timer, StackTrace> entry in timers.entries) {
150 151 152 153 154 155 156
              if (entry.key.isActive) {
                throw StateError('A Timer was active at the end of a test: ${entry.value}');
              }
            }
            return null;
          });
      });
157
    }, createHttpClient: (SecurityContext c) => FakeHttpClient.any());
158
  }
159
}