flutter_command_runner_test.dart 12.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
import 'package:file/memory.dart';
8
import 'package:flutter_tools/src/base/bot_detector.dart';
9
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/io.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/base/terminal.dart';
13
import 'package:flutter_tools/src/cache.dart';
14
import 'package:flutter_tools/src/globals.dart' as globals;
15 16
import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';
17
import 'package:flutter_tools/src/version.dart';
18

19
import '../../src/context.dart';
20
import '../../src/fakes.dart';
21
import '../../src/test_flutter_command_runner.dart';
22
import 'utils.dart';
23

24 25 26
const String _kFlutterRoot = '/flutter/flutter';
const String _kProjectRoot = '/project';

27 28
void main() {
  group('FlutterCommandRunner', () {
29
    MemoryFileSystem fileSystem;
30 31 32 33 34 35 36
    Platform platform;

    setUpAll(() {
      Cache.disableLocking();
    });

    setUp(() {
37 38 39 40
      fileSystem = MemoryFileSystem.test();
      fileSystem.directory(_kFlutterRoot).createSync(recursive: true);
      fileSystem.directory(_kProjectRoot).createSync(recursive: true);
      fileSystem.currentDirectory = _kProjectRoot;
41

42 43 44 45 46 47
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
        },
        version: '1 2 3 4 5',
      );
48 49 50 51
    });

    group('run', () {
      testUsingContext('checks that Flutter installation is up-to-date', () async {
52
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
53
        final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;
54

55 56
        await runner.run(<String>['dummy']);

57
        expect(version.didCheckFlutterVersionFreshness, true);
58
      }, overrides: <Type, Generator>{
59
        FileSystem: () => fileSystem,
60
        ProcessManager: () => FakeProcessManager.any(),
61
        Platform: () => platform,
62
        FlutterVersion: () => FakeFlutterVersion(),
63
        BotDetector: () => const FakeBotDetector(false),
64
        OutputPreferences: () => OutputPreferences.test(),
65
      });
66

67
      testUsingContext('does not check that Flutter installation is up-to-date with --machine flag', () async {
68
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
69
        final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;
70 71 72

        await runner.run(<String>['dummy', '--machine', '--version']);

73
        expect(version.didCheckFlutterVersionFreshness, false);
74
      }, overrides: <Type, Generator>{
75
        FileSystem: () => fileSystem,
76 77
        ProcessManager: () => FakeProcessManager.any(),
        Platform: () => platform,
78
        FlutterVersion: () => FakeFlutterVersion(),
79
        OutputPreferences: () => OutputPreferences.test(),
80
      });
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
      testUsingContext('does not check that Flutter installation is up-to-date with CI=true in environment', () async {
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
        final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;

        await runner.run(<String>['dummy', '--version']);

        expect(version.didCheckFlutterVersionFreshness, false);
      }, overrides: <Type, Generator>{
        FileSystem: () => fileSystem,
        ProcessManager: () => FakeProcessManager.any(),
        Platform: () => platform,
        BotDetector: () => const FakeBotDetector(true),
      }, initializeFlutterRoot: false);

      testUsingContext('checks that Flutter installation is up-to-date with CI=true and --machine when explicit --version-check', () async {
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
        final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;

        await runner.run(<String>['dummy', '--version', '--machine', '--version-check']);

         expect(version.didCheckFlutterVersionFreshness, true);
      }, overrides: <Type, Generator>{
        FileSystem: () => fileSystem,
        ProcessManager: () => FakeProcessManager.any(),
        Platform: () => platform,
        BotDetector: () => const FakeBotDetector(true),
      }, initializeFlutterRoot: false);

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
      testUsingContext('checks that Flutter installation is up-to-date if shell completion to terminal', () async {
        final FlutterCommand command = DummyFlutterCommand(name: 'bash-completion');
        final FlutterCommandRunner runner = createTestCommandRunner(command) as FlutterCommandRunner;
        final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;

        await runner.run(<String>['bash-completion']);

        expect(version.didCheckFlutterVersionFreshness, true);
      }, overrides: <Type, Generator>{
        FileSystem: () => fileSystem,
        ProcessManager: () => FakeProcessManager.any(),
        Platform: () => platform,
        FlutterVersion: () => FakeFlutterVersion(),
        BotDetector: () => const FakeBotDetector(false),
        Stdio: () => FakeStdio(hasFakeTerminal: true),
      });

      testUsingContext('does not check that Flutter installation is up-to-date if redirecting shell completion', () async {
        final FlutterCommand command = DummyFlutterCommand(name: 'bash-completion');
        final FlutterCommandRunner runner = createTestCommandRunner(command) as FlutterCommandRunner;
        final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;

        await runner.run(<String>['bash-completion']);

        expect(version.didCheckFlutterVersionFreshness, false);
      }, overrides: <Type, Generator>{
        FileSystem: () => fileSystem,
        ProcessManager: () => FakeProcessManager.any(),
        Platform: () => platform,
        FlutterVersion: () => FakeFlutterVersion(),
        BotDetector: () => const FakeBotDetector(false),
        Stdio: () => FakeStdio(hasFakeTerminal: false),
      });

144
      testUsingContext('Fetches tags when --version is used', () async {
145
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
146
        final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;
147 148 149

        await runner.run(<String>['--version']);

150
        expect(version.didFetchTagsAndUpdate, true);
151
      }, overrides: <Type, Generator>{
152
        FileSystem: () => fileSystem,
153 154
        ProcessManager: () => FakeProcessManager.any(),
        Platform: () => platform,
155
        FlutterVersion: () => FakeFlutterVersion(),
156
        OutputPreferences: () => OutputPreferences.test(),
157
      });
158

159
    testUsingContext("Doesn't crash on invalid .packages file", () async {
160
      final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
161 162
      fileSystem.file('pubspec.yaml').createSync();
      fileSystem.file('.packages')
163 164 165 166 167 168
        ..createSync()
        ..writeAsStringSync('Not a valid package');

      await runner.run(<String>['dummy']);

    }, overrides: <Type, Generator>{
169
      FileSystem: () => fileSystem,
170
      ProcessManager: () => FakeProcessManager.any(),
171
      Platform: () => platform,
172
      OutputPreferences: () => OutputPreferences.test(),
173 174
    });

175
    group('getRepoPackages', () {
Dan Field's avatar
Dan Field committed
176 177
      String oldFlutterRoot;

178
      setUp(() {
Dan Field's avatar
Dan Field committed
179 180
        oldFlutterRoot = Cache.flutterRoot;
        Cache.flutterRoot = _kFlutterRoot;
181
        fileSystem.directory(fileSystem.path.join(_kFlutterRoot, 'examples'))
182
            .createSync(recursive: true);
183
        fileSystem.directory(fileSystem.path.join(_kFlutterRoot, 'packages'))
184
            .createSync(recursive: true);
185
        fileSystem.directory(fileSystem.path.join(_kFlutterRoot, 'dev', 'tools', 'aatool'))
186
            .createSync(recursive: true);
187

188
        fileSystem.file(fileSystem.path.join(_kFlutterRoot, 'dev', 'tools', 'pubspec.yaml'))
189
            .createSync();
190
        fileSystem.file(fileSystem.path.join(_kFlutterRoot, 'dev', 'tools', 'aatool', 'pubspec.yaml'))
191 192
            .createSync();
      });
193

Dan Field's avatar
Dan Field committed
194 195 196 197
      tearDown(() {
        Cache.flutterRoot = oldFlutterRoot;
      });

198
      testUsingContext('', () {
199
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
200
        final List<String> packagePaths = runner.getRepoPackages()
201
          .map((Directory d) => d.path).toList();
202
        expect(packagePaths, <String>[
203 204
          fileSystem.directory(fileSystem.path.join(_kFlutterRoot, 'dev', 'tools', 'aatool')).path,
          fileSystem.directory(fileSystem.path.join(_kFlutterRoot, 'dev', 'tools')).path,
205 206
        ]);
      }, overrides: <Type, Generator>{
207
        FileSystem: () => fileSystem,
208
        ProcessManager: () => FakeProcessManager.any(),
209
        Platform: () => platform,
210
        FlutterVersion: () => FakeFlutterVersion(),
211
        OutputPreferences: () => OutputPreferences.test(),
212
      });
213
    });
214 215 216

    group('wrapping', () {
      testUsingContext('checks that output wrapping is turned on when writing to a terminal', () async {
217
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
218 219 220 221 222
        final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
        runner.addCommand(fakeCommand);
        await runner.run(<String>['fake']);
        expect(fakeCommand.preferences.wrapText, isTrue);
      }, overrides: <Type, Generator>{
223
        FileSystem: () => fileSystem,
224 225
        ProcessManager: () => FakeProcessManager.any(),
        Stdio: () => FakeStdio(hasFakeTerminal: true),
226
        OutputPreferences: () => OutputPreferences.test(),
227 228 229
      }, initializeFlutterRoot: false);

      testUsingContext('checks that output wrapping is turned off when not writing to a terminal', () async {
230
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
231 232 233 234 235
        final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
        runner.addCommand(fakeCommand);
        await runner.run(<String>['fake']);
        expect(fakeCommand.preferences.wrapText, isFalse);
      }, overrides: <Type, Generator>{
236
        FileSystem: () => fileSystem,
237 238
        ProcessManager: () => FakeProcessManager.any(),
        Stdio: () => FakeStdio(hasFakeTerminal: false),
239
        OutputPreferences: () => OutputPreferences.test(),
240 241 242
      }, initializeFlutterRoot: false);

      testUsingContext('checks that output wrapping is turned off when set on the command line and writing to a terminal', () async {
243
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
244 245 246 247 248
        final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
        runner.addCommand(fakeCommand);
        await runner.run(<String>['--no-wrap', 'fake']);
        expect(fakeCommand.preferences.wrapText, isFalse);
      }, overrides: <Type, Generator>{
249
        FileSystem: () => fileSystem,
250 251
        ProcessManager: () => FakeProcessManager.any(),
        Stdio: () => FakeStdio(hasFakeTerminal: true),
252
        OutputPreferences: () => OutputPreferences.test(),
253 254 255
      }, initializeFlutterRoot: false);

      testUsingContext('checks that output wrapping is turned on when set on the command line, but not writing to a terminal', () async {
256
        final FlutterCommandRunner runner = createTestCommandRunner(DummyFlutterCommand()) as FlutterCommandRunner;
257 258 259 260 261
        final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
        runner.addCommand(fakeCommand);
        await runner.run(<String>['--wrap', 'fake']);
        expect(fakeCommand.preferences.wrapText, isTrue);
      }, overrides: <Type, Generator>{
262
        FileSystem: () => fileSystem,
263 264
        ProcessManager: () => FakeProcessManager.any(),
        Stdio: () => FakeStdio(hasFakeTerminal: false),
265
        OutputPreferences: () => OutputPreferences.test(),
266 267
      }, initializeFlutterRoot: false);
    });
268
  });
269
  });
270
}
271

272
class FakeFlutterCommand extends FlutterCommand {
273 274 275 276
  OutputPreferences preferences;

  @override
  Future<FlutterCommandResult> runCommand() {
277
    preferences = globals.outputPreferences;
278 279 280 281 282 283 284 285 286
    return Future<FlutterCommandResult>.value(const FlutterCommandResult(ExitStatus.success));
  }

  @override
  String get description => null;

  @override
  String get name => 'fake';
}
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

class FakeStdio extends Stdio {
  FakeStdio({this.hasFakeTerminal});

  final bool hasFakeTerminal;

  @override
  bool get hasTerminal => hasFakeTerminal;

  @override
  int get terminalColumns => hasFakeTerminal ? 80 : null;

  @override
  int get terminalLines => hasFakeTerminal ? 24 : null;
  @override
  bool get supportsAnsiEscapes => hasFakeTerminal;
}