channel_test.dart 9.89 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:args/command_runner.dart';
8 9
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/cache.dart';
11
import 'package:flutter_tools/src/globals.dart' as globals;
12
import 'package:flutter_tools/src/commands/channel.dart';
13
import 'package:flutter_tools/src/version.dart';
14

15 16
import '../src/common.dart';
import '../src/context.dart';
17

18 19
void main() {
  group('channel', () {
20 21 22 23 24
    FakeProcessManager fakeProcessManager;

    setUp(() {
      fakeProcessManager = FakeProcessManager.list(<FakeCommand>[]);
    });
25

26 27 28 29
    setUpAll(() {
      Cache.disableLocking();
    });

30
    Future<void> simpleChannelTest(List<String> args) async {
31
      final ChannelCommand command = ChannelCommand();
32
      final CommandRunner<void> runner = createTestCommandRunner(command);
33
      await runner.run(args);
34
      expect(testLogger.errorText, hasLength(0));
35 36 37
      // The bots may return an empty list of channels (network hiccup?)
      // and when run locally the list of branches might be different
      // so we check for the header text rather than any specific channel name.
38 39 40 41
      expect(
        testLogger.statusText,
        containsIgnoringWhitespace('Flutter channels:'),
      );
42 43 44 45 46 47 48 49
    }

    testUsingContext('list', () async {
      await simpleChannelTest(<String>['channel']);
    });

    testUsingContext('verbose list', () async {
      await simpleChannelTest(<String>['channel', '-v']);
50
    });
51

52 53 54 55
    testUsingContext('sorted by stability', () async {
      final ChannelCommand command = ChannelCommand();
      final CommandRunner<void> runner = createTestCommandRunner(command);

56 57 58 59 60 61 62 63 64 65
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>['git', 'branch', '-r'],
          stdout: 'origin/beta\n'
              'origin/master\n'
              'origin/dev\n'
              'origin/stable\n',
        ),
      );

66
      await runner.run(<String>['channel']);
67
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
68 69 70 71 72
      expect(testLogger.errorText, hasLength(0));
      // format the status text for a simpler assertion.
      final Iterable<String> rows = testLogger.statusText
        .split('\n')
        .map((String line) => line.substring(2)); // remove '* ' or '  ' from output
73
      expect(rows, containsAllInOrder(kOfficialChannels));
74 75 76 77

      // clear buffer for next process
      testLogger.clear();

78 79 80 81 82 83 84 85 86 87 88 89 90
      // Extra branches.
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>['git', 'branch', '-r'],
          stdout: 'origin/beta\n'
              'origin/master\n'
              'origin/dependabot/bundler\n'
              'origin/dev\n'
              'origin/v1.4.5-hotfixes\n'
              'origin/stable\n',
        ),
      );

91
      await runner.run(<String>['channel']);
92 93
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
      expect(rows, containsAllInOrder(kOfficialChannels));
94 95 96 97 98
      expect(testLogger.errorText, hasLength(0));
      // format the status text for a simpler assertion.
      final Iterable<String> rows2 = testLogger.statusText
        .split('\n')
        .map((String line) => line.substring(2)); // remove '* ' or '  ' from output
99
      expect(rows2, containsAllInOrder(kOfficialChannels));
100 101 102 103

      // clear buffer for next process
      testLogger.clear();

104 105 106 107 108 109 110 111 112 113 114
      // Missing branches.
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>['git', 'branch', '-r'],
          stdout: 'origin/beta\n'
              'origin/dependabot/bundler\n'
              'origin/v1.4.5-hotfixes\n'
              'origin/stable\n',
        ),
      );

115
      await runner.run(<String>['channel']);
116
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
117 118 119 120
      expect(testLogger.errorText, hasLength(0));
      // check if available official channels are in order of stability
      int prev = -1;
      int next = -1;
121
      for (final String branch in kOfficialChannels) {
122 123 124 125 126 127 128 129
        next = testLogger.statusText.indexOf(branch);
        if (next != -1) {
          expect(prev < next, isTrue);
          prev = next;
        }
      }

    }, overrides: <Type, Generator>{
130 131
      ProcessManager: () => fakeProcessManager,
      FileSystem: () => MemoryFileSystem.test(),
132 133
    });

134
    testUsingContext('removes duplicates', () async {
135 136 137
      fakeProcessManager.addCommand(
        const FakeCommand(
          command: <String>['git', 'branch', '-r'],
138
          stdout: 'origin/dev\n'
139 140 141 142 143 144 145
              'origin/beta\n'
              'origin/stable\n'
              'upstream/dev\n'
              'upstream/beta\n'
              'upstream/stable\n',
        ),
      );
146

147
      final ChannelCommand command = ChannelCommand();
148
      final CommandRunner<void> runner = createTestCommandRunner(command);
149 150
      await runner.run(<String>['channel']);

151
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
152 153 154 155 156 157 158 159 160
      expect(testLogger.errorText, hasLength(0));

      // format the status text for a simpler assertion.
      final Iterable<String> rows = testLogger.statusText
        .split('\n')
        .map((String line) => line.trim())
        .where((String line) => line?.isNotEmpty == true)
        .skip(1); // remove `Flutter channels:` line

161
      expect(rows, <String>['dev', 'beta', 'stable']);
162
    }, overrides: <Type, Generator>{
163 164
      ProcessManager: () => fakeProcessManager,
      FileSystem: () => MemoryFileSystem.test(),
165 166 167
    });

    testUsingContext('can switch channels', () async {
168 169 170 171 172 173 174 175 176 177 178
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>['git', 'fetch'],
        ),
        const FakeCommand(
          command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/beta'],
        ),
        const FakeCommand(
            command: <String>['git', 'checkout', 'beta', '--']
        ),
      ]);
179

180
      final ChannelCommand command = ChannelCommand();
181
      final CommandRunner<void> runner = createTestCommandRunner(command);
182 183
      await runner.run(<String>['channel', 'beta']);

184
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
185 186 187 188
      expect(
        testLogger.statusText,
        containsIgnoringWhitespace("Switching to flutter channel 'beta'..."),
      );
189
      expect(testLogger.errorText, hasLength(0));
190

191 192 193 194 195 196 197 198 199 200 201
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>['git', 'fetch'],
        ),
        const FakeCommand(
          command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/stable'],
        ),
        const FakeCommand(
            command: <String>['git', 'checkout', 'stable', '--']
        ),
      ]);
202 203 204

      await runner.run(<String>['channel', 'stable']);

205
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
206
    }, overrides: <Type, Generator>{
207
      FileSystem: () => MemoryFileSystem.test(),
208
      ProcessManager: () => fakeProcessManager,
209 210
    });

211
    testUsingContext('switching channels prompts to run flutter upgrade', () async {
212 213 214 215 216 217 218 219 220 221 222
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>['git', 'fetch'],
        ),
        const FakeCommand(
          command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/beta'],
        ),
        const FakeCommand(
            command: <String>['git', 'checkout', 'beta', '--']
        ),
      ]);
223 224 225 226 227

      final ChannelCommand command = ChannelCommand();
      final CommandRunner<void> runner = createTestCommandRunner(command);
      await runner.run(<String>['channel', 'beta']);

228 229 230 231 232 233
      expect(
        testLogger.statusText,
        containsIgnoringWhitespace("Successfully switched to flutter channel 'beta'."),
      );
      expect(
        testLogger.statusText,
kwkr's avatar
kwkr committed
234 235
        containsIgnoringWhitespace(
          "To ensure that you're on the latest build "
236 237
          "from this channel, run 'flutter upgrade'"),
      );
238
      expect(testLogger.errorText, hasLength(0));
239
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
240
    }, overrides: <Type, Generator>{
241
      FileSystem: () => MemoryFileSystem.test(),
242
      ProcessManager: () => fakeProcessManager,
243 244
    });

245 246 247
    // This verifies that bug https://github.com/flutter/flutter/issues/21134
    // doesn't return.
    testUsingContext('removes version stamp file when switching channels', () async {
248 249 250 251 252 253 254 255 256 257 258
      fakeProcessManager.addCommands(<FakeCommand>[
        const FakeCommand(
          command: <String>['git', 'fetch'],
        ),
        const FakeCommand(
          command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/beta'],
        ),
        const FakeCommand(
          command: <String>['git', 'checkout', 'beta', '--']
        ),
      ]);
259

260
      final File versionCheckFile = globals.cache.getStampFileFor(
261
        VersionCheckStamp.flutterVersionCheckStampFile,
262 263 264 265 266 267 268 269 270 271 272 273
      );

      /// Create a bogus "leftover" version check file to make sure it gets
      /// removed when the channel changes. The content doesn't matter.
      versionCheckFile.createSync(recursive: true);
      versionCheckFile.writeAsStringSync('''
        {
          "lastTimeVersionWasChecked": "2151-08-29 10:17:30.763802",
          "lastKnownRemoteVersion": "2151-09-26 15:56:19.000Z"
        }
      ''');

274
      final ChannelCommand command = ChannelCommand();
275
      final CommandRunner<void> runner = createTestCommandRunner(command);
276 277 278 279 280
      await runner.run(<String>['channel', 'beta']);

      expect(testLogger.statusText, isNot(contains('A new version of Flutter')));
      expect(testLogger.errorText, hasLength(0));
      expect(versionCheckFile.existsSync(), isFalse);
281
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
282
    }, overrides: <Type, Generator>{
283
      FileSystem: () => MemoryFileSystem.test(),
284
      ProcessManager: () => fakeProcessManager,
285
    });
286 287
  });
}