upgrade_test.dart 9.29 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium 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:flutter_tools/src/base/common.dart';
6
import 'package:flutter_tools/src/base/file_system.dart';
7
import 'package:flutter_tools/src/base/io.dart';
8
import 'package:flutter_tools/src/base/platform.dart';
9
import 'package:flutter_tools/src/base/os.dart';
10
import 'package:flutter_tools/src/cache.dart';
11
import 'package:flutter_tools/src/commands/upgrade.dart';
12 13 14
import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';
15
import 'package:platform/platform.dart';
16
import 'package:process/process.dart';
17

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

22
void main() {
23 24 25 26
  group('UpgradeCommandRunner', () {
    FakeUpgradeCommandRunner fakeCommandRunner;
    UpgradeCommandRunner realCommandRunner;
    MockProcessManager processManager;
27
    FakePlatform fakePlatform;
28 29 30 31 32 33 34 35
    final MockFlutterVersion flutterVersion = MockFlutterVersion();
    const GitTagVersion gitTagVersion = GitTagVersion(1, 2, 3, 4, 5, 'asd');
    when(flutterVersion.channel).thenReturn('dev');

    setUp(() {
      fakeCommandRunner = FakeUpgradeCommandRunner();
      realCommandRunner = UpgradeCommandRunner();
      processManager = MockProcessManager();
36 37 38 39 40 41 42 43 44 45 46 47
      when(processManager.start(
        <String>[
          fs.path.join('bin', 'flutter'),
          'upgrade',
          '--continue',
          '--no-version-check',
        ],
        environment: anyNamed('environment'),
        workingDirectory: anyNamed('workingDirectory'),
      )).thenAnswer((Invocation invocation) async {
        return Future<Process>.value(createMockProcess());
      });
48
      fakeCommandRunner.willHaveUncomittedChanges = false;
49
      fakePlatform = FakePlatform()..environment = Map<String, String>.unmodifiable(<String, String>{
50 51
        'ENV1': 'irrelevant',
        'ENV2': 'irrelevant',
52
      });
53 54
    });

55
    testUsingContext('throws on unknown tag, official branch,  noforce', () async {
56
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
57
        false,
58 59 60 61 62
        false,
        const GitTagVersion.unknown(),
        flutterVersion,
      );
      expect(result, throwsA(isInstanceOf<ToolExit>()));
63 64
    }, overrides: <Type, Generator>{
      Platform: () => fakePlatform,
65 66
    });

67
    testUsingContext('does not throw on unknown tag, official branch, force', () async {
68 69
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
        true,
70
        false,
71 72 73 74
        const GitTagVersion.unknown(),
        flutterVersion,
      );
      expect(await result, null);
75 76
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
77
      Platform: () => fakePlatform,
78 79
    });

80
    testUsingContext('throws tool exit with uncommitted changes', () async {
81 82
      fakeCommandRunner.willHaveUncomittedChanges = true;
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
83
        false,
84 85 86 87 88
        false,
        gitTagVersion,
        flutterVersion,
      );
      expect(result, throwsA(isA<ToolExit>()));
89 90
    }, overrides: <Type, Generator>{
      Platform: () => fakePlatform,
91 92
    });

93
    testUsingContext('does not throw tool exit with uncommitted changes and force', () async {
94
      fakeCommandRunner.willHaveUncomittedChanges = true;
95

96 97
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
        true,
98
        false,
99 100 101 102
        gitTagVersion,
        flutterVersion,
      );
      expect(await result, null);
103 104
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
105
      Platform: () => fakePlatform,
106 107
    });

108
    testUsingContext('Doesn\'t throw on known tag, dev branch, no force', () async {
109
      final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
110
        false,
111 112 113 114 115
        false,
        gitTagVersion,
        flutterVersion,
      );
      expect(await result, null);
116 117
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
118
      Platform: () => fakePlatform,
119 120 121 122 123 124
    });

    testUsingContext('verifyUpstreamConfigured', () async {
      when(processManager.run(
        <String>['git', 'rev-parse', '@{u}'],
        environment:anyNamed('environment'),
125
        workingDirectory: anyNamed('workingDirectory')),
126 127 128 129 130 131 132
      ).thenAnswer((Invocation invocation) async {
        return FakeProcessResult()
          ..exitCode = 0;
      });
      await realCommandRunner.verifyUpstreamConfigured();
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      Platform: () => fakePlatform,
    });

    testUsingContext('flutterUpgradeContinue passes env variables to child process', () async {
      await realCommandRunner.flutterUpgradeContinue();

      final VerificationResult result = verify(processManager.start(
        <String>[
          fs.path.join('bin', 'flutter'),
          'upgrade',
          '--continue',
          '--no-version-check',
        ],
        environment: captureAnyNamed('environment'),
        workingDirectory: anyNamed('workingDirectory'),
      ));

150 151
      expect(result.captured.first,
          <String, String>{ 'FLUTTER_ALREADY_LOCKED': 'true', ...fakePlatform.environment });
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
    });

    testUsingContext('precacheArtifacts passes env variables to child process', () async {
      final List<String> precacheCommand = <String>[
        fs.path.join('bin', 'flutter'),
        '--no-color',
        '--no-version-check',
        'precache',
      ];

      when(processManager.start(
        precacheCommand,
        environment: anyNamed('environment'),
        workingDirectory: anyNamed('workingDirectory'),
      )).thenAnswer((Invocation invocation) async {
        return Future<Process>.value(createMockProcess());
      });

      await realCommandRunner.precacheArtifacts();

      final VerificationResult result = verify(processManager.start(
        precacheCommand,
        environment: captureAnyNamed('environment'),
        workingDirectory: anyNamed('workingDirectory'),
      ));

181 182
      expect(result.captured.first,
          <String, String>{ 'FLUTTER_ALREADY_LOCKED': 'true', ...fakePlatform.environment });
183 184 185
    }, overrides: <Type, Generator>{
      ProcessManager: () => processManager,
      Platform: () => fakePlatform,
186 187 188 189
    });
  });

  group('matchesGitLine', () {
190 191 192 193
    setUpAll(() {
      Cache.disableLocking();
    });

194
    bool _match(String line) => UpgradeCommandRunner.matchesGitLine(line);
195 196

    test('regex match', () {
197
      expect(_match(' .../flutter_gallery/lib/demo/buttons_demo.dart    | 10 +--'), true);
198 199 200 201
      expect(_match(' dev/benchmarks/complex_layout/lib/main.dart        |  24 +-'), true);

      expect(_match(' rename {packages/flutter/doc => dev/docs}/styles.html (92%)'), true);
      expect(_match(' delete mode 100644 doc/index.html'), true);
202
      expect(_match(' create mode 100644 examples/flutter_gallery/lib/gallery/demo.dart'), true);
203 204 205 206 207 208 209 210

      expect(_match('Fast-forward'), true);
    });

    test('regex doesn\'t match', () {
      expect(_match('Updating 79cfe1e..5046107'), false);
      expect(_match('229 files changed, 6179 insertions(+), 3065 deletions(-)'), false);
    });
211 212

    group('findProjectRoot', () {
213
      Directory tempDir;
214 215

      setUp(() async {
216
        tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_upgrade_test.');
217 218 219
      });

      tearDown(() {
220
        tryToDelete(tempDir);
221 222 223
      });

      testUsingContext('in project', () async {
224
        final String projectPath = await createProject(tempDir);
225 226
        expect(findProjectRoot(projectPath), projectPath);
        expect(findProjectRoot(fs.path.join(projectPath, 'lib')), projectPath);
227

228
        final String hello = fs.path.join(Cache.flutterRoot, 'examples', 'hello_world');
229
        expect(findProjectRoot(hello), hello);
230
        expect(findProjectRoot(fs.path.join(hello, 'lib')), hello);
231 232 233
      });

      testUsingContext('outside project', () async {
234
        final String projectPath = await createProject(tempDir);
235
        expect(findProjectRoot(fs.directory(projectPath).parent.path), null);
236
        expect(findProjectRoot(Cache.flutterRoot), null);
237 238
      });
    });
239 240
  });
}
241 242

class FakeUpgradeCommandRunner extends UpgradeCommandRunner {
243 244
  bool willHaveUncomittedChanges = false;

245 246 247
  @override
  Future<void> verifyUpstreamConfigured() async {}

248 249 250
  @override
  Future<bool> hasUncomittedChanges() async => willHaveUncomittedChanges;

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  @override
  Future<void> resetChanges(GitTagVersion gitTagVersion) async {}

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

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

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

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

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

class MockFlutterVersion extends Mock implements FlutterVersion {}
271
class MockProcess extends Mock implements Process {}
272 273 274 275 276 277 278 279 280 281 282 283 284 285
class MockProcessManager extends Mock implements ProcessManager {}
class FakeProcessResult implements ProcessResult {
  @override
  int exitCode;

  @override
  int pid = 0;

  @override
  String stderr = '';

  @override
  String stdout = '';
}