roll_dev_test.dart 15.8 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:args/args.dart';
6
import 'package:dev_tools/roll_dev.dart';
7 8
import 'package:mockito/mockito.dart';

9 10 11
import './common.dart';

void main() {
12 13 14 15 16
  group('run()', () {
    const String usage = 'usage info...';
    const String level = 'z';
    const String commit = 'abcde012345';
    const String origin = 'upstream';
17 18
    const String lastVersion = '1.2.0-0.0.pre';
    const String nextVersion = '1.2.0-1.0.pre';
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    FakeArgResults fakeArgResults;
    MockGit mockGit;

    setUp(() {
      mockGit = MockGit();
    });

    test('returns false if help requested', () {
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
        help: true,
      );
      expect(
        run(
          usage: usage,
          argResults: fakeArgResults,
          git: mockGit,
        ),
        false,
      );
    });

    test('returns false if level not provided', () {
      fakeArgResults = FakeArgResults(
45
        level: null,
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        commit: commit,
        origin: origin,
      );
      expect(
        run(
          usage: usage,
          argResults: fakeArgResults,
          git: mockGit,
        ),
        false,
      );
    });

    test('returns false if commit not provided', () {
      fakeArgResults = FakeArgResults(
        level: level,
62
        commit: null,
63 64 65 66 67 68 69 70 71 72 73 74 75
        origin: origin,
      );
      expect(
        run(
          usage: usage,
          argResults: fakeArgResults,
          git: mockGit,
        ),
        false,
      );
    });

    test('throws exception if upstream remote wrong', () {
76 77
      const String remote = 'wrong-remote';
      when(mockGit.getOutput('remote get-url $origin', any)).thenReturn(remote);
78 79 80 81 82
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
      );
83 84 85
      const String errorMessage = 'The remote named $origin is set to $remote, when $kUpstreamRemote was expected.';
      expect(
        () => run(
86 87 88
          usage: usage,
          argResults: fakeArgResults,
          git: mockGit,
89 90 91
        ),
        throwsExceptionWith(errorMessage),
      );
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
    });

    test('throws exception if git checkout not clean', () {
      when(mockGit.getOutput('remote get-url $origin', any)).thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any)).thenReturn(
        ' M dev/tools/test/roll_dev_test.dart',
      );
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
      );
      Exception exception;
      try {
        run(
          usage: usage,
          argResults: fakeArgResults,
          git: mockGit,
        );
      } on Exception catch (e) {
        exception = e;
      }
      const String pattern = r'Your git repository is not clean. Try running '
        '"git clean -fd". Warning, this will delete files! Run with -n to find '
        'out which ones.';
      expect(exception?.toString(), contains(pattern));
    });

120
    test('does not reset or tag if --just-print is specified', () {
121 122 123
      when(mockGit.getOutput('remote get-url $origin', any)).thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any)).thenReturn('');
      when(mockGit.getOutput(
Christopher Fujino's avatar
Christopher Fujino committed
124
        'describe --match *.*.*-*.*.pre --exact-match --tags refs/remotes/$origin/dev',
125
        any,
126 127 128 129 130
      )).thenReturn(lastVersion);
      when(mockGit.getOutput(
        'rev-parse $lastVersion',
        any,
      )).thenReturn('zxy321');
131 132 133 134 135 136 137 138 139 140 141 142
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
        justPrint: true,
      );
      expect(run(
        usage: usage,
        argResults: fakeArgResults,
        git: mockGit,
      ), false);
      verify(mockGit.run('fetch $origin', any));
143
      verifyNever(mockGit.run('reset $commit --hard', any));
144 145 146
      verifyNever(mockGit.getOutput('rev-parse HEAD', any));
    });

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
    test('exits with exception if --skip-tagging is provided but commit isn\'t '
         'already tagged', () {
      when(mockGit.getOutput('remote get-url $origin', any)).thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any)).thenReturn('');
      when(mockGit.getOutput(
        'describe --match *.*.*-*.*.pre --exact-match --tags refs/remotes/$origin/dev',
        any,
      )).thenReturn(lastVersion);
      when(mockGit.getOutput(
        'rev-parse $lastVersion',
        any,
      )).thenReturn('zxy321');
      const String exceptionMessage = 'Failed to verify $commit is already '
        'tagged. You can only use the flag `$kSkipTagging` if the commit has '
        'already been tagged.';
      when(mockGit.run(
        'describe --exact-match --tags $commit',
        any,
      )).thenThrow(Exception(exceptionMessage));

      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
        skipTagging: true,
      );
      expect(
        () => run(
          usage: usage,
          argResults: fakeArgResults,
          git: mockGit,
        ),
        throwsExceptionWith(exceptionMessage),
      );
      verify(mockGit.run('fetch $origin', any));
      verifyNever(mockGit.run('reset $commit --hard', any));
      verifyNever(mockGit.getOutput('rev-parse HEAD', any));
    });

    test('throws exception if desired commit is already tip of dev branch', () {
187 188 189
      when(mockGit.getOutput('remote get-url $origin', any)).thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any)).thenReturn('');
      when(mockGit.getOutput(
Christopher Fujino's avatar
Christopher Fujino committed
190
        'describe --match *.*.*-*.*.pre --exact-match --tags refs/remotes/$origin/dev',
191
        any,
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
      )).thenReturn(lastVersion);
      when(mockGit.getOutput(
        'rev-parse $lastVersion',
        any,
      )).thenReturn(commit);
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
        justPrint: true,
      );
      expect(
        () => run(
          usage: usage,
          argResults: fakeArgResults,
          git: mockGit,
        ),
        throwsExceptionWith('is already on the dev branch as'),
      );
      verify(mockGit.run('fetch $origin', any));
      verifyNever(mockGit.run('reset $commit --hard', any));
      verifyNever(mockGit.getOutput('rev-parse HEAD', any));
    });

    test('does not tag if last release is not direct ancestor of desired '
        'commit and --force not supplied', () {
      when(mockGit.getOutput('remote get-url $origin', any))
        .thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any))
        .thenReturn('');
      when(mockGit.getOutput(
        'describe --match *.*.*-*.*.pre --exact-match --tags refs/remotes/$origin/dev',
        any,
      )).thenReturn(lastVersion);
      when(mockGit.getOutput(
        'rev-parse $lastVersion',
        any,
      )).thenReturn('zxy321');
      when(mockGit.run('merge-base --is-ancestor $lastVersion $commit', any))
        .thenThrow(Exception(
          'Failed to verify $lastVersion is a direct ancestor of $commit. The '
          'flag `--force` is required to force push a new release past a '
          'cherry-pick',
        ));
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
      );
      const String errorMessage = 'Failed to verify $lastVersion is a direct '
        'ancestor of $commit. The flag `--force` is required to force push a '
        'new release past a cherry-pick';
      expect(
        () => run(
          argResults: fakeArgResults,
          git: mockGit,
          usage: usage,
        ),
        throwsExceptionWith(errorMessage),
      );

      verify(mockGit.run('fetch $origin', any));
      verifyNever(mockGit.run('reset $commit --hard', any));
      verifyNever(mockGit.run('push $origin HEAD:dev', any));
      verifyNever(mockGit.run('tag $nextVersion', any));
    });

    test('does not tag but updates branch if --skip-tagging provided', () {
      when(mockGit.getOutput('remote get-url $origin', any))
        .thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any))
        .thenReturn('');
      when(mockGit.getOutput(
        'describe --match *.*.*-*.*.pre --exact-match --tags refs/remotes/$origin/dev',
        any,
      )).thenReturn(lastVersion);
      when(mockGit.getOutput(
        'rev-parse $lastVersion',
        any,
      )).thenReturn('zxy321');
      when(mockGit.getOutput('rev-parse HEAD', any)).thenReturn(commit);
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
        skipTagging: true,
      );
      expect(run(
        usage: usage,
        argResults: fakeArgResults,
        git: mockGit,
      ), true);
      verify(mockGit.run('fetch $origin', any));
      verify(mockGit.run('reset $commit --hard', any));
      verifyNever(mockGit.run('tag $nextVersion', any));
      verifyNever(mockGit.run('push $origin $nextVersion', any));
      verify(mockGit.run('push $origin HEAD:dev', any));
    });

    test('successfully tags and publishes release', () {
      when(mockGit.getOutput('remote get-url $origin', any))
        .thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any))
        .thenReturn('');
      when(mockGit.getOutput(
        'describe --match *.*.*-*.*.pre --exact-match --tags refs/remotes/$origin/dev',
        any,
      )).thenReturn('1.2.0-0.0.pre');
      when(mockGit.getOutput(
        'rev-parse $lastVersion',
        any,
      )).thenReturn('zxy321');
304 305 306 307 308 309 310 311 312 313 314 315 316
      when(mockGit.getOutput('rev-parse HEAD', any)).thenReturn(commit);
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
      );
      expect(run(
        usage: usage,
        argResults: fakeArgResults,
        git: mockGit,
      ), true);
      verify(mockGit.run('fetch $origin', any));
      verify(mockGit.run('reset $commit --hard', any));
317 318
      verify(mockGit.run('tag $nextVersion', any));
      verify(mockGit.run('push $origin $nextVersion', any));
319 320 321 322 323 324 325
      verify(mockGit.run('push $origin HEAD:dev', any));
    });

    test('successfully publishes release with --force', () {
      when(mockGit.getOutput('remote get-url $origin', any)).thenReturn(kUpstreamRemote);
      when(mockGit.getOutput('status --porcelain', any)).thenReturn('');
      when(mockGit.getOutput(
Christopher Fujino's avatar
Christopher Fujino committed
326
        'describe --match *.*.*-*.*.pre --exact-match --tags refs/remotes/$origin/dev',
327
        any,
328 329 330 331 332
      )).thenReturn(lastVersion);
      when(mockGit.getOutput(
        'rev-parse $lastVersion',
        any,
      )).thenReturn('zxy321');
333 334 335 336 337 338 339 340 341 342 343 344 345 346
      when(mockGit.getOutput('rev-parse HEAD', any)).thenReturn(commit);
      fakeArgResults = FakeArgResults(
        level: level,
        commit: commit,
        origin: origin,
        force: true,
      );
      expect(run(
        usage: usage,
        argResults: fakeArgResults,
        git: mockGit,
      ), true);
      verify(mockGit.run('fetch $origin', any));
      verify(mockGit.run('reset $commit --hard', any));
347
      verify(mockGit.run('tag $nextVersion', any));
348 349 350 351
      verify(mockGit.run('push --force $origin HEAD:dev', any));
    });
  });

352 353 354
  group('parseFullTag', () {
    test('returns match on valid version input', () {
      final List<String> validTags = <String>[
355 356 357 358 359 360 361
        '1.2.3-1.2.pre',
        '10.2.30-12.22.pre',
        '1.18.0-0.0.pre',
        '2.0.0-1.99.pre',
        '12.34.56-78.90.pre',
        '0.0.1-0.0.pre',
        '958.80.144-6.224.pre',
362 363 364 365 366 367 368 369 370
      ];
      for (final String validTag in validTags) {
        final Match match = parseFullTag(validTag);
        expect(match, isNotNull, reason: 'Expected $validTag to be parsed');
      }
    });

    test('returns null on invalid version input', () {
      final List<String> invalidTags = <String>[
371 372 373 374 375
        '1.2.3-1.2.pre-3-gabc123',
        '1.2.3-1.2.3.pre',
        '1.2.3.1.2.pre',
        '1.2.3-dev.1.2',
        '1.2.3-1.2-3',
376 377
        'v1.2.3',
        '2.0.0',
378 379
        'v1.2.3-1.2.pre',
        '1.2.3-1.2.pre_',
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
      ];
      for (final String invalidTag in invalidTags) {
        final Match match = parseFullTag(invalidTag);
        expect(match, null, reason: 'Expected $invalidTag to not be parsed');
      }
    });
  });

  group('getVersionFromParts', () {
    test('returns correct string from valid parts', () {
      List<int> parts = <int>[1, 2, 3, 4, 5];
      expect(getVersionFromParts(parts), '1.2.3-4.5.pre');

      parts = <int>[11, 2, 33, 1, 0];
      expect(getVersionFromParts(parts), '11.2.33-1.0.pre');
    });
  });
397 398 399 400 401 402 403 404 405 406

  group('incrementLevel()', () {
    const String hash = 'abc123';

    test('throws exception if hash is not valid release candidate', () {
      String level = 'z';

      String version = '1.0.0-0.0.pre-1-g$hash';
      expect(
        () => incrementLevel(version, level),
407
        throwsExceptionWith('Git reported the latest version as "$version"'),
408 409 410 411 412 413
        reason: 'should throw because $version should be an exact tag',
      );

      version = '1.2.3';
      expect(
        () => incrementLevel(version, level),
414
        throwsExceptionWith('Git reported the latest version as "$version"'),
415 416 417 418 419 420 421
        reason: 'should throw because $version should be a dev tag, not stable.'
      );

      version = '1.0.0-0.0.pre-1-g$hash';
      level = 'q';
      expect(
        () => incrementLevel(version, level),
422
        throwsExceptionWith('Git reported the latest version as "$version"'),
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        reason: 'should throw because $level is unsupported',
      );
    });

    test('successfully increments x', () {
      const String level = 'x';

      String version = '1.0.0-0.0.pre';
      expect(incrementLevel(version, level), '2.0.0-0.0.pre');

      version = '10.20.0-40.50.pre';
      expect(incrementLevel(version, level), '11.0.0-0.0.pre');

      version = '1.18.0-3.0.pre';
      expect(incrementLevel(version, level), '2.0.0-0.0.pre');
    });

    test('successfully increments y', () {
      const String level = 'y';

      String version = '1.0.0-0.0.pre';
      expect(incrementLevel(version, level), '1.1.0-0.0.pre');

      version = '10.20.0-40.50.pre';
      expect(incrementLevel(version, level), '10.21.0-0.0.pre');

      version = '1.18.0-3.0.pre';
      expect(incrementLevel(version, level), '1.19.0-0.0.pre');
    });

    test('successfully increments z', () {
      const String level = 'z';

      String version = '1.0.0-0.0.pre';
      expect(incrementLevel(version, level), '1.0.0-1.0.pre');

      version = '10.20.0-40.50.pre';
      expect(incrementLevel(version, level), '10.20.0-41.0.pre');

      version = '1.18.0-3.0.pre';
      expect(incrementLevel(version, level), '1.18.0-4.0.pre');
    });
  });
}

468 469 470 471 472 473 474 475 476 477
Matcher throwsExceptionWith(String messageSubString) {
  return throwsA(
      isA<Exception>().having(
          (Exception e) => e.toString(),
          'description',
          contains(messageSubString),
      ),
  );
}

478 479 480 481 482
class FakeArgResults implements ArgResults {
  FakeArgResults({
    String level,
    String commit,
    String origin,
483 484 485
    bool justPrint = false,
    bool autoApprove = true, // so we don't have to mock stdin
    bool help = false,
486
    bool force = false,
487
    bool skipTagging = false,
488 489 490 491 492 493 494 495
  }) : _parsedArgs = <String, dynamic>{
    'increment': level,
    'commit': commit,
    'origin': origin,
    'just-print': justPrint,
    'yes': autoApprove,
    'help': help,
    'force': force,
496
    'skip-tagging': skipTagging,
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
  };

  @override
  String name;

  @override
  ArgResults command;

  @override
  final List<String> rest = <String>[];

  @override
  List<String> arguments;

  final Map<String, dynamic> _parsedArgs;

  @override
  Iterable<String> get options {
    return null;
  }

  @override
  dynamic operator [](String name) {
    return _parsedArgs[name];
  }

  @override
  bool wasParsed(String name) {
    return null;
  }
527
}
528 529

class MockGit extends Mock implements Git {}