upgrade_test.dart 13.2 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
import 'package:flutter_tools/src/base/file_system.dart';
6
import 'package:flutter_tools/src/base/io.dart';
7
import 'package:flutter_tools/src/base/platform.dart';
8
import 'package:flutter_tools/src/cache.dart';
9
import 'package:flutter_tools/src/commands/upgrade.dart';
10
import 'package:flutter_tools/src/convert.dart';
11
import 'package:flutter_tools/src/globals.dart' as globals;
12
import 'package:flutter_tools/src/persistent_tool_state.dart';
13 14 15 16
import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
17

18 19
import '../../src/common.dart';
import '../../src/context.dart';
20
import '../../src/fake_process_manager.dart';
21
import '../../src/mocks.dart';
22

23
void main() {
24 25 26
  group('UpgradeCommandRunner', () {
    FakeUpgradeCommandRunner fakeCommandRunner;
    UpgradeCommandRunner realCommandRunner;
27
    FakeProcessManager processManager;
28
    FakePlatform fakePlatform;
29
    final MockFlutterVersion flutterVersion = MockFlutterVersion();
30 31 32 33 34 35 36 37
    const GitTagVersion gitTagVersion = GitTagVersion(
      x: 1,
      y: 2,
      z: 3,
      hotfix: 4,
      commits: 5,
      hash: 'asd',
    );
38 39 40 41 42
    when(flutterVersion.channel).thenReturn('dev');

    setUp(() {
      fakeCommandRunner = FakeUpgradeCommandRunner();
      realCommandRunner = UpgradeCommandRunner();
43
      processManager = FakeProcessManager.list(<FakeCommand>[]);
44
      fakeCommandRunner.willHaveUncomittedChanges = false;
45
      fakePlatform = FakePlatform()..environment = Map<String, String>.unmodifiable(<String, String>{
46 47
        'ENV1': 'irrelevant',
        'ENV2': 'irrelevant',
48
      });
49 50
    });

51
    testUsingContext('throws on unknown tag, official branch,  noforce', () async {
52
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
53 54 55 56 57
        force: false,
        continueFlow: false,
        testFlow: false,
        gitTagVersion: const GitTagVersion.unknown(),
        flutterVersion: flutterVersion,
58
      );
Dan Field's avatar
Dan Field committed
59
      expect(result, throwsToolExit());
60
      expect(processManager.hasRemainingExpectations, isFalse);
61 62
    }, overrides: <Type, Generator>{
      Platform: () => fakePlatform,
63 64
    });

65
    testUsingContext('throws tool exit with uncommitted changes', () async {
66 67
      fakeCommandRunner.willHaveUncomittedChanges = true;
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
68 69 70 71 72
        force: false,
        continueFlow: false,
        testFlow: false,
        gitTagVersion: gitTagVersion,
        flutterVersion: flutterVersion,
73
      );
Dan Field's avatar
Dan Field committed
74
      expect(result, throwsToolExit());
75
      expect(processManager.hasRemainingExpectations, isFalse);
76 77
    }, overrides: <Type, Generator>{
      Platform: () => fakePlatform,
78 79
    });

80
    testUsingContext("Doesn't continue on known tag, dev branch, no force, already up-to-date", () async {
81 82
      const String revision = 'abc123';
      when(flutterVersion.frameworkRevision).thenReturn(revision);
83
      fakeCommandRunner.alreadyUpToDate = true;
84
      fakeCommandRunner.remoteRevision = revision;
85
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
86 87 88 89 90
        force: false,
        continueFlow: false,
        testFlow: false,
        gitTagVersion: gitTagVersion,
        flutterVersion: flutterVersion,
91
      );
92
      expect(await result, FlutterCommandResult.success());
93
      expect(testLogger.statusText, contains('Flutter is already up to date'));
94
      expect(processManager.hasRemainingExpectations, isFalse);
95 96 97 98 99
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
    });

100
    testUsingContext('fetchRemoteRevision returns revision if git succeeds', () async {
101
      const String revision = 'abc123';
102 103 104 105 106 107 108 109 110 111 112

      processManager.addCommands(<FakeCommand>[
        const FakeCommand(command: <String>[
          'git', 'fetch', '--tags'
        ]),
        const FakeCommand(command: <String>[
          'git', 'rev-parse', '--verify', '@{u}',
        ],
        stdout: revision),
      ]);

113
      expect(await realCommandRunner.fetchRemoteRevision(), revision);
114
      expect(processManager.hasRemainingExpectations, isFalse);
115 116
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
117 118 119
      Platform: () => fakePlatform,
    });

120
    testUsingContext('fetchRemoteRevision throws toolExit if HEAD is detached', () async {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      processManager.addCommands(<FakeCommand>[
        const FakeCommand(command: <String>[
          'git', 'fetch', '--tags'
        ]),
        FakeCommand(
          command: const <String>['git', 'rev-parse', '--verify', '@{u}'],
          onRun: () {
            throw const ProcessException(
              'git',
              <String>['rev-parse', '--verify', '@{u}'],
              'fatal: HEAD does not point to a branch',
            );
          }
        ),
      ]);

      await expectLater(
            () async => await realCommandRunner.fetchRemoteRevision(),
139 140
        throwsToolExit(message: 'You are not currently on a release branch.'),
      );
141
      expect(processManager.hasRemainingExpectations, isFalse);
142 143 144 145 146 147
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
    });

    testUsingContext('fetchRemoteRevision throws toolExit if no upstream configured', () async {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      processManager.addCommands(<FakeCommand>[
        const FakeCommand(command: <String>[
          'git', 'fetch', '--tags'
        ]),
        FakeCommand(
          command: const <String>['git', 'rev-parse', '--verify', '@{u}'],
          onRun: () {
            throw const ProcessException(
              'git',
              <String>['rev-parse', '--verify', '@{u}'],
              'fatal: no upstream configured for branch',
            );
          },
        ),
      ]);

      await expectLater(
            () async => await realCommandRunner.fetchRemoteRevision(),
166 167 168 169
        throwsToolExit(
          message: 'Unable to upgrade Flutter: no origin repository configured\.',
        ),
      );
170
      expect(processManager.hasRemainingExpectations, isFalse);
171 172 173 174 175
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
    });

176 177 178
    testUsingContext('git exception during attemptReset throwsToolExit', () async {
      const String revision = 'abc123';
      const String errorMessage = 'fatal: Could not parse object ´$revision´';
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      processManager.addCommands(<FakeCommand>[
        FakeCommand(
          command: const <String>['git', 'reset', '--hard', revision],
          onRun: () {
            throw const ProcessException(
              'git',
              <String>['reset', '--hard', revision],
              errorMessage,
            );
          },
        ),
      ]);

      await expectLater(
            () async => await realCommandRunner.attemptReset(revision),
194 195
        throwsToolExit(message: errorMessage),
      );
196
      expect(processManager.hasRemainingExpectations, isFalse);
197 198 199 200 201
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
    });

202
    testUsingContext('flutterUpgradeContinue passes env variables to child process', () async {
203 204 205 206 207 208 209 210 211 212 213
      processManager.addCommand(
        FakeCommand(
          command: <String>[
            globals.fs.path.join('bin', 'flutter'),
            'upgrade',
            '--continue',
            '--no-version-check',
          ],
          environment: <String, String>{'FLUTTER_ALREADY_LOCKED': 'true', ...fakePlatform.environment}
        ),
      );
214
      await realCommandRunner.flutterUpgradeContinue();
215
      expect(processManager.hasRemainingExpectations, isFalse);
216 217 218 219 220 221
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
    });

    testUsingContext('precacheArtifacts passes env variables to child process', () async {
222 223 224 225 226 227 228 229 230 231 232
      processManager.addCommand(
        FakeCommand(
          command: <String>[
            globals.fs.path.join('bin', 'flutter'),
            '--no-color',
            '--no-version-check',
            'precache',
          ],
          environment: <String, String>{'FLUTTER_ALREADY_LOCKED': 'true', ...fakePlatform.environment}
        ),
      );
233
      await realCommandRunner.precacheArtifacts();
234
      expect(processManager.hasRemainingExpectations, isFalse);
235 236 237
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
238
    });
239

240
    group('runs upgrade', () {
241
      setUp(() {
242 243 244 245 246 247 248 249
        processManager.addCommand(
          FakeCommand(command: <String>[
            globals.fs.path.join('bin', 'flutter'),
            'upgrade',
            '--continue',
            '--no-version-check',
          ]),
        );
250 251
      });

252 253 254 255 256 257 258 259 260 261 262 263 264
      testUsingContext('does not throw on unknown tag, official branch, force', () async {
        final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
          force: true,
          continueFlow: false,
          testFlow: false,
          gitTagVersion: const GitTagVersion.unknown(),
          flutterVersion: flutterVersion,
        );
        expect(await result, FlutterCommandResult.success());
        expect(processManager.hasRemainingExpectations, isFalse);
      }, overrides: <Type, Generator>{
        ProcessManager: () => processManager,
        Platform: () => fakePlatform,
265 266
      });

267 268
      testUsingContext('does not throw tool exit with uncommitted changes and force', () async {
        fakeCommandRunner.willHaveUncomittedChanges = true;
269

270 271 272 273 274 275
        final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
          force: true,
          continueFlow: false,
          testFlow: false,
          gitTagVersion: gitTagVersion,
          flutterVersion: flutterVersion,
276
        );
277 278 279 280 281 282
        expect(await result, FlutterCommandResult.success());
        expect(processManager.hasRemainingExpectations, isFalse);
      }, overrides: <Type, Generator>{
        ProcessManager: () => processManager,
        Platform: () => fakePlatform,
      });
283

284 285 286 287 288 289 290
      testUsingContext("Doesn't throw on known tag, dev branch, no force", () async {
        final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
          force: false,
          continueFlow: false,
          testFlow: false,
          gitTagVersion: gitTagVersion,
          flutterVersion: flutterVersion,
291
        );
292 293
        expect(await result, FlutterCommandResult.success());
        expect(processManager.hasRemainingExpectations, isFalse);
294
      }, overrides: <Type, Generator>{
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        ProcessManager: () => processManager,
        Platform: () => fakePlatform,
      });

      group('full command', () {
        FakeProcessManager fakeProcessManager;
        Directory tempDir;
        File flutterToolState;

        FlutterVersion mockFlutterVersion;

        setUp(() {
          Cache.disableLocking();
          fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
            const FakeCommand(
              command: <String>[
                'git', 'tag', '--points-at', 'HEAD',
              ],
              stdout: '',
            ),
            const FakeCommand(
              command: <String>[
                'git', 'describe', '--match', '*.*.*', '--first-parent', '--long', '--tags',
              ],
              stdout: 'v1.12.16-19-gb45b676af',
            ),
          ]);
          tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_upgrade_test.');
          flutterToolState = tempDir.childFile('.flutter_tool_state');
          mockFlutterVersion = MockFlutterVersion(isStable: true);
        });

        tearDown(() {
          Cache.enableLocking();
          tryToDelete(tempDir);
        });

        testUsingContext('upgrade continue prints welcome message', () async {
          final UpgradeCommand upgradeCommand = UpgradeCommand(fakeCommandRunner);
          applyMocksToCommand(upgradeCommand);

          await createTestCommandRunner(upgradeCommand).run(
            <String>[
              'upgrade',
              '--continue',
            ],
          );

          expect(
            json.decode(flutterToolState.readAsStringSync()),
            containsPair('redisplay-welcome-message', true),
          );
        }, overrides: <Type, Generator>{
          FlutterVersion: () => mockFlutterVersion,
          ProcessManager: () => fakeProcessManager,
          PersistentToolState: () => PersistentToolState.test(
            directory: tempDir,
            logger: testLogger,
          ),
        });
355 356
      });
    });
357

358
  });
359
}
360 361

class FakeUpgradeCommandRunner extends UpgradeCommandRunner {
362 363
  bool willHaveUncomittedChanges = false;

364 365
  bool alreadyUpToDate = false;

366 367
  String remoteRevision = '';

368
  @override
369
  Future<String> fetchRemoteRevision() async => remoteRevision;
370

371 372 373
  @override
  Future<bool> hasUncomittedChanges() async => willHaveUncomittedChanges;

374 375 376 377
  @override
  Future<void> upgradeChannel(FlutterVersion flutterVersion) async {}

  @override
378
  Future<void> attemptReset(String newRevision) async {}
379 380 381 382 383 384 385 386 387 388

  @override
  Future<void> precacheArtifacts() async {}

  @override
  Future<void> updatePackages(FlutterVersion flutterVersion) async {}

  @override
  Future<void> runDoctor() async {}
}