build_info_test.dart 9.09 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
  testWithoutContext('getNameForTargetPlatform on Darwin arches', () {
84 85 86 87 88
    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')));
  });
89

90
  testWithoutContext('getIOSArchForName on Darwin arches', () {
91 92 93 94
    expect(getIOSArchForName('armv7'), DarwinArch.armv7);
    expect(getIOSArchForName('arm64'), DarwinArch.arm64);
    expect(getIOSArchForName('arm64e'), DarwinArch.arm64);
    expect(getIOSArchForName('x86_64'), DarwinArch.x86_64);
95
    expect(() => getIOSArchForName('bogus'), throwsException);
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
  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',
      packagesPath: 'foo/.packages',
      codeSizeDirectory: 'foo/code-size',
      fileSystemRoots: <String>['test5', 'test6'],
      fileSystemScheme: 'scheme',
    );

    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',
    });
  });

130
  testWithoutContext('toEnvironmentConfig encoding of standard values', () {
131 132 133 134 135 136 137 138
    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'],
139
      bundleSkSLPath: 'foo/bar/baz.sksl.json',
140
      packagesPath: 'foo/.packages',
141
      codeSizeDirectory: 'foo/code-size',
142 143
      // These values are ignored by toEnvironmentConfig
      androidProjectArgs: <String>['foo=bar', 'fizz=bazz']
144 145 146 147 148
    );

    expect(buildInfo.toEnvironmentConfig(), <String, String>{
      'TREE_SHAKE_ICONS': 'true',
      'TRACK_WIDGET_CREATION': 'true',
149
      'DART_DEFINES': 'Zm9vPTI=,YmFyPTI=',
150 151
      'DART_OBFUSCATION': 'true',
      'SPLIT_DEBUG_INFO': 'foo/',
152 153
      'EXTRA_FRONT_END_OPTIONS': '--enable-experiment=non-nullable,bar',
      'EXTRA_GEN_SNAPSHOT_OPTIONS': '--enable-experiment=non-nullable,fizz',
154
      'BUNDLE_SKSL_PATH': 'foo/bar/baz.sksl.json',
155
      'PACKAGE_CONFIG': 'foo/.packages',
156
      'CODE_SIZE_DIRECTORY': 'foo/code-size',
157 158
    });
  });
159

160 161 162 163 164 165 166 167 168 169 170 171
  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',
      packagesPath: 'foo/.packages',
      codeSizeDirectory: 'foo/code-size',
172
      androidProjectArgs: <String>['foo=bar', 'fizz=bazz']
173 174 175 176 177 178 179 180 181 182 183
    );

    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',
184 185 186
      '-Pcode-size-directory=foo/code-size',
      '-Pfoo=bar',
      '-Pfizz=bazz'
187 188 189
    ]);
  });

190
  testWithoutContext('encodeDartDefines encodes define values with base64 encoded components', () {
191 192 193 194 195
    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==');
196 197
  });

198
  testWithoutContext('decodeDartDefines decodes base64 encoded dart defines', () {
199
    expect(decodeDartDefines(<String, String>{
200
      kDartDefines: 'ImhlbGxvIg=='
201
    }, kDartDefines), <String>['"hello"']);
202
    expect(decodeDartDefines(<String, String>{
203
      kDartDefines: 'aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbQ=='
204
    }, kDartDefines), <String>['https://www.google.com']);
205
    expect(decodeDartDefines(<String, String>{
206
      kDartDefines: 'MiwzLDQ=,NQ=='
207
    }, kDartDefines), <String>['2,3,4', '5']);
208
    expect(decodeDartDefines(<String, String>{
209
      kDartDefines: 'dHJ1ZQ==,ZmFsc2U=,Zmxhc2U='
210
    }, kDartDefines), <String>['true', 'false', 'flase']);
211
    expect(decodeDartDefines(<String, String>{
212
      kDartDefines: 'MTIzMiw0NTY=,Mg=='
213
    }, kDartDefines), <String>['1232,456', '2']);
214
  });
215
}