version_test.dart 5.77 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:conductor_core/src/proto/conductor_state.pbenum.dart';
6
import 'package:conductor_core/src/version.dart';
7

8
import 'common.dart';
9 10

void main() {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  group('Version.fromString()', () {
    test('parses commits past a tagged stable', () {
      const String versionString = '2.8.0-1-g2ef5ad67fe';
      final Version version;
      try {
        version = Version.fromString(versionString);
      } on Exception catch (exception) {
        fail('Failed parsing "$versionString" with:\n$exception');
      }
      expect(version.x, 2);
      expect(version.y, 8);
      expect(version.z, 0);
      expect(version.m, isNull);
      expect(version.n, isNull);
      expect(version.commits, 1);
      expect(version.type, VersionType.gitDescribe);
    });
  });
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  group('Version.increment()', () {
    test('throws exception on nonsensical `level`', () {
      final List<String> levels = <String>['f', '0', 'xyz'];
      for (final String level in levels) {
        final Version version = Version.fromString('1.0.0-0.0.pre');
        expect(
          () => Version.increment(version, level).toString(),
          throwsExceptionWith('Unknown increment level $level.'),
        );
      }
    });

    test('does not support incrementing x', () {
      const String level = 'x';

      final Version version = Version.fromString('1.0.0-0.0.pre');
      expect(
        () => Version.increment(version, level).toString(),
        throwsExceptionWith(
            'Incrementing $level is not supported by this tool'),
      );
    });

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

      Version version = Version.fromString('1.0.0-0.0.pre');
      expect(Version.increment(version, level).toString(), '1.1.0-0.0.pre');

      version = Version.fromString('10.20.0-40.50.pre');
      expect(Version.increment(version, level).toString(), '10.21.0-0.0.pre');

      version = Version.fromString('1.18.0-3.0.pre');
      expect(Version.increment(version, level).toString(), '1.19.0-0.0.pre');
    });

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

68 69
      Version version = Version.fromString('1.0.0');
      expect(Version.increment(version, level).toString(), '1.0.1');
70

71 72
      version = Version.fromString('10.20.0');
      expect(Version.increment(version, level).toString(), '10.20.1');
73

74 75
      version = Version.fromString('1.18.3');
      expect(Version.increment(version, level).toString(), '1.18.4');
76 77
    });

78
    test('does not support incrementing m', () {
79 80
      const String level = 'm';

81 82 83 84 85
      final Version version = Version.fromString('1.0.0-0.0.pre');
      expect(
        () => Version.increment(version, level).toString(),
        throwsAssertionWith("Do not increment 'm' via Version.increment"),
      );
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    });

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

      Version version = Version.fromString('1.0.0-0.0.pre');
      expect(Version.increment(version, level).toString(), '1.0.0-0.1.pre');

      version = Version.fromString('10.20.0-40.50.pre');
      expect(Version.increment(version, level).toString(), '10.20.0-40.51.pre');

      version = Version.fromString('1.18.0-3.0.pre');
      expect(Version.increment(version, level).toString(), '1.18.0-3.1.pre');
    });
  }, onPlatform: <String, dynamic>{
    'windows': const Skip('Flutter Conductor only supported on macos/linux'),
  });
103 104 105 106 107 108 109

  group('.ensureValid()', () {
    test('throws when x does not match', () {
      const String versionString = '1.2.3-4.5.pre';
      const String candidateBranch = 'flutter-3.2-candidate.4';
      final Version version = Version.fromString(versionString);
      expect(
110
        () => version.ensureValid(candidateBranch, ReleaseType.BETA_HOTFIX),
111 112 113 114 115 116 117 118 119 120 121 122
        throwsExceptionWith(
          'Parsed version $versionString has a different x value than '
          'candidate branch $candidateBranch',
        ),
      );
    });

    test('throws when y does not match', () {
      const String versionString = '1.2.3';
      const String candidateBranch = 'flutter-1.15-candidate.4';
      final Version version = Version.fromString(versionString);
      expect(
123
        () => version.ensureValid(candidateBranch, ReleaseType.BETA_INITIAL),
124 125 126 127 128 129 130 131 132 133 134 135
        throwsExceptionWith(
          'Parsed version $versionString has a different y value than '
          'candidate branch $candidateBranch',
        ),
      );
    });

    test('throws when m does not match', () {
      const String versionString = '1.2.3-4.5.pre';
      const String candidateBranch = 'flutter-1.2-candidate.0';
      final Version version = Version.fromString(versionString);
      expect(
136
        () => version.ensureValid(candidateBranch, ReleaseType.BETA_HOTFIX),
137 138 139 140 141 142 143 144 145 146 147 148
        throwsExceptionWith(
          'Parsed version $versionString has a different m value than '
          'candidate branch $candidateBranch',
        ),
      );
    });

    test('does not validate m if version type is stable', () {
      const String versionString = '1.2.0';
      const String candidateBranch = 'flutter-1.2-candidate.98';
      final Version version = Version.fromString(versionString);
      expect(
149
        () => version.ensureValid(candidateBranch, ReleaseType.STABLE_HOTFIX),
150 151 152 153 154 155 156 157 158
        isNot(throwsException),
      );
    });

    test('throws on malformed candidate branch', () {
      const String versionString = '1.2.0';
      const String candidateBranch = 'stable';
      final Version version = Version.fromString(versionString);
      expect(
159
        () => version.ensureValid(candidateBranch, ReleaseType.STABLE_HOTFIX),
160 161 162 163 164 165
        throwsExceptionWith(
          'Candidate branch $candidateBranch does not match the pattern',
        ),
      );
    });
  });
166
}