build_info_test.dart 10.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter_tools/src/base/logger.dart';
6 7
import 'package:flutter_tools/src/build_info.dart';

8
import '../src/common.dart';
9 10

void main() {
11
  late BufferLogger logger;
12 13 14
  setUp(() {
    logger = BufferLogger.test();
  });
15 16

  group('Validate build number', () {
17
    testWithoutContext('CFBundleVersion for iOS', () async {
18
      String? buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, 'xyz', logger);
19
      expect(buildName, isNull);
20
      buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, '0.0.1', logger);
21
      expect(buildName, '0.0.1');
22
      buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, '123.xyz', logger);
23
      expect(buildName, '123');
24
      buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, '123.456.xyz', logger);
25 26 27
      expect(buildName, '123.456');
    });

28
    testWithoutContext('versionCode for Android', () async {
29
      String? buildName = validatedBuildNumberForPlatform(TargetPlatform.android_arm, '123.abc+-', logger);
30
      expect(buildName, '123');
31
      buildName = validatedBuildNumberForPlatform(TargetPlatform.android_arm, 'abc', logger);
32 33 34 35 36
      expect(buildName, '1');
    });
  });

  group('Validate build name', () {
37
    testWithoutContext('CFBundleShortVersionString for iOS', () async {
38
      String? buildName = validatedBuildNameForPlatform(TargetPlatform.ios, 'xyz', logger);
39
      expect(buildName, isNull);
40
      buildName = validatedBuildNameForPlatform(TargetPlatform.ios, '0.0.1', logger);
41
      expect(buildName, '0.0.1');
42 43 44

      buildName = validatedBuildNameForPlatform(TargetPlatform.ios, '123.456.xyz', logger);
      expect(logger.traceText, contains('Invalid build-name'));
45
      expect(buildName, '123.456.0');
46 47

      buildName = validatedBuildNameForPlatform(TargetPlatform.ios, '123.xyz', logger);
48 49 50
      expect(buildName, '123.0.0');
    });

51
    testWithoutContext('versionName for Android', () async {
52
      String? buildName = validatedBuildNameForPlatform(TargetPlatform.android_arm, '123.abc+-', logger);
53
      expect(buildName, '123.abc+-');
54
      buildName = validatedBuildNameForPlatform(TargetPlatform.android_arm, 'abc+-', logger);
55 56
      expect(buildName, 'abc+-');
    });
57

58
    testWithoutContext('build mode configuration is correct', () {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      expect(BuildMode.debug.isRelease, false);
      expect(BuildMode.debug.isPrecompiled, false);
      expect(BuildMode.debug.isJit, true);

      expect(BuildMode.profile.isRelease, false);
      expect(BuildMode.profile.isPrecompiled, true);
      expect(BuildMode.profile.isJit, false);

      expect(BuildMode.release.isRelease, true);
      expect(BuildMode.release.isPrecompiled, true);
      expect(BuildMode.release.isJit, false);

      expect(BuildMode.jitRelease.isRelease, true);
      expect(BuildMode.jitRelease.isPrecompiled, false);
      expect(BuildMode.jitRelease.isJit, true);

      expect(BuildMode.fromName('debug'), BuildMode.debug);
      expect(BuildMode.fromName('profile'), BuildMode.profile);
      expect(BuildMode.fromName('jit_release'), BuildMode.jitRelease);
      expect(BuildMode.fromName('release'), BuildMode.release);
Dan Field's avatar
Dan Field committed
79
      expect(() => BuildMode.fromName('foo'), throwsArgumentError);
80
    });
81
  });
82

83 84 85 86 87 88 89 90 91 92 93 94
  testWithoutContext('getDartNameForDarwinArch returns name used in Dart SDK', () {
    expect(getDartNameForDarwinArch(DarwinArch.armv7),  'armv7');
    expect(getDartNameForDarwinArch(DarwinArch.arm64),  'arm64');
    expect(getDartNameForDarwinArch(DarwinArch.x86_64), 'x64');
  });

  testWithoutContext('getNameForDarwinArch returns Apple names', () {
    expect(getNameForDarwinArch(DarwinArch.armv7),  'armv7');
    expect(getNameForDarwinArch(DarwinArch.arm64),  'arm64');
    expect(getNameForDarwinArch(DarwinArch.x86_64), 'x86_64');
  });

95
  testWithoutContext('getNameForTargetPlatform on Darwin arches', () {
96 97 98 99 100
    expect(getNameForTargetPlatform(TargetPlatform.ios, darwinArch: DarwinArch.arm64), 'ios-arm64');
    expect(getNameForTargetPlatform(TargetPlatform.ios, darwinArch: DarwinArch.armv7), 'ios-armv7');
    expect(getNameForTargetPlatform(TargetPlatform.ios, darwinArch: DarwinArch.x86_64), 'ios-x86_64');
    expect(getNameForTargetPlatform(TargetPlatform.android), isNot(contains('ios')));
  });
101

102
  testWithoutContext('getIOSArchForName on Darwin arches', () {
103 104 105 106
    expect(getIOSArchForName('armv7'), DarwinArch.armv7);
    expect(getIOSArchForName('arm64'), DarwinArch.arm64);
    expect(getIOSArchForName('arm64e'), DarwinArch.arm64);
    expect(getIOSArchForName('x86_64'), DarwinArch.x86_64);
107
    expect(() => getIOSArchForName('bogus'), throwsException);
108
  });
109

110 111 112 113 114 115 116 117 118 119 120
  testWithoutContext('named BuildInfo has correct defaults', () {
    expect(BuildInfo.debug.mode, BuildMode.debug);
    expect(BuildInfo.debug.trackWidgetCreation, true);

    expect(BuildInfo.profile.mode, BuildMode.profile);
    expect(BuildInfo.profile.trackWidgetCreation, false);

    expect(BuildInfo.release.mode, BuildMode.release);
    expect(BuildInfo.release.trackWidgetCreation, false);
  });

121 122 123 124 125 126 127 128 129 130
  testWithoutContext('toBuildSystemEnvironment encoding of standard values', () {
    const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
      treeShakeIcons: true,
      trackWidgetCreation: true,
      dartDefines: <String>['foo=2', 'bar=2'],
      dartObfuscation: true,
      splitDebugInfoPath: 'foo/',
      extraFrontEndOptions: <String>['--enable-experiment=non-nullable', 'bar'],
      extraGenSnapshotOptions: <String>['--enable-experiment=non-nullable', 'fizz'],
      bundleSkSLPath: 'foo/bar/baz.sksl.json',
131
      packagesPath: 'foo/.dart_tool/package_config.json',
132 133 134
      codeSizeDirectory: 'foo/code-size',
      fileSystemRoots: <String>['test5', 'test6'],
      fileSystemScheme: 'scheme',
135 136
      buildName: '122',
      buildNumber: '22'
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    );

    expect(buildInfo.toBuildSystemEnvironment(), <String, String>{
      'BuildMode': 'debug',
      'DartDefines': 'Zm9vPTI=,YmFyPTI=',
      'DartObfuscation': 'true',
      'ExtraFrontEndOptions': '--enable-experiment=non-nullable,bar',
      'ExtraGenSnapshotOptions': '--enable-experiment=non-nullable,fizz',
      'SplitDebugInfo': 'foo/',
      'TrackWidgetCreation': 'true',
      'TreeShakeIcons': 'true',
      'BundleSkSLPath': 'foo/bar/baz.sksl.json',
      'CodeSizeDirectory': 'foo/code-size',
      'FileSystemRoots': 'test5,test6',
      'FileSystemScheme': 'scheme',
152 153
      'BuildName': '122',
      'BuildNumber': '22',
154 155 156
    });
  });

157
  testWithoutContext('toEnvironmentConfig encoding of standard values', () {
158 159 160 161 162 163 164 165
    const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
      treeShakeIcons: true,
      trackWidgetCreation: true,
      dartDefines: <String>['foo=2', 'bar=2'],
      dartObfuscation: true,
      splitDebugInfoPath: 'foo/',
      extraFrontEndOptions: <String>['--enable-experiment=non-nullable', 'bar'],
      extraGenSnapshotOptions: <String>['--enable-experiment=non-nullable', 'fizz'],
166
      bundleSkSLPath: 'foo/bar/baz.sksl.json',
167
      packagesPath: 'foo/.dart_tool/package_config.json',
168
      codeSizeDirectory: 'foo/code-size',
169 170
      // These values are ignored by toEnvironmentConfig
      androidProjectArgs: <String>['foo=bar', 'fizz=bazz']
171 172 173 174 175
    );

    expect(buildInfo.toEnvironmentConfig(), <String, String>{
      'TREE_SHAKE_ICONS': 'true',
      'TRACK_WIDGET_CREATION': 'true',
176
      'DART_DEFINES': 'Zm9vPTI=,YmFyPTI=',
177 178
      'DART_OBFUSCATION': 'true',
      'SPLIT_DEBUG_INFO': 'foo/',
179 180
      'EXTRA_FRONT_END_OPTIONS': '--enable-experiment=non-nullable,bar',
      'EXTRA_GEN_SNAPSHOT_OPTIONS': '--enable-experiment=non-nullable,fizz',
181
      'BUNDLE_SKSL_PATH': 'foo/bar/baz.sksl.json',
182
      'PACKAGE_CONFIG': 'foo/.dart_tool/package_config.json',
183
      'CODE_SIZE_DIRECTORY': 'foo/code-size',
184 185
    });
  });
186

187 188 189 190 191 192 193 194 195 196
  testWithoutContext('toGradleConfig encoding of standard values', () {
    const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
      treeShakeIcons: true,
      trackWidgetCreation: true,
      dartDefines: <String>['foo=2', 'bar=2'],
      dartObfuscation: true,
      splitDebugInfoPath: 'foo/',
      extraFrontEndOptions: <String>['--enable-experiment=non-nullable', 'bar'],
      extraGenSnapshotOptions: <String>['--enable-experiment=non-nullable', 'fizz'],
      bundleSkSLPath: 'foo/bar/baz.sksl.json',
197
      packagesPath: 'foo/.dart_tool/package_config.json',
198
      codeSizeDirectory: 'foo/code-size',
199
      androidProjectArgs: <String>['foo=bar', 'fizz=bazz']
200 201 202 203 204 205 206 207 208 209 210
    );

    expect(buildInfo.toGradleConfig(), <String>[
      '-Pdart-defines=Zm9vPTI=,YmFyPTI=',
      '-Pdart-obfuscation=true',
      '-Pextra-front-end-options=--enable-experiment=non-nullable,bar',
      '-Pextra-gen-snapshot-options=--enable-experiment=non-nullable,fizz',
      '-Psplit-debug-info=foo/',
      '-Ptrack-widget-creation=true',
      '-Ptree-shake-icons=true',
      '-Pbundle-sksl-path=foo/bar/baz.sksl.json',
211 212
      '-Pcode-size-directory=foo/code-size',
      '-Pfoo=bar',
213
      '-Pfizz=bazz',
214 215 216
    ]);
  });

217
  testWithoutContext('encodeDartDefines encodes define values with base64 encoded components', () {
218 219 220 221 222
    expect(encodeDartDefines(<String>['"hello"']), 'ImhlbGxvIg==');
    expect(encodeDartDefines(<String>['https://www.google.com']), 'aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbQ==');
    expect(encodeDartDefines(<String>['2,3,4', '5']), 'MiwzLDQ=,NQ==');
    expect(encodeDartDefines(<String>['true', 'false', 'flase']), 'dHJ1ZQ==,ZmFsc2U=,Zmxhc2U=');
    expect(encodeDartDefines(<String>['1232,456', '2']), 'MTIzMiw0NTY=,Mg==');
223 224
  });

225
  testWithoutContext('decodeDartDefines decodes base64 encoded dart defines', () {
226
    expect(decodeDartDefines(<String, String>{
227
      kDartDefines: 'ImhlbGxvIg==',
228
    }, kDartDefines), <String>['"hello"']);
229
    expect(decodeDartDefines(<String, String>{
230
      kDartDefines: 'aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbQ==',
231
    }, kDartDefines), <String>['https://www.google.com']);
232
    expect(decodeDartDefines(<String, String>{
233
      kDartDefines: 'MiwzLDQ=,NQ==',
234
    }, kDartDefines), <String>['2,3,4', '5']);
235
    expect(decodeDartDefines(<String, String>{
236
      kDartDefines: 'dHJ1ZQ==,ZmFsc2U=,Zmxhc2U=',
237
    }, kDartDefines), <String>['true', 'false', 'flase']);
238
    expect(decodeDartDefines(<String, String>{
239
      kDartDefines: 'MTIzMiw0NTY=,Mg==',
240
    }, kDartDefines), <String>['1232,456', '2']);
241
  });
242
}