start_test.dart 36.8 KB
Newer Older
1 2 3 4 5 6 7
// 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 'dart:convert' show jsonDecode;

import 'package:args/command_runner.dart';
8
import 'package:conductor_core/src/proto/conductor_state.pb.dart' as pb;
9
import 'package:conductor_core/src/proto/conductor_state.pbenum.dart';
10 11 12
import 'package:conductor_core/src/repository.dart';
import 'package:conductor_core/src/start.dart';
import 'package:conductor_core/src/state.dart';
13 14 15 16 17 18 19 20
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:platform/platform.dart';

import './common.dart';

void main() {
  group('start command', () {
21 22
    const String branchPointRevision =
        '5131a6e5e0c50b8b7b2906cd58dab8746d6450be';
23 24
    const String flutterRoot = '/flutter';
    const String checkoutsParentDirectory = '$flutterRoot/dev/tools/';
25 26
    const String githubUsername = 'user';
    const String frameworkMirror =
27 28
        'git@github.com:$githubUsername/flutter.git';
    const String engineMirror = 'git@github.com:$githubUsername/engine.git';
29
    const String candidateBranch = 'flutter-1.2-candidate.3';
30
    const String releaseChannel = 'beta';
31
    const String revision = 'abcd1234';
32
    const String conductorVersion = 'deadbeef';
33 34 35 36 37
    late Checkouts checkouts;
    late MemoryFileSystem fileSystem;
    late FakePlatform platform;
    late TestStdio stdio;
    late FakeProcessManager processManager;
38 39 40 41 42 43 44

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

    CommandRunner<void> createRunner({
45 46 47
      Map<String, String>? environment,
      String? operatingSystem,
      List<FakeCommand>? commands,
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
    }) {
      operatingSystem ??= const LocalPlatform().operatingSystem;
      final String pathSeparator = operatingSystem == 'windows' ? r'\' : '/';
      environment ??= <String, String>{
        'HOME': '/path/to/user/home',
      };
      final Directory homeDir = fileSystem.directory(
        environment['HOME'],
      );
      // Tool assumes this exists
      homeDir.createSync(recursive: true);
      platform = FakePlatform(
        environment: environment,
        operatingSystem: operatingSystem,
        pathSeparator: pathSeparator,
      );
      processManager = FakeProcessManager.list(commands ?? <FakeCommand>[]);
      checkouts = Checkouts(
        fileSystem: fileSystem,
        parentDirectory: fileSystem.directory(checkoutsParentDirectory),
        platform: platform,
        processManager: processManager,
        stdio: stdio,
      );
      final StartCommand command = StartCommand(
        checkouts: checkouts,
74
        conductorVersion: conductorVersion,
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      );
      return CommandRunner<void>('codesign-test', '')..addCommand(command);
    }

    test('throws exception if run from Windows', () async {
      final CommandRunner<void> runner = createRunner(
        commands: <FakeCommand>[
          const FakeCommand(
            command: <String>['git', 'rev-parse', 'HEAD'],
            stdout: revision,
          ),
        ],
        operatingSystem: 'windows',
      );
      await expectLater(
90 91 92 93 94
        () async => runner.run(<String>[
          'start',
          '--$kCandidateOption',
          candidateBranch,
          '--$kReleaseOption',
95
          'beta',
96 97 98
          '--$kStateOption',
          '/path/to/statefile.json',
        ]),
99 100 101 102 103 104
        throwsExceptionWith(
          'Error! This tool is only supported on macOS and Linux',
        ),
      );
    });

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    test('throws if provided an invalid --$kVersionOverrideOption', () async {
      final CommandRunner<void> runner = createRunner();

      final String stateFilePath = fileSystem.path.join(
        platform.environment['HOME']!,
        kStateFileName,
      );

      await expectLater(
        () async => runner.run(<String>[
          'start',
          '--$kCandidateOption',
          candidateBranch,
          '--$kReleaseOption',
          releaseChannel,
          '--$kStateOption',
          stateFilePath,
          '--$kVersionOverrideOption',
          'an invalid version string',
124 125
          '--$kGithubUsernameOption',
          githubUsername,
126 127 128 129 130 131 132
        ]),
        throwsExceptionWith('an invalid version string cannot be parsed'),
      );
    });

    test('creates state file if provided correct inputs', () async {
      stdio.stdin.add('y'); // accept prompt from ensureBranchPointTagged()
133 134
      const String revision2 = 'def789';
      const String revision3 = '123abc';
135 136 137 138
      const String previousDartRevision =
          '171876a4e6cf56ee6da1f97d203926bd7afda7ef';
      const String nextDartRevision =
          'f6c91128be6b77aef8351e1e3a9d07c85bc2e46e';
139 140 141
      const String previousVersion = '1.2.0-1.0.pre';
      // This is what this release will be
      const String nextVersion = '1.2.0-1.1.pre';
142
      const String candidateBranch = 'flutter-1.2-candidate.1';
143

144 145
      final Directory engine = fileSystem
          .directory(checkoutsParentDirectory)
146 147 148 149 150 151 152
          .childDirectory('flutter_conductor_checkouts')
          .childDirectory('engine');

      final File depsFile = engine.childFile('DEPS');

      final List<FakeCommand> engineCommands = <FakeCommand>[
        FakeCommand(
153 154 155 156 157 158 159 160 161 162 163 164
            command: <String>[
              'git',
              'clone',
              '--origin',
              'upstream',
              '--',
              EngineRepository.defaultUpstream,
              engine.path,
            ],
            onRun: () {
              // Create the DEPS file which the tool will update
              engine.createSync(recursive: true);
165 166
              depsFile
                  .writeAsStringSync(generateMockDeps(previousDartRevision));
167
            }),
168 169 170 171 172 173 174
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', engineMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
175
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'status', '--porcelain'],
          stdout: 'MM path/to/DEPS',
        ),
        const FakeCommand(
          command: <String>['git', 'add', '--all'],
        ),
        const FakeCommand(
197 198 199 200 201 202
          command: <String>[
            'git',
            'commit',
            '--message',
            'Update Dart SDK to $nextDartRevision',
          ],
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
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
      ];

      final List<FakeCommand> frameworkCommands = <FakeCommand>[
        FakeCommand(
          command: <String>[
            'git',
            'clone',
            '--origin',
            'upstream',
            '--',
            FrameworkRepository.defaultUpstream,
            fileSystem.path.join(
              checkoutsParentDirectory,
              'flutter_conductor_checkouts',
              'framework',
            ),
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', frameworkMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
237
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'describe',
            '--match',
            '*.*.*',
            '--tags',
            'refs/remotes/upstream/$candidateBranch',
          ],
          stdout: '$previousVersion-42-gabc123',
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
266
        const FakeCommand(
267 268 269 270 271 272
          command: <String>[
            'git',
            'merge-base',
            'upstream/$candidateBranch',
            'upstream/master',
          ],
273 274 275 276
          stdout: branchPointRevision,
        ),
        // check if commit is tagged, zero exit code means it is tagged
        const FakeCommand(
277 278 279 280 281 282 283
          command: <String>[
            'git',
            'describe',
            '--exact-match',
            '--tags',
            branchPointRevision,
          ],
284
        ),
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
      ];

      final CommandRunner<void> runner = createRunner(
        commands: <FakeCommand>[
          ...engineCommands,
          ...frameworkCommands,
        ],
      );

      final String stateFilePath = fileSystem.path.join(
        platform.environment['HOME']!,
        kStateFileName,
      );

      await runner.run(<String>[
        'start',
        '--$kCandidateOption',
        candidateBranch,
        '--$kReleaseOption',
        releaseChannel,
        '--$kStateOption',
        stateFilePath,
        '--$kDartRevisionOption',
        nextDartRevision,
309 310
        '--$kGithubUsernameOption',
        githubUsername,
311 312 313 314 315 316 317 318 319
      ]);

      final File stateFile = fileSystem.file(stateFilePath);

      final pb.ConductorState state = pb.ConductorState();
      state.mergeFromProto3Json(
        jsonDecode(stateFile.readAsStringSync()),
      );

320
      expect(state.releaseType, ReleaseType.BETA_HOTFIX);
321 322 323 324
      expect(
          stdio.error,
          isNot(contains(
              'Tried to tag the branch point, however the target version')));
325
      expect(processManager, hasNoRemainingExpectations);
326 327 328 329 330 331 332 333 334
      expect(state.isInitialized(), true);
      expect(state.releaseChannel, releaseChannel);
      expect(state.releaseVersion, nextVersion);
      expect(state.engine.candidateBranch, candidateBranch);
      expect(state.engine.startingGitHead, revision2);
      expect(state.engine.dartRevision, nextDartRevision);
      expect(state.engine.upstream.url, 'git@github.com:flutter/engine.git');
      expect(state.framework.candidateBranch, candidateBranch);
      expect(state.framework.startingGitHead, revision3);
335 336
      expect(
          state.framework.upstream.url, 'git@github.com:flutter/flutter.git');
337 338 339 340
      expect(state.currentPhase, ReleasePhase.APPLY_ENGINE_CHERRYPICKS);
      expect(state.conductorVersion, conductorVersion);
    });

341
    test('uses --$kVersionOverrideOption', () async {
342
      stdio.stdin.add('y'); // accept prompt from ensureBranchPointTagged()
343 344
      const String revision2 = 'def789';
      const String revision3 = '123abc';
345 346 347 348
      const String previousDartRevision =
          '171876a4e6cf56ee6da1f97d203926bd7afda7ef';
      const String nextDartRevision =
          'f6c91128be6b77aef8351e1e3a9d07c85bc2e46e';
349
      const String previousVersion = '1.2.0-1.0.pre';
350 351
      const String candidateBranch = 'flutter-1.2-candidate.1';
      const String versionOverride = '42.0.0-42.0.pre';
352

353 354
      final Directory engine = fileSystem
          .directory(checkoutsParentDirectory)
355 356 357 358
          .childDirectory('flutter_conductor_checkouts')
          .childDirectory('engine');

      final File depsFile = engine.childFile('DEPS');
359 360 361

      final List<FakeCommand> engineCommands = <FakeCommand>[
        FakeCommand(
362 363 364 365 366 367 368 369 370 371 372 373
            command: <String>[
              'git',
              'clone',
              '--origin',
              'upstream',
              '--',
              EngineRepository.defaultUpstream,
              engine.path,
            ],
            onRun: () {
              // Create the DEPS file which the tool will update
              engine.createSync(recursive: true);
374 375
              depsFile
                  .writeAsStringSync(generateMockDeps(previousDartRevision));
376
            }),
377 378 379 380 381 382 383
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', engineMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
384
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'status', '--porcelain'],
          stdout: 'MM path/to/DEPS',
        ),
        const FakeCommand(
          command: <String>['git', 'add', '--all'],
        ),
        const FakeCommand(
406 407 408 409 410 411
          command: <String>[
            'git',
            'commit',
            '--message',
            'Update Dart SDK to $nextDartRevision'
          ],
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
      ];

      final List<FakeCommand> frameworkCommands = <FakeCommand>[
        FakeCommand(
          command: <String>[
            'git',
            'clone',
            '--origin',
            'upstream',
            '--',
            FrameworkRepository.defaultUpstream,
            fileSystem.path.join(
              checkoutsParentDirectory,
              'flutter_conductor_checkouts',
              'framework',
            ),
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', frameworkMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
446
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'describe',
            '--match',
            '*.*.*',
            '--tags',
            'refs/remotes/upstream/$candidateBranch',
          ],
          stdout: '$previousVersion-42-gabc123',
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
475
        const FakeCommand(
476 477 478 479 480 481
          command: <String>[
            'git',
            'merge-base',
            'upstream/$candidateBranch',
            'upstream/master'
          ],
482 483
          stdout: branchPointRevision,
        ),
484 485 486 487
      ];

      final CommandRunner<void> runner = createRunner(
        commands: <FakeCommand>[
488 489
          ...engineCommands,
          ...frameworkCommands,
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
        ],
      );

      final String stateFilePath = fileSystem.path.join(
        platform.environment['HOME']!,
        kStateFileName,
      );

      await runner.run(<String>[
        'start',
        '--$kCandidateOption',
        candidateBranch,
        '--$kReleaseOption',
        releaseChannel,
        '--$kStateOption',
        stateFilePath,
        '--$kDartRevisionOption',
        nextDartRevision,
508 509
        '--$kVersionOverrideOption',
        versionOverride,
510 511
        '--$kGithubUsernameOption',
        githubUsername,
512 513 514 515 516 517 518 519 520
      ]);

      final File stateFile = fileSystem.file(stateFilePath);

      final pb.ConductorState state = pb.ConductorState();
      state.mergeFromProto3Json(
        jsonDecode(stateFile.readAsStringSync()),
      );

521 522
      expect(processManager, hasNoRemainingExpectations);
      expect(state.releaseVersion, versionOverride);
523 524
    });

525 526
    test('logs to STDERR but does not fail on an unexpected candidate branch',
        () async {
527 528 529
      stdio.stdin.add('y'); // accept prompt from ensureBranchPointTagged()
      const String revision2 = 'def789';
      const String revision3 = '123abc';
530 531 532 533
      const String previousDartRevision =
          '171876a4e6cf56ee6da1f97d203926bd7afda7ef';
      const String nextDartRevision =
          'f6c91128be6b77aef8351e1e3a9d07c85bc2e46e';
534 535 536
      // note that this significantly behind the candidate branch name
      const String previousVersion = '0.9.0-1.0.pre';
      // This is what this release will be
537
      const String nextVersion = '0.9.0-1.1.pre';
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

      final Directory engine = fileSystem
          .directory(checkoutsParentDirectory)
          .childDirectory('flutter_conductor_checkouts')
          .childDirectory('engine');

      final File depsFile = engine.childFile('DEPS');

      final List<FakeCommand> engineCommands = <FakeCommand>[
        FakeCommand(
            command: <String>[
              'git',
              'clone',
              '--origin',
              'upstream',
              '--',
              EngineRepository.defaultUpstream,
              engine.path,
            ],
            onRun: () {
              // Create the DEPS file which the tool will update
              engine.createSync(recursive: true);
560 561
              depsFile
                  .writeAsStringSync(generateMockDeps(previousDartRevision));
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
            }),
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', engineMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'status', '--porcelain'],
          stdout: 'MM path/to/DEPS',
        ),
        const FakeCommand(
          command: <String>['git', 'add', '--all'],
        ),
        const FakeCommand(
592 593 594 595 596 597
          command: <String>[
            'git',
            'commit',
            '--message',
            'Update Dart SDK to $nextDartRevision',
          ],
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
      ];

      final List<FakeCommand> frameworkCommands = <FakeCommand>[
        FakeCommand(
          command: <String>[
            'git',
            'clone',
            '--origin',
            'upstream',
            '--',
            FrameworkRepository.defaultUpstream,
            fileSystem.path.join(
              checkoutsParentDirectory,
              'flutter_conductor_checkouts',
              'framework',
            ),
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', frameworkMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'describe',
            '--match',
            '*.*.*',
            '--tags',
            'refs/remotes/upstream/$candidateBranch',
          ],
          stdout: '$previousVersion-42-gabc123',
        ),
657 658 659 660
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
661
        const FakeCommand(
662 663 664 665 666 667
          command: <String>[
            'git',
            'merge-base',
            'upstream/$candidateBranch',
            'upstream/master',
          ],
668 669
          stdout: branchPointRevision,
        ),
670
        // check if commit is tagged, 0 exit code means it is tagged
671
        const FakeCommand(
672 673 674 675 676 677 678
          command: <String>[
            'git',
            'describe',
            '--exact-match',
            '--tags',
            branchPointRevision,
          ],
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
        ),
      ];

      final CommandRunner<void> runner = createRunner(
        commands: <FakeCommand>[
          ...engineCommands,
          ...frameworkCommands,
        ],
      );

      final String stateFilePath = fileSystem.path.join(
        platform.environment['HOME']!,
        kStateFileName,
      );

      await runner.run(<String>[
        'start',
        '--$kCandidateOption',
        candidateBranch,
        '--$kReleaseOption',
        releaseChannel,
        '--$kStateOption',
        stateFilePath,
        '--$kDartRevisionOption',
        nextDartRevision,
704 705
        '--$kGithubUsernameOption',
        githubUsername,
706 707 708 709 710 711 712 713 714
      ]);

      final File stateFile = fileSystem.file(stateFilePath);

      final pb.ConductorState state = pb.ConductorState();
      state.mergeFromProto3Json(
        jsonDecode(stateFile.readAsStringSync()),
      );

715 716
      expect(stdio.error,
          isNot(contains('Tried to tag the branch point, however')));
717
      expect(processManager, hasNoRemainingExpectations);
718 719 720 721 722 723 724 725 726
      expect(state.isInitialized(), true);
      expect(state.releaseChannel, releaseChannel);
      expect(state.releaseVersion, nextVersion);
      expect(state.engine.candidateBranch, candidateBranch);
      expect(state.engine.startingGitHead, revision2);
      expect(state.engine.dartRevision, nextDartRevision);
      expect(state.engine.upstream.url, 'git@github.com:flutter/engine.git');
      expect(state.framework.candidateBranch, candidateBranch);
      expect(state.framework.startingGitHead, revision3);
727 728
      expect(
          state.framework.upstream.url, 'git@github.com:flutter/flutter.git');
729 730
      expect(state.currentPhase, ReleasePhase.APPLY_ENGINE_CHERRYPICKS);
      expect(state.conductorVersion, conductorVersion);
731
      expect(state.releaseType, ReleaseType.BETA_HOTFIX);
732 733 734 735
      expect(
          stdio.error,
          contains(
              'Parsed version $previousVersion.42 has a different x value than candidate branch $candidateBranch'));
736 737
    });

738 739 740
    test('can convert from dev style version to stable version', () async {
      const String revision2 = 'def789';
      const String revision3 = '123abc';
741 742 743 744
      const String previousDartRevision =
          '171876a4e6cf56ee6da1f97d203926bd7afda7ef';
      const String nextDartRevision =
          'f6c91128be6b77aef8351e1e3a9d07c85bc2e46e';
745
      const String previousVersion = '1.2.0-3.0.pre';
746 747
      const String nextVersion = '1.2.0';

748 749
      final Directory engine = fileSystem
          .directory(checkoutsParentDirectory)
750 751 752 753 754 755 756
          .childDirectory('flutter_conductor_checkouts')
          .childDirectory('engine');

      final File depsFile = engine.childFile('DEPS');

      final List<FakeCommand> engineCommands = <FakeCommand>[
        FakeCommand(
757 758 759 760 761 762 763 764 765 766 767 768
            command: <String>[
              'git',
              'clone',
              '--origin',
              'upstream',
              '--',
              EngineRepository.defaultUpstream,
              engine.path,
            ],
            onRun: () {
              // Create the DEPS file which the tool will update
              engine.createSync(recursive: true);
769 770
              depsFile
                  .writeAsStringSync(generateMockDeps(previousDartRevision));
771
            }),
772 773 774 775 776 777 778
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', engineMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
779
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
780 781 782 783 784 785 786 787 788 789 790 791 792
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
793 794 795 796
        const FakeCommand(
          command: <String>['git', 'status', '--porcelain'],
          stdout: 'MM path/to/DEPS',
        ),
797 798 799 800
        const FakeCommand(
          command: <String>['git', 'add', '--all'],
        ),
        const FakeCommand(
801 802 803 804 805 806
          command: <String>[
            'git',
            'commit',
            '--message',
            'Update Dart SDK to $nextDartRevision',
          ],
807 808 809 810 811
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
812 813 814 815 816
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
      ];
817

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
      final List<FakeCommand> frameworkCommands = <FakeCommand>[
        FakeCommand(
          command: <String>[
            'git',
            'clone',
            '--origin',
            'upstream',
            '--',
            FrameworkRepository.defaultUpstream,
            fileSystem.path.join(
              checkoutsParentDirectory,
              'flutter_conductor_checkouts',
              'framework',
            ),
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', frameworkMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
841
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
842 843 844 845 846 847 848 849 850 851 852 853 854
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
855 856 857 858 859 860 861 862 863 864 865
        const FakeCommand(
          command: <String>[
            'git',
            'describe',
            '--match',
            '*.*.*',
            '--tags',
            'refs/remotes/upstream/$candidateBranch',
          ],
          stdout: '$previousVersion-42-gabc123',
        ),
866 867 868 869
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
870
        const FakeCommand(
871 872 873 874 875 876
          command: <String>[
            'git',
            'merge-base',
            'upstream/$candidateBranch',
            'upstream/master'
          ],
877 878 879 880
          stdout: branchPointRevision,
        ),
        // check if commit is tagged, 0 exit code thus it is tagged
        const FakeCommand(
881 882 883 884 885 886 887
          command: <String>[
            'git',
            'describe',
            '--exact-match',
            '--tags',
            branchPointRevision,
          ],
888
        ),
889
      ];
890

891 892
      final CommandRunner<void> runner = createRunner(
        commands: <FakeCommand>[
893 894
          ...engineCommands,
          ...frameworkCommands,
895 896 897 898
        ],
      );

      final String stateFilePath = fileSystem.path.join(
899
        platform.environment['HOME']!,
900 901 902 903 904 905 906 907
        kStateFileName,
      );

      await runner.run(<String>[
        'start',
        '--$kCandidateOption',
        candidateBranch,
        '--$kReleaseOption',
908
        'stable',
909 910
        '--$kStateOption',
        stateFilePath,
911 912
        '--$kDartRevisionOption',
        nextDartRevision,
913 914
        '--$kGithubUsernameOption',
        githubUsername,
915 916 917 918 919 920 921 922 923
      ]);

      final File stateFile = fileSystem.file(stateFilePath);

      final pb.ConductorState state = pb.ConductorState();
      state.mergeFromProto3Json(
        jsonDecode(stateFile.readAsStringSync()),
      );

924
      expect(processManager.hasRemainingExpectations, false);
925
      expect(state.isInitialized(), true);
926
      expect(state.releaseChannel, 'stable');
927
      expect(state.releaseVersion, nextVersion);
928 929
      expect(state.engine.candidateBranch, candidateBranch);
      expect(state.engine.startingGitHead, revision2);
930
      expect(state.engine.dartRevision, nextDartRevision);
931 932
      expect(state.framework.candidateBranch, candidateBranch);
      expect(state.framework.startingGitHead, revision3);
933
      expect(state.currentPhase, ReleasePhase.APPLY_ENGINE_CHERRYPICKS);
934
      expect(state.conductorVersion, conductorVersion);
935
      expect(state.releaseType, ReleaseType.STABLE_INITIAL);
936
    });
937 938 939
    test(
        'StartContext gets engine and framework checkout directories after run',
        () async {
940 941 942 943
      stdio.stdin.add('y');
      const String revision2 = 'def789';
      const String revision3 = '123abc';
      const String branchPointRevision = 'deadbeef';
944 945 946 947
      const String previousDartRevision =
          '171876a4e6cf56ee6da1f97d203926bd7afda7ef';
      const String nextDartRevision =
          'f6c91128be6b77aef8351e1e3a9d07c85bc2e46e';
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
      const String previousVersion = '1.2.0-1.0.pre';
      // This is a git tag applied to the branch point, not an actual release
      const String branchPointTag = '1.2.0-3.0.pre';

      final Directory engine = fileSystem
          .directory(checkoutsParentDirectory)
          .childDirectory('flutter_conductor_checkouts')
          .childDirectory('engine');

      final Directory framework = fileSystem
          .directory(checkoutsParentDirectory)
          .childDirectory('flutter_conductor_checkouts')
          .childDirectory('framework');

      final File depsFile = engine.childFile('DEPS');

      final List<FakeCommand> engineCommands = <FakeCommand>[
        FakeCommand(
            command: <String>[
              'git',
              'clone',
              '--origin',
              'upstream',
              '--',
              EngineRepository.defaultUpstream,
              engine.path,
            ],
            onRun: () {
              // Create the DEPS file which the tool will update
              engine.createSync(recursive: true);
978 979
              depsFile
                  .writeAsStringSync(generateMockDeps(previousDartRevision));
980 981 982 983 984 985 986 987
            }),
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', engineMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
988
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'status', '--porcelain'],
          stdout: 'MM path/to/DEPS',
        ),
        const FakeCommand(
          command: <String>['git', 'add', '--all'],
        ),
        const FakeCommand(
1010 1011 1012 1013 1014 1015
          command: <String>[
            'git',
            'commit',
            '--message',
            'Update Dart SDK to $nextDartRevision'
          ],
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision2,
        ),
      ];

      final List<FakeCommand> frameworkCommands = <FakeCommand>[
        FakeCommand(
          command: <String>[
            'git',
            'clone',
            '--origin',
            'upstream',
            '--',
            FrameworkRepository.defaultUpstream,
            framework.path,
          ],
        ),
        const FakeCommand(
          command: <String>['git', 'remote', 'add', 'mirror', frameworkMirror],
        ),
        const FakeCommand(
          command: <String>['git', 'fetch', 'mirror'],
        ),
        const FakeCommand(
1046
          command: <String>['git', 'checkout', 'upstream/$candidateBranch'],
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
        ),
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: revision3,
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'checkout',
            '-b',
            'cherrypicks-$candidateBranch',
          ],
        ),
        const FakeCommand(
          command: <String>[
            'git',
            'describe',
            '--match',
            '*.*.*',
            '--tags',
            'refs/remotes/upstream/$candidateBranch',
          ],
          stdout: '$previousVersion-42-gabc123',
        ),
1071 1072 1073 1074 1075
        // HEAD and branch point are same
        const FakeCommand(
          command: <String>['git', 'rev-parse', 'HEAD'],
          stdout: branchPointRevision,
        ),
1076
        const FakeCommand(
1077 1078 1079 1080 1081 1082
          command: <String>[
            'git',
            'merge-base',
            'upstream/$candidateBranch',
            'upstream/master'
          ],
1083 1084
          stdout: branchPointRevision,
        ),
1085 1086
        // check if commit is tagged
        const FakeCommand(
1087 1088 1089 1090 1091 1092 1093
          command: <String>[
            'git',
            'describe',
            '--exact-match',
            '--tags',
            branchPointRevision
          ],
1094 1095 1096
          // non-zero exit code means branch point is NOT tagged
          exitCode: 128,
        ),
1097 1098 1099 1100
        const FakeCommand(
          command: <String>['git', 'tag', branchPointTag, branchPointRevision],
        ),
        const FakeCommand(
1101 1102 1103 1104 1105 1106
          command: <String>[
            'git',
            'push',
            FrameworkRepository.defaultUpstream,
            branchPointTag
          ],
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
        ),
      ];

      final String operatingSystem = const LocalPlatform().operatingSystem;
      final Map<String, String> environment = <String, String>{
        'HOME': '/path/to/user/home',
      };
      final Directory homeDir = fileSystem.directory(
        environment['HOME'],
      );
      // Tool assumes this exists
      homeDir.createSync(recursive: true);
      platform = FakePlatform(
        environment: environment,
        operatingSystem: operatingSystem,
      );

      final String stateFilePath = fileSystem.path.join(
        platform.environment['HOME']!,
        kStateFileName,
      );
      final File stateFile = fileSystem.file(stateFilePath);

      processManager = FakeProcessManager.list(<FakeCommand>[
        ...engineCommands,
        ...frameworkCommands,
      ]);
      checkouts = Checkouts(
        fileSystem: fileSystem,
        parentDirectory: fileSystem.directory(checkoutsParentDirectory),
        platform: platform,
        processManager: processManager,
        stdio: stdio,
      );

      final StartContext startContext = StartContext(
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
        candidateBranch: candidateBranch,
        checkouts: checkouts,
        dartRevision: nextDartRevision,
        engineCherrypickRevisions: <String>[],
        engineMirror: engineMirror,
        engineUpstream: EngineRepository.defaultUpstream,
        frameworkCherrypickRevisions: <String>[],
        frameworkMirror: frameworkMirror,
        frameworkUpstream: FrameworkRepository.defaultUpstream,
        releaseChannel: releaseChannel,
        processManager: processManager,
        conductorVersion: conductorVersion,
1155
        githubUsername: githubUsername,
1156 1157
        stateFile: stateFile,
      );
1158 1159 1160

      await startContext.run();

1161 1162 1163 1164 1165
      final pb.ConductorState state = pb.ConductorState();
      state.mergeFromProto3Json(
        jsonDecode(stateFile.readAsStringSync()),
      );

1166 1167 1168 1169
      expect((await startContext.engine.checkoutDirectory).path,
          equals(engine.path));
      expect((await startContext.framework.checkoutDirectory).path,
          equals(framework.path));
1170 1171
      expect(state.releaseType, ReleaseType.BETA_INITIAL);
      expect(processManager, hasNoRemainingExpectations);
1172
    });
1173 1174 1175 1176
  }, onPlatform: <String, dynamic>{
    'windows': const Skip('Flutter Conductor only supported on macos/linux'),
  });
}
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

String generateMockDeps(String dartRevision) {
  return '''
vars = {
  'chromium_git': 'https://chromium.googlesource.com',
  'swiftshader_git': 'https://swiftshader.googlesource.com',
  'dart_git': 'https://dart.googlesource.com',
  'flutter_git': 'https://flutter.googlesource.com',
  'fuchsia_git': 'https://fuchsia.googlesource.com',
  'github_git': 'https://github.com',
  'skia_git': 'https://skia.googlesource.com',
  'ocmock_git': 'https://github.com/erikdoe/ocmock.git',
  'skia_revision': '4e9d5e2bdf04c58bc0bff57be7171e469e5d7175',

  'dart_revision': '$dartRevision',
  'dart_boringssl_gen_rev': '7322fc15cc065d8d2957fccce6b62a509dc4d641',
}''';
}