build_bundle_test.dart 10.1 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 6
// @dart = 2.8

7
import 'package:args/command_runner.dart';
8
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/build_info.dart';
11
import 'package:flutter_tools/src/build_system/build_system.dart';
12
import 'package:flutter_tools/src/build_system/targets/common.dart';
13
import 'package:flutter_tools/src/build_system/targets/icon_tree_shaker.dart';
14
import 'package:flutter_tools/src/bundle.dart';
15 16
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/build_bundle.dart';
17
import 'package:flutter_tools/src/features.dart';
18
import 'package:flutter_tools/src/reporting/reporting.dart';
19
import 'package:mockito/mockito.dart';
20
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
21

22 23
import '../../src/common.dart';
import '../../src/context.dart';
24
import '../../src/fakes.dart';
25
import '../../src/test_build_system.dart';
26
import '../../src/test_flutter_command_runner.dart';
27 28 29

void main() {
  Cache.disableLocking();
30 31 32 33
  Directory tempDir;
  MockBundleBuilder mockBundleBuilder;

  setUp(() {
34
    tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.');
35 36 37 38 39

    mockBundleBuilder = MockBundleBuilder();
    when(
      mockBundleBuilder.build(
        platform: anyNamed('platform'),
40
        buildInfo: anyNamed('buildInfo'),
41 42 43 44 45 46 47 48 49 50
        mainPath: anyNamed('mainPath'),
        manifestPath: anyNamed('manifestPath'),
        applicationKernelFilePath: anyNamed('applicationKernelFilePath'),
        depfilePath: anyNamed('depfilePath'),
        assetDirPath: anyNamed('assetDirPath'),
        trackWidgetCreation: anyNamed('trackWidgetCreation'),
        extraFrontEndOptions: anyNamed('extraFrontEndOptions'),
        extraGenSnapshotOptions: anyNamed('extraGenSnapshotOptions'),
        fileSystemRoots: anyNamed('fileSystemRoots'),
        fileSystemScheme: anyNamed('fileSystemScheme'),
51
        treeShakeIcons: anyNamed('treeShakeIcons'),
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      ),
    ).thenAnswer((_) => Future<void>.value());
  });

  tearDown(() {
    tryToDelete(tempDir);
  });

  Future<BuildBundleCommand> runCommandIn(String projectPath, { List<String> arguments }) async {
    final BuildBundleCommand command = BuildBundleCommand(bundleBuilder: mockBundleBuilder);
    final CommandRunner<void> runner = createTestCommandRunner(command);
    await runner.run(<String>[
      'bundle',
      ...?arguments,
      '--target=$projectPath/lib/main.dart',
67
      '--no-pub'
68 69 70 71 72 73 74 75 76 77 78
    ]);
    return command;
  }

  testUsingContext('bundle getUsage indicate that project is a module', () async {
    final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);

    final BuildBundleCommand command = await runCommandIn(projectPath);

    expect(await command.usageValues,
79
        containsPair(CustomDimensions.commandBuildBundleIsModule, 'true'));
80
  });
81 82 83 84 85 86 87 88

  testUsingContext('bundle getUsage indicate that project is not a module', () async {
    final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=app']);

    final BuildBundleCommand command = await runCommandIn(projectPath);

    expect(await command.usageValues,
89
        containsPair(CustomDimensions.commandBuildBundleIsModule, 'false'));
90
  });
91 92 93 94 95 96 97 98

  testUsingContext('bundle getUsage indicate the target platform', () async {
    final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=app']);

    final BuildBundleCommand command = await runCommandIn(projectPath);

    expect(await command.usageValues,
99
        containsPair(CustomDimensions.commandBuildBundleTargetPlatform, 'android-arm'));
100
  });
101 102

  testUsingContext('bundle fails to build for Windows if feature is disabled', () async {
103 104 105
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync(recursive: true);
    globals.fs.file('.packages').createSync(recursive: true);
106 107 108 109 110 111
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand()
        ..bundleBuilder = MockBundleBuilder());

    expect(() => runner.run(<String>[
      'bundle',
      '--no-pub',
112
      '--target-platform=windows-x64',
Dan Field's avatar
Dan Field committed
113
    ]), throwsToolExit());
114
  }, overrides: <Type, Generator>{
115
    FileSystem: () => MemoryFileSystem.test(),
116
    ProcessManager: () => FakeProcessManager.any(),
117 118 119 120
    FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: false),
  });

  testUsingContext('bundle fails to build for Linux if feature is disabled', () async {
121 122 123
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync();
    globals.fs.file('.packages').createSync();
124 125 126 127 128 129
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand()
        ..bundleBuilder = MockBundleBuilder());

    expect(() => runner.run(<String>[
      'bundle',
      '--no-pub',
130
      '--target-platform=linux-x64',
Dan Field's avatar
Dan Field committed
131
    ]), throwsToolExit());
132
  }, overrides: <Type, Generator>{
133
    FileSystem: () => MemoryFileSystem.test(),
134
    ProcessManager: () => FakeProcessManager.any(),
135 136 137 138
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: false),
  });

  testUsingContext('bundle fails to build for macOS if feature is disabled', () async {
139 140 141
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync();
    globals.fs.file('.packages').createSync();
142 143 144 145 146 147
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand()
        ..bundleBuilder = MockBundleBuilder());

    expect(() => runner.run(<String>[
      'bundle',
      '--no-pub',
148
      '--target-platform=darwin-x64',
Dan Field's avatar
Dan Field committed
149
    ]), throwsToolExit());
150
  }, overrides: <Type, Generator>{
151
    FileSystem: () => MemoryFileSystem.test(),
152
    ProcessManager: () => FakeProcessManager.any(),
153 154 155 156
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: false),
  });

  testUsingContext('bundle can build for Windows if feature is enabled', () async {
157 158 159
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync();
    globals.fs.file('.packages').createSync();
160 161 162 163 164 165
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand()
        ..bundleBuilder = MockBundleBuilder());

    await runner.run(<String>[
      'bundle',
      '--no-pub',
166
      '--target-platform=windows-x64',
167 168
    ]);
  }, overrides: <Type, Generator>{
169
    FileSystem: () => MemoryFileSystem.test(),
170
    ProcessManager: () => FakeProcessManager.any(),
171 172 173 174
    FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: true),
  });

  testUsingContext('bundle can build for Linux if feature is enabled', () async {
175 176 177
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync();
    globals.fs.file('.packages').createSync();
178 179 180 181 182 183
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand()
        ..bundleBuilder = MockBundleBuilder());

    await runner.run(<String>[
      'bundle',
      '--no-pub',
184
      '--target-platform=linux-x64',
185 186
    ]);
  }, overrides: <Type, Generator>{
187
    FileSystem: () => MemoryFileSystem.test(),
188
    ProcessManager: () => FakeProcessManager.any(),
189 190
    FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true),
  });
191

192
  testUsingContext('bundle can build for macOS if feature is enabled', () async {
193 194 195
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync();
    globals.fs.file('.packages').createSync();
196 197 198 199 200 201
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand()
        ..bundleBuilder = MockBundleBuilder());

    await runner.run(<String>[
      'bundle',
      '--no-pub',
202
      '--target-platform=darwin-x64',
203 204
    ]);
  }, overrides: <Type, Generator>{
205
    FileSystem: () => MemoryFileSystem.test(),
206
    ProcessManager: () => FakeProcessManager.any(),
207
    FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
208
  });
209 210

  testUsingContext('passes track widget creation through', () async {
211 212 213
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync();
    globals.fs.file('.packages').createSync();
214 215 216 217 218 219 220 221 222 223
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand());

    await runner.run(<String>[
      'bundle',
      '--no-pub',
      '--debug',
      '--target-platform=android-arm',
      '--track-widget-creation'
    ]);
  }, overrides: <Type, Generator>{
224 225 226 227 228 229 230 231 232
    BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) {
      expect(environment.defines, <String, String>{
        kTargetFile: globals.fs.path.join('lib', 'main.dart'),
        kBuildMode: 'debug',
        kTargetPlatform: 'android-arm',
        kTrackWidgetCreation: 'true',
        kIconTreeShakerFlag: null,
      });
    }),
233
    FileSystem: () => MemoryFileSystem.test(),
234
    ProcessManager: () => FakeProcessManager.any(),
235
  });
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

  testUsingContext('passes dart-define through', () async {
    globals.fs.file('lib/main.dart').createSync(recursive: true);
    globals.fs.file('pubspec.yaml').createSync();
    globals.fs.file('.packages').createSync();
    final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand());

    await runner.run(<String>[
      'bundle',
      '--no-pub',
      '--debug',
      '--target-platform=android-arm',
      '--dart-define=foo=bar'
    ]);
  }, overrides: <Type, Generator>{
    BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) {
      expect(environment.defines, <String, String>{
        kTargetFile: globals.fs.path.join('lib', 'main.dart'),
        kBuildMode: 'debug',
        kTargetPlatform: 'android-arm',
        kTrackWidgetCreation: 'true',
        kIconTreeShakerFlag: null,
        kDartDefines: 'Zm9vPWJhcg==',
      });
    }),
    FileSystem: () => MemoryFileSystem.test(),
    ProcessManager: () => FakeProcessManager.any(),
  });
264 265 266
}

class MockBundleBuilder extends Mock implements BundleBuilder {}