bundle_builder_test.dart 6.33 KB
Newer Older
1 2 3 4 5 6 7
// 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.

// @dart = 2.8

import 'package:file/memory.dart';
8
import 'package:flutter_tools/src/base/config.dart';
9 10 11
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/build_system/build_system.dart';
12
import 'package:flutter_tools/src/bundle.dart';
13
import 'package:flutter_tools/src/bundle_builder.dart';
14
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
15
import 'package:flutter_tools/src/project.dart';
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

import '../src/common.dart';
import '../src/context.dart';
import '../src/test_build_system.dart';

// Tests for BundleBuilder.
void main() {
  testUsingContext('Copies assets to expected directory after building', () async {
    final BuildSystem buildSystem = TestBuildSystem.all(
      BuildResult(success: true),
      (Target target, Environment environment) {
        environment.outputDir.childFile('kernel_blob.bin').createSync(recursive: true);
        environment.outputDir.childFile('isolate_snapshot_data').createSync();
        environment.outputDir.childFile('vm_snapshot_data').createSync();
        environment.outputDir.childFile('LICENSE').createSync(recursive: true);
      }
    );

    await BundleBuilder().build(
      platform: TargetPlatform.ios,
36
      buildInfo: BuildInfo.debug,
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
      project: FlutterProject.fromDirectoryTest(globals.fs.currentDirectory),
      mainPath: globals.fs.path.join('lib', 'main.dart'),
      assetDirPath: 'example',
      depfilePath: 'example.d',
      buildSystem: buildSystem
    );
    expect(globals.fs.file(globals.fs.path.join('example', 'kernel_blob.bin')).existsSync(), true);
    expect(globals.fs.file(globals.fs.path.join('example', 'LICENSE')).existsSync(), true);
    expect(globals.fs.file(globals.fs.path.join('example.d')).existsSync(), false);
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
  });

  testUsingContext('Handles build system failure', () {
    expect(
      () => BundleBuilder().build(
        platform: TargetPlatform.ios,
55
        buildInfo: BuildInfo.debug,
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 120 121
        project: FlutterProject.fromDirectoryTest(globals.fs.currentDirectory),
        mainPath: 'lib/main.dart',
        assetDirPath: 'example',
        depfilePath: 'example.d',
        buildSystem: TestBuildSystem.all(BuildResult(success: false))
      ),
      throwsToolExit()
    );
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
  });

  testUsingContext('Passes correct defines to build system', () async {
    final FlutterProject project = FlutterProject.fromDirectoryTest(globals.fs.currentDirectory);
    final String mainPath = globals.fs.path.join('lib', 'main.dart');
    const String assetDirPath = 'example';
    const String depfilePath = 'example.d';
    Environment env;
    final BuildSystem buildSystem = TestBuildSystem.all(
      BuildResult(success: true),
      (Target target, Environment environment) {
        env = environment;
        environment.outputDir.childFile('kernel_blob.bin').createSync(recursive: true);
        environment.outputDir.childFile('isolate_snapshot_data').createSync();
        environment.outputDir.childFile('vm_snapshot_data').createSync();
        environment.outputDir.childFile('LICENSE').createSync(recursive: true);
      }
    );

    await BundleBuilder().build(
      platform: TargetPlatform.ios,
      buildInfo: const BuildInfo(
        BuildMode.debug,
        null,
        trackWidgetCreation: true,
        extraFrontEndOptions: <String>['test1', 'test2'],
        extraGenSnapshotOptions: <String>['test3', 'test4'],
        fileSystemRoots: <String>['test5', 'test6'],
        fileSystemScheme: 'test7',
        dartDefines: <String>['test8', 'test9'],
        treeShakeIcons: true,
      ),
      project: project,
      mainPath: mainPath,
      assetDirPath: assetDirPath,
      depfilePath: depfilePath,
      buildSystem: buildSystem
    );

    expect(env, isNotNull);
    expect(env.defines[kBuildMode], 'debug');
    expect(env.defines[kTargetPlatform], 'ios');
    expect(env.defines[kTargetFile], mainPath);
    expect(env.defines[kTrackWidgetCreation], 'true');
    expect(env.defines[kExtraFrontEndOptions], 'test1,test2');
    expect(env.defines[kExtraGenSnapshotOptions], 'test3,test4');
    expect(env.defines[kFileSystemRoots], 'test5,test6');
    expect(env.defines[kFileSystemScheme], 'test7');
    expect(env.defines[kDartDefines], encodeDartDefines(<String>['test8', 'test9']));
    expect(env.defines[kIconTreeShakerFlag], 'true');
    expect(env.defines[kDeferredComponents], 'false');
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
  });
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

  testWithoutContext('--flutter-widget-cache and --enable-experiment are removed from getDefaultCachedKernelPath hash', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Config config = Config.test();

    expect(getDefaultCachedKernelPath(
      trackWidgetCreation: true,
      dartDefines: <String>[],
      extraFrontEndOptions: <String>['--enable-experiment=foo', '--flutter-widget-cache'],
      fileSystem: fileSystem,
      config: config,
    ), 'build/cache.dill.track.dill');

    expect(getDefaultCachedKernelPath(
      trackWidgetCreation: true,
      dartDefines: <String>['foo=bar'],
      extraFrontEndOptions: <String>['--enable-experiment=foo', '--flutter-widget-cache'],
      fileSystem: fileSystem,
      config: config,
    ), 'build/06ad47d8e64bd28de537b62ff85357c4.cache.dill.track.dill');

    expect(getDefaultCachedKernelPath(
      trackWidgetCreation: false,
      dartDefines: <String>[],
      extraFrontEndOptions: <String>['--enable-experiment=foo', '--flutter-widget-cache'],
      fileSystem: fileSystem,
      config: config,
    ), 'build/cache.dill');

    expect(getDefaultCachedKernelPath(
      trackWidgetCreation: true,
      dartDefines: <String>[],
      extraFrontEndOptions: <String>['--enable-experiment=foo', '--flutter-widget-cache', '--foo=bar'],
      fileSystem: fileSystem,
      config: config,
    ), 'build/95b595cca01caa5f0ca0a690339dd7f6.cache.dill.track.dill');
  });
159
}