downgrade_test.dart 8.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/downgrade.dart';
import 'package:flutter_tools/src/persistent_tool_state.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';

import '../../src/common.dart';
import '../../src/context.dart';

void main() {
  FileSystem fileSystem;
  BufferLogger bufferLogger;
  AnsiTerminal terminal;
  ProcessManager processManager;
  MockStdio mockStdio;
  FlutterVersion flutterVersion;

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

  tearDownAll(() {
    Cache.enableLocking();
  });

  setUp(() {
    flutterVersion = MockFlutterVersion();
    mockStdio = MockStdio();
    processManager = FakeProcessManager.any();
    terminal = MockTerminal();
    fileSystem = MemoryFileSystem.test();
41
    bufferLogger = BufferLogger.test(terminal: terminal);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 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 144 145 146 147 148 149 150 151 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  });

  testUsingContext('Downgrade exits on unknown channel', () async {
    fileSystem.currentDirectory.childFile('.flutter_tool_state')
      .writeAsStringSync('{"last-active-master-version":"invalid"}');
    final DowngradeCommand command = DowngradeCommand(
      persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
      processManager: processManager,
      terminal: terminal,
      stdio: mockStdio,
      flutterVersion: flutterVersion,
      logger: bufferLogger,
    );

    expect(createTestCommandRunner(command).run(const <String>['downgrade']),
      throwsToolExit(message: 'Flutter is not currently on a known channel.'));
  });

  testUsingContext('Downgrade exits on no recorded version', () async {
    when(flutterVersion.channel).thenReturn('dev');
    fileSystem.currentDirectory.childFile('.flutter_tool_state')
      .writeAsStringSync('{"last-active-master-version":"abcd"}');
    final DowngradeCommand command = DowngradeCommand(
      persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>[
            'git', 'describe', '--tags', 'abcd'
          ],
          exitCode: 0,
          stdout: 'v1.2.3'
        )
      ]),
      terminal: terminal,
      stdio: mockStdio,
      flutterVersion: flutterVersion,
      logger: bufferLogger,
    );

    expect(createTestCommandRunner(command).run(const <String>['downgrade']),
      throwsToolExit(message:
        'There is no previously recorded version for channel "dev".\n'
        'Channel "master" was previously on: v1.2.3.'
      ),
    );
  });

  testUsingContext('Downgrade exits on unknown recorded version', () async {
    when(flutterVersion.channel).thenReturn('master');
    fileSystem.currentDirectory.childFile('.flutter_tool_state')
      .writeAsStringSync('{"last-active-master-version":"invalid"}');
    final DowngradeCommand command = DowngradeCommand(
      persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>[
            'git', 'describe', '--tags', 'invalid'
          ],
          exitCode: 1,
        )
      ]),
      terminal: terminal,
      stdio: mockStdio,
      flutterVersion: flutterVersion,
      logger: bufferLogger,
    );

    expect(createTestCommandRunner(command).run(const <String>['downgrade']),
      throwsToolExit(message: 'Failed to parse version for downgrade'));
  });

   testUsingContext('Downgrade prompts for user input when terminal is attached - y', () async {
    when(flutterVersion.channel).thenReturn('master');
    when(mockStdio.hasTerminal).thenReturn(true);
    fileSystem.currentDirectory.childFile('.flutter_tool_state')
      .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
    final DowngradeCommand command = DowngradeCommand(
      persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
      processManager: processManager,
      terminal: terminal,
      stdio: mockStdio,
      flutterVersion: flutterVersion,
      logger: bufferLogger,
    );

    when(terminal.promptForCharInput(
      const <String>['y', 'n'],
      prompt: anyNamed('prompt'),
      logger: anyNamed('logger'),
    )).thenAnswer((Invocation invocation) async => 'y');

    await createTestCommandRunner(command).run(const <String>['downgrade']);

    verify(terminal.promptForCharInput(
      const <String>['y', 'n'],
      prompt: anyNamed('prompt'),
      logger: anyNamed('logger'),
    )).called(1);
    expect(bufferLogger.statusText, contains('Success'));
  });

   testUsingContext('Downgrade prompts for user input when terminal is attached - n', () async {
    when(flutterVersion.channel).thenReturn('master');
    when(mockStdio.hasTerminal).thenReturn(true);
    fileSystem.currentDirectory.childFile('.flutter_tool_state')
      .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
    final DowngradeCommand command = DowngradeCommand(
      persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
      processManager: processManager,
      terminal: terminal,
      stdio: mockStdio,
      flutterVersion: flutterVersion,
      logger: bufferLogger,
    );

    when(terminal.promptForCharInput(
      const <String>['y', 'n'],
      prompt: anyNamed('prompt'),
      logger: anyNamed('logger'),
    )).thenAnswer((Invocation invocation) async => 'n');

    await createTestCommandRunner(command).run(const <String>['downgrade']);

    verify(terminal.promptForCharInput(
      const <String>['y', 'n'],
      prompt: anyNamed('prompt'),
      logger: anyNamed('logger'),
    )).called(1);
    expect(bufferLogger.statusText, isNot(contains('Success')));
  });

  testUsingContext('Downgrade does not prompt when there is no terminal', () async {
    when(flutterVersion.channel).thenReturn('master');
    when(mockStdio.hasTerminal).thenReturn(false);
    fileSystem.currentDirectory.childFile('.flutter_tool_state')
      .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
    final DowngradeCommand command = DowngradeCommand(
      persistentToolState: PersistentToolState.test(
        directory: fileSystem.currentDirectory,
        logger: bufferLogger,
      ),
      processManager: processManager,
      terminal: terminal,
      stdio: mockStdio,
      flutterVersion: flutterVersion,
      logger: bufferLogger,
    );

    await createTestCommandRunner(command).run(const <String>['downgrade']);

    verifyNever(terminal.promptForCharInput(
      const <String>['y', 'n'],
      prompt: anyNamed('prompt'),
      logger: anyNamed('logger'),
    ));
    expect(bufferLogger.statusText, contains('Success'));
  });

  testUsingContext('Downgrade performs correct git commands', () async {
    when(flutterVersion.channel).thenReturn('master');
    when(mockStdio.hasTerminal).thenReturn(false);
    fileSystem.currentDirectory.childFile('.flutter_tool_state')
      .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
    final DowngradeCommand command = DowngradeCommand(
      persistentToolState: PersistentToolState.test(
        directory: fileSystem.currentDirectory,
        logger: bufferLogger,
      ),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>[
            'git', 'describe', '--tags', 'g6b00b5e88'
          ],
          stdout: 'v1.2.3',
        ),
        const FakeCommand(
          command: <String>[
            'git', 'reset', '--hard', 'g6b00b5e88'
          ],
        ),
        const FakeCommand(
          command: <String>[
            'git', 'checkout', 'master', '--'
          ]
        ),
      ]),
      terminal: terminal,
      stdio: mockStdio,
      flutterVersion: flutterVersion,
      logger: bufferLogger,
    );

    await createTestCommandRunner(command).run(const <String>['downgrade']);

    expect(bufferLogger.statusText, contains('Success'));
  });
}

class MockTerminal extends Mock implements AnsiTerminal {}
class MockStdio extends Mock implements Stdio {}