candidates_test.dart 5.22 KB
Newer Older
1 2 3 4 5
// 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:args/command_runner.dart';
6 7
import 'package:conductor_core/src/candidates.dart';
import 'package:conductor_core/src/repository.dart';
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 41 42
import 'package:file/memory.dart';
import 'package:platform/platform.dart';

import './common.dart';

void main() {
  group('candidates command', () {
    const String flutterRoot = '/flutter';
    const String flutterBinPath = '$flutterRoot/bin/flutter';
    const String checkoutsParentDirectory = '$flutterRoot/dev/tools/';
    const String remoteName = 'origin';

    late MemoryFileSystem fileSystem;
    late FakePlatform platform;
    late TestStdio stdio;
    late FakeProcessManager processManager;
    final String operatingSystem = const LocalPlatform().operatingSystem;

    setUp(() {
      stdio = TestStdio();
      fileSystem = MemoryFileSystem.test();
    });

    CommandRunner<void> createRunner({
      required Checkouts checkouts,
    }) {
      final CandidatesCommand command = CandidatesCommand(
        checkouts: checkouts,
        flutterRoot: fileSystem.directory(flutterRoot),
      );
      return CommandRunner<void>('clean-test', '')..addCommand(command);
    }

    test('prints only branches from targeted remote', () async {
      const String currentVersion = '1.2.3';
43
      const String branch = 'flutter-1.3-candidate.0';
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

      processManager = FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['git', 'fetch', remoteName],
        ),
        const FakeCommand(
          command: <String>[flutterBinPath, 'help'],
        ),
        const FakeCommand(
          command: <String>[flutterBinPath, '--version', '--machine'],
          stdout: '{"frameworkVersion": "$currentVersion"}',
        ),
        FakeCommand(
          command: const <String>[
            'git',
            'branch',
            '--no-color',
            '--remotes',
            '--list',
            '$remoteName/*',
          ],
          stdout: <String>[
66
            'other-remote/flutter-5.0-candidate.0',
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            '$remoteName/$branch',
          ].join('\n'),
        ),
      ]);
      final String pathSeparator = operatingSystem == 'windows' ? r'\' : '/';

      platform = FakePlatform(
        environment: <String, String>{
          'HOME': <String>['path', 'to', 'home'].join(pathSeparator),
        },
        pathSeparator: pathSeparator,
      );
      final Checkouts checkouts = Checkouts(
        fileSystem: fileSystem,
        parentDirectory: fileSystem.directory(checkoutsParentDirectory),
        platform: platform,
        processManager: processManager,
        stdio: stdio,
      );

      final CommandRunner<void> runner = createRunner(checkouts: checkouts);
      await runner.run(<String>['candidates', '--$kRemote', remoteName]);
      expect(stdio.stdout.contains('currentVersion = $currentVersion'), true);
      expect(stdio.stdout.contains(branch), true);
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
      expect(stdio.stdout.contains('flutter-5.0-candidate.0'), false);
    });

    test('does not print branches older or equal to current version', () async {
      const String currentVersion = '2.3.0-13.0.pre.48';
      const String newBranch = 'flutter-2.4-candidate.0';
      const String oldBranch = 'flutter-1.0-candidate.0';
      const String currentBranch = 'flutter-2.3-candidate.13';

      processManager = FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['git', 'fetch', remoteName],
        ),
        const FakeCommand(
          command: <String>[flutterBinPath, 'help'],
        ),
        const FakeCommand(
          command: <String>[flutterBinPath, '--version', '--machine'],
          stdout: '{"frameworkVersion": "$currentVersion"}',
        ),
        FakeCommand(
          command: const <String>[
            'git',
            'branch',
            '--no-color',
            '--remotes',
            '--list',
            '$remoteName/*',
          ],
          stdout: <String>[
            '$remoteName/$oldBranch',
            '$remoteName/$currentBranch',
            '$remoteName/$newBranch',
          ].join('\n'),
        ),
      ]);
      final String pathSeparator = operatingSystem == 'windows' ? r'\' : '/';

      platform = FakePlatform(
        environment: <String, String>{
          'HOME': <String>['path', 'to', 'home'].join(pathSeparator),
        },
        pathSeparator: pathSeparator,
      );
      final Checkouts checkouts = Checkouts(
        fileSystem: fileSystem,
        parentDirectory: fileSystem.directory(checkoutsParentDirectory),
        platform: platform,
        processManager: processManager,
        stdio: stdio,
      );

      final CommandRunner<void> runner = createRunner(checkouts: checkouts);
      await runner.run(<String>['candidates', '--$kRemote', remoteName]);
      expect(stdio.stdout.contains('currentVersion = $currentVersion'), true);
      expect(stdio.stdout.contains(newBranch), true);
      expect(stdio.stdout.contains(oldBranch), false);
      expect(stdio.stdout.contains(currentBranch), false);
149 150 151 152 153
    });
  }, onPlatform: <String, dynamic>{
    'windows': const Skip('Flutter Conductor only supported on macos/linux'),
  });
}