gradle_find_bundle_test.dart 16.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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 'package:file/memory.dart';
import 'package:flutter_tools/src/android/gradle.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/reporting/reporting.dart';
12
import 'package:unified_analytics/unified_analytics.dart';
13 14

import '../../src/common.dart';
15
import '../../src/fakes.dart';
16 17

void main() {
18
  late FileSystem fileSystem;
19
  late FakeAnalytics fakeAnalytics;
20 21 22

  setUp(() {
    fileSystem = MemoryFileSystem.test();
23 24 25 26
    fakeAnalytics = getInitializedFakeAnalyticsInstance(
      fs: fileSystem,
      fakeFlutterVersion: FakeFlutterVersion(),
    );
27 28
  });

29 30 31 32 33 34 35
  testWithoutContext('Finds app bundle when flavor contains multiple dimensions in release mode', () {
    final FlutterProject project = generateFakeAppBundle('fooBarRelease', 'app-foo-bar-release.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'fooBar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
36
      fakeAnalytics,
37 38 39 40 41 42
    );

    expect(bundle, isNotNull);
    expect(bundle.path, '/build/app/outputs/bundle/fooBarRelease/app-foo-bar-release.aab');
  });

43 44 45 46 47 48 49
  testWithoutContext('Finds app bundle when flavor contains underscores in release mode', () {
    final FlutterProject project = generateFakeAppBundle('foo_barRelease', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'foo_bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
50
      fakeAnalytics,
51 52 53
    );

    expect(bundle, isNotNull);
54
    expect(bundle.path, '/build/app/outputs/bundle/foo_barRelease/app.aab');
55 56 57 58 59 60 61 62 63
  });

  testWithoutContext('Finds app bundle when flavor contains underscores and uppercase letters in release mode', () {
    final FlutterProject project = generateFakeAppBundle('foo_barRelease', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'foo_Bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
64
      fakeAnalytics,
65 66 67
    );

    expect(bundle, isNotNull);
68
    expect(bundle.path, '/build/app/outputs/bundle/foo_barRelease/app.aab');
69 70 71 72 73 74 75 76 77
  });

  testWithoutContext("Finds app bundle when flavor doesn't contain underscores in release mode", () {
    final FlutterProject project = generateFakeAppBundle('fooRelease', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'foo', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
78
      fakeAnalytics,
79 80 81
    );

    expect(bundle, isNotNull);
82
    expect(bundle.path, '/build/app/outputs/bundle/fooRelease/app.aab');
83 84 85 86 87 88 89 90 91
  });

  testWithoutContext("Finds app bundle when flavor doesn't contain underscores but contains uppercase letters in release mode", () {
    final FlutterProject project = generateFakeAppBundle('fooaRelease', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'fooA', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
92
      fakeAnalytics,
93 94 95
    );

    expect(bundle, isNotNull);
96
    expect(bundle.path, '/build/app/outputs/bundle/fooaRelease/app.aab');
97 98 99 100 101 102 103 104 105
  });

  testWithoutContext('Finds app bundle when no flavor is used in release mode', () {
    final FlutterProject project = generateFakeAppBundle('release', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, null, treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
106
      fakeAnalytics,
107 108 109
    );

    expect(bundle, isNotNull);
110
    expect(bundle.path, '/build/app/outputs/bundle/release/app.aab');
111 112
  });

113 114 115 116 117 118 119
   testWithoutContext('Finds app bundle when flavor contains multiple dimensions in debug mode', () {
    final FlutterProject project = generateFakeAppBundle('fooBarDebug', 'app-foo-bar-debug.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.debug, 'fooBar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
120
      fakeAnalytics,
121 122 123 124 125 126
    );

    expect(bundle, isNotNull);
    expect(bundle.path, '/build/app/outputs/bundle/fooBarDebug/app-foo-bar-debug.aab');
  });

127 128 129 130 131 132 133
  testWithoutContext('Finds app bundle when flavor contains underscores in debug mode', () {
    final FlutterProject project = generateFakeAppBundle('foo_barDebug', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.debug, 'foo_bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
134
      fakeAnalytics,
135 136 137
    );

    expect(bundle, isNotNull);
138
    expect(bundle.path, '/build/app/outputs/bundle/foo_barDebug/app.aab');
139 140 141 142 143 144 145 146 147
  });

  testWithoutContext('Finds app bundle when flavor contains underscores and uppercase letters in debug mode', () {
    final FlutterProject project = generateFakeAppBundle('foo_barDebug', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.debug, 'foo_Bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
148
      fakeAnalytics,
149 150 151
    );

    expect(bundle, isNotNull);
152
    expect(bundle.path, '/build/app/outputs/bundle/foo_barDebug/app.aab');
153 154 155 156 157 158 159 160 161
  });

  testWithoutContext("Finds app bundle when flavor doesn't contain underscores in debug mode", () {
    final FlutterProject project = generateFakeAppBundle('fooDebug', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.debug, 'foo', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
162
      fakeAnalytics,
163 164 165
    );

    expect(bundle, isNotNull);
166
    expect(bundle.path, '/build/app/outputs/bundle/fooDebug/app.aab');
167 168 169 170 171 172 173 174 175
  });

  testWithoutContext("Finds app bundle when flavor doesn't contain underscores but contains uppercase letters in debug mode", () {
    final FlutterProject project = generateFakeAppBundle('fooaDebug', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.debug, 'fooA', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
176
      fakeAnalytics,
177 178 179
    );

    expect(bundle, isNotNull);
180
    expect(bundle.path, '/build/app/outputs/bundle/fooaDebug/app.aab');
181 182 183 184 185 186 187 188 189
  });

  testWithoutContext('Finds app bundle when no flavor is used in debug mode', () {
    final FlutterProject project = generateFakeAppBundle('debug', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      BuildInfo.debug,
      BufferLogger.test(),
      TestUsage(),
190
      fakeAnalytics,
191 192 193
    );

    expect(bundle, isNotNull);
194
    expect(bundle.path, '/build/app/outputs/bundle/debug/app.aab');
195 196
  });

197 198 199 200 201 202 203
  testWithoutContext('Finds app bundle when flavor contains multiple dimensions in profile mode', () {
    final FlutterProject project = generateFakeAppBundle('fooBarProfile', 'app-foo-bar-profile.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, 'fooBar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
204
      fakeAnalytics,
205 206 207 208 209 210
    );

    expect(bundle, isNotNull);
    expect(bundle.path, '/build/app/outputs/bundle/fooBarProfile/app-foo-bar-profile.aab');
  });

211 212 213 214 215 216 217
  testWithoutContext('Finds app bundle when flavor contains underscores in profile mode', () {
    final FlutterProject project = generateFakeAppBundle('foo_barProfile', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, 'foo_bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
218
      fakeAnalytics,
219 220 221
    );

    expect(bundle, isNotNull);
222
    expect(bundle.path, '/build/app/outputs/bundle/foo_barProfile/app.aab');
223 224 225 226 227 228 229 230 231
  });

  testWithoutContext('Finds app bundle when flavor contains underscores and uppercase letters in profile mode', () {
    final FlutterProject project = generateFakeAppBundle('foo_barProfile', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, 'foo_Bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
232
      fakeAnalytics,
233 234 235
    );

    expect(bundle, isNotNull);
236
    expect(bundle.path, '/build/app/outputs/bundle/foo_barProfile/app.aab');
237 238 239 240 241 242 243 244 245
  });

  testWithoutContext("Finds app bundle when flavor doesn't contain underscores in profile mode", () {
    final FlutterProject project = generateFakeAppBundle('fooProfile', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, 'foo', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
246
      fakeAnalytics,
247 248 249
    );

    expect(bundle, isNotNull);
250
    expect(bundle.path, '/build/app/outputs/bundle/fooProfile/app.aab');
251 252 253 254 255 256 257 258 259
  });

  testWithoutContext("Finds app bundle when flavor doesn't contain underscores but contains uppercase letters in profile mode", () {
    final FlutterProject project = generateFakeAppBundle('fooaProfile', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, 'fooA', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
260
      fakeAnalytics,
261 262 263
    );

    expect(bundle, isNotNull);
264
    expect(bundle.path, '/build/app/outputs/bundle/fooaProfile/app.aab');
265 266 267 268 269 270 271 272 273
  });

  testWithoutContext('Finds app bundle when no flavor is used in profile mode', () {
    final FlutterProject project = generateFakeAppBundle('profile', 'app.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, null, treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
274
      fakeAnalytics,
275 276 277
    );

    expect(bundle, isNotNull);
278
    expect(bundle.path, '/build/app/outputs/bundle/profile/app.aab');
279 280 281 282 283 284 285 286 287
  });

  testWithoutContext('Finds app bundle in release mode - Gradle 3.5', () {
    final FlutterProject project = generateFakeAppBundle('release', 'app-release.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, null, treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
288
      fakeAnalytics,
289 290 291
    );

    expect(bundle, isNotNull);
292
    expect(bundle.path, '/build/app/outputs/bundle/release/app-release.aab');
293 294 295 296 297 298 299 300 301
  });

  testWithoutContext('Finds app bundle in profile mode - Gradle 3.5', () {
    final FlutterProject project = generateFakeAppBundle('profile', 'app-profile.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, null, treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
302
      fakeAnalytics,
303 304 305
    );

    expect(bundle, isNotNull);
306
    expect(bundle.path, '/build/app/outputs/bundle/profile/app-profile.aab');
307 308 309 310 311 312 313 314 315
  });

  testWithoutContext('Finds app bundle in debug mode - Gradle 3.5', () {
    final FlutterProject project = generateFakeAppBundle('debug', 'app-debug.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      BuildInfo.debug,
      BufferLogger.test(),
      TestUsage(),
316
      fakeAnalytics,
317 318 319
    );

    expect(bundle, isNotNull);
320
    expect(bundle.path, '/build/app/outputs/bundle/debug/app-debug.aab');
321 322 323 324 325 326 327 328 329
  });

  testWithoutContext('Finds app bundle when flavor contains underscores in release mode - Gradle 3.5', () {
    final FlutterProject project = generateFakeAppBundle('foo_barRelease', 'app-foo_bar-release.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'foo_bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
330
      fakeAnalytics,
331 332 333
    );

    expect(bundle, isNotNull);
334
    expect(bundle.path, '/build/app/outputs/bundle/foo_barRelease/app-foo_bar-release.aab');
335 336 337 338 339 340 341 342 343
  });

  testWithoutContext('Finds app bundle when flavor contains underscores and uppercase letters in release mode - Gradle 3.5', () {
    final FlutterProject project = generateFakeAppBundle('foo_barRelease', 'app-foo_bar-release.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'foo_Bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
344
      fakeAnalytics,
345 346 347
    );

    expect(bundle, isNotNull);
348
    expect(bundle.path, '/build/app/outputs/bundle/foo_barRelease/app-foo_bar-release.aab');
349 350 351 352 353 354 355 356 357
  });

  testWithoutContext('Finds app bundle when flavor contains underscores in profile mode - Gradle 3.5', () {
    final FlutterProject project = generateFakeAppBundle('foo_barProfile', 'app-foo_bar-profile.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.profile, 'foo_bar', treeShakeIcons: false),
      BufferLogger.test(),
    TestUsage(),
358
    fakeAnalytics,
359 360 361
    );

    expect(bundle, isNotNull);
362
    expect(bundle.path, '/build/app/outputs/bundle/foo_barProfile/app-foo_bar-profile.aab');
363 364 365 366 367 368 369 370 371
  });

  testWithoutContext('Finds app bundle when flavor contains underscores and uppercase letters in debug mode - Gradle 3.5', () {
    final FlutterProject project = generateFakeAppBundle('foo_barDebug', 'app-foo_bar-debug.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.debug, 'foo_Bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
372
      fakeAnalytics,
373 374 375
    );

    expect(bundle, isNotNull);
376
    expect(bundle.path, '/build/app/outputs/bundle/foo_barDebug/app-foo_bar-debug.aab');
377 378
  });

379 380 381 382 383 384 385 386
  testWithoutContext(
      'Finds app bundle when flavor contains underscores and uppercase letters in release mode - Gradle 4.1', () {
    final FlutterProject project = generateFakeAppBundle('foo_BarRelease', 'app-foo_Bar-release.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.release, 'Foo_Bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
387
      fakeAnalytics,
388 389 390 391 392 393 394 395 396 397 398 399 400 401
    );

    expect(bundle, isNotNull);
    expect(bundle.path, '/build/app/outputs/bundle/foo_BarRelease/app-foo_Bar-release.aab');
  });

  testWithoutContext(
      'Finds app bundle when flavor contains underscores and uppercase letters in debug mode - Gradle 4.1', () {
    final FlutterProject project = generateFakeAppBundle('foo_BarDebug', 'app-foo_Bar-debug.aab', fileSystem);
    final File bundle = findBundleFile(
      project,
      const BuildInfo(BuildMode.debug, 'Foo_Bar', treeShakeIcons: false),
      BufferLogger.test(),
      TestUsage(),
402
      fakeAnalytics,
403 404 405 406 407 408
    );

    expect(bundle, isNotNull);
    expect(bundle.path, '/build/app/outputs/bundle/foo_BarDebug/app-foo_Bar-debug.aab');
  });

409
  testWithoutContext('AAB not found', () {
410 411 412 413 414 415 416 417 418
    final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
    final TestUsage testUsage = TestUsage();
    expect(
      () {
        findBundleFile(
          project,
          const BuildInfo(BuildMode.debug, 'foo_bar', treeShakeIcons: false),
          BufferLogger.test(),
          testUsage,
419
          fakeAnalytics,
420 421 422 423 424 425 426 427 428
        );
      },
      throwsToolExit(
        message:
          "Gradle build failed to produce an .aab file. It's likely that this file "
          "was generated under ${project.android.buildDirectory.path}, but the tool couldn't find it."
      )
    );
    expect(testUsage.events, contains(
429
      TestUsageEvent(
430
        'build',
431
        'gradle',
432
        label: 'gradle-expected-file-not-found',
433
        parameters: CustomDimensions.fromMap(<String, String> {
434
          'cd37': 'androidGradlePluginVersion: 7.6.3, fileExtension: .aab',
435
        }),
436 437
      ),
    ));
438 439 440 441 442 443 444
    expect(fakeAnalytics.sentEvents, hasLength(1));
    expect(
      fakeAnalytics.sentEvents,
      contains(
        Event.flutterBuildInfo(
          label: 'gradle-expected-file-not-found',
          buildType: 'gradle',
445
          settings: 'androidGradlePluginVersion: 7.6.3, fileExtension: .aab',
446 447 448
        ),
      ),
    );
449 450 451 452 453
  });
}

/// Generates a fake app bundle at the location [directoryName]/[fileName].
FlutterProject generateFakeAppBundle(String directoryName, String fileName, FileSystem fileSystem) {
454
  final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
455 456 457 458 459 460 461 462 463 464 465 466

  final Directory bundleDirectory = getBundleDirectory(project);
  bundleDirectory
    .childDirectory(directoryName)
    .createSync(recursive: true);

  bundleDirectory
    .childDirectory(directoryName)
    .childFile(fileName)
    .createSync();
  return project;
}