build_web_test.dart 7.8 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 9
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/platform.dart';
11
import 'package:flutter_tools/src/build_info.dart';
12
import 'package:flutter_tools/src/build_system/build_system.dart';
13
import 'package:flutter_tools/src/cache.dart';
14
import 'package:flutter_tools/src/commands/build.dart';
15
import 'package:flutter_tools/src/commands/build_web.dart';
16
import 'package:flutter_tools/src/features.dart';
17
import 'package:flutter_tools/src/runner/flutter_command.dart';
18

19
import '../../src/common.dart';
20
import '../../src/context.dart';
21
import '../../src/fakes.dart';
22
import '../../src/test_build_system.dart';
23
import '../../src/test_flutter_command_runner.dart';
24 25

void main() {
26 27 28 29 30 31
  FileSystem fileSystem;
  final Platform fakePlatform = FakePlatform(
    environment: <String, String>{
      'FLUTTER_ROOT': '/'
    }
  );
32 33

  setUpAll(() {
34
    Cache.flutterRoot = '';
35 36 37 38
    Cache.disableLocking();
  });

  setUp(() {
39 40 41 42 43 44 45
    fileSystem = MemoryFileSystem.test();
    fileSystem.file('pubspec.yaml')
      ..createSync()
      ..writeAsStringSync('name: foo\n');
    fileSystem.file('.packages').createSync();
    fileSystem.file(fileSystem.path.join('web', 'index.html')).createSync(recursive: true);
    fileSystem.file(fileSystem.path.join('lib', 'main.dart')).createSync(recursive: true);
46 47
  });

48 49
  testUsingContext('Refuses to build for web when missing index.html', () async {
    fileSystem.file(fileSystem.path.join('web', 'index.html')).deleteSync();
50
    final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());
51

52 53 54 55
    expect(
      () => runner.run(<String>['build', 'web', '--no-pub']),
      throwsToolExit(message: 'Missing index.html.')
    );
56 57 58 59 60 61
  }, overrides: <Type, Generator>{
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
    ProcessManager: () => FakeProcessManager.any(),
  });
62

63
  testUsingContext('Refuses to build a debug build for web', () async {
64 65
    final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());

66
    expect(() => runner.run(<String>['build', 'web', '--debug', '--no-pub']),
67
      throwsA(isA<UsageException>()));
68
  }, overrides: <Type, Generator>{
69 70
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
71
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
72 73
    ProcessManager: () => FakeProcessManager.any(),
  });
74

75
  testUsingContext('Refuses to build for web when feature is disabled', () async {
76 77
    final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());

78
    expect(
79
      () => runner.run(<String>['build', 'web', '--no-pub']),
80
      throwsToolExit(message: '"build web" is not currently supported. To enable, run "flutter config --enable-web".')
81
    );
82
  }, overrides: <Type, Generator>{
83 84
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
85
    FeatureFlags: () => TestFeatureFlags(),
86 87
    ProcessManager: () => FakeProcessManager.any(),
  });
88

89
  testUsingContext('Builds a web bundle - end to end', () async {
90 91
    final BuildCommand buildCommand = BuildCommand();
    final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
92
    setupFileSystemForEndToEndTest(fileSystem);
93
    await runner.run(<String>['build', 'web', '--no-pub', '--dart-define=foo=a']);
94

95
    expect(fileSystem.file(fileSystem.path.join('lib', 'generated_plugin_registrant.dart')).existsSync(), true);
96
  }, overrides: <Type, Generator>{
97 98
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
99
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
100
    ProcessManager: () => FakeProcessManager.any(),
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) {
      expect(environment.defines, <String, String>{
        'TargetFile': 'lib/main.dart',
        'HasWebPlugins': 'true',
        'cspMode': 'false',
        'SourceMaps': 'false',
        'NativeNullAssertions': 'true',
        'ServiceWorkerStrategy': 'offline-first',
        'BuildMode': 'release',
        'DartDefines': 'Zm9vPWE=,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==',
        'DartObfuscation': 'false',
        'TrackWidgetCreation': 'false',
        'TreeShakeIcons': 'false',
      });
    }),
116
  });
117

118
  testUsingContext('hidden if feature flag is not enabled', () async {
119
    expect(BuildWebCommand(verboseHelp: false).hidden, true);
120
  }, overrides: <Type, Generator>{
121 122
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
123
    FeatureFlags: () => TestFeatureFlags(),
124 125
    ProcessManager: () => FakeProcessManager.any(),
  });
126

127
  testUsingContext('not hidden if feature flag is enabled', () async {
128
    expect(BuildWebCommand(verboseHelp: false).hidden, false);
129
  }, overrides: <Type, Generator>{
130 131
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
132
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
133 134
    ProcessManager: () => FakeProcessManager.any(),
  });
135 136 137 138 139

  testUsingContext('Defaults to web renderer auto mode when no option is specified', () async {
    final TestWebBuildCommand buildCommand = TestWebBuildCommand();
    final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
    setupFileSystemForEndToEndTest(fileSystem);
140
    await runner.run(<String>['build', 'web', '--no-pub']);
141 142 143 144 145 146 147 148
    final BuildInfo buildInfo =
        await buildCommand.webCommand.getBuildInfo(forcedBuildMode: BuildMode.debug);
    expect(buildInfo.dartDefines, contains('FLUTTER_WEB_AUTO_DETECT=true'));
  }, overrides: <Type, Generator>{
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
    ProcessManager: () => FakeProcessManager.any(),
149
    BuildSystem: () => TestBuildSystem.all(BuildResult(success: true)),
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  });
}

void setupFileSystemForEndToEndTest(FileSystem fileSystem) {
  final List<String> dependencies = <String>[
    fileSystem.path.join('packages', 'flutter_tools', 'lib', 'src', 'build_system', 'targets', 'web.dart'),
    fileSystem.path.join('bin', 'cache', 'flutter_web_sdk'),
    fileSystem.path.join('bin', 'cache', 'dart-sdk', 'bin', 'snapshots', 'dart2js.dart.snapshot'),
    fileSystem.path.join('bin', 'cache', 'dart-sdk', 'bin', 'dart'),
    fileSystem.path.join('bin', 'cache', 'dart-sdk '),
  ];
  for (final String dependency in dependencies) {
    fileSystem.file(dependency).createSync(recursive: true);
  }

  // Project files.
  fileSystem.file('.packages')
      .writeAsStringSync('''
foo:lib/
fizz:bar/lib/
''');
  fileSystem.file('pubspec.yaml')
      .writeAsStringSync('''
name: foo

dependencies:
  flutter:
    sdk: flutter
  fizz:
    path:
      bar/
''');
  fileSystem.file(fileSystem.path.join('bar', 'pubspec.yaml'))
    ..createSync(recursive: true)
    ..writeAsStringSync('''
name: bar

flutter:
  plugin:
    platforms:
      web:
        pluginClass: UrlLauncherPlugin
        fileName: url_launcher_web.dart
''');
  fileSystem.file(fileSystem.path.join('bar', 'lib', 'url_launcher_web.dart'))
    ..createSync(recursive: true)
    ..writeAsStringSync('''
class UrlLauncherPlugin {}
''');
  fileSystem.file(fileSystem.path.join('lib', 'main.dart'))
      .writeAsStringSync('void main() { }');
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
class TestWebBuildCommand extends FlutterCommand {
  TestWebBuildCommand({ bool verboseHelp = false }) :
    webCommand = BuildWebCommand(verboseHelp: verboseHelp) {
    addSubcommand(webCommand);
  }

  final BuildWebCommand webCommand;

  @override
  final String name = 'build';

  @override
  final String description = 'Build a test executable app.';

  @override
  Future<FlutterCommandResult> runCommand() async => null;
219 220 221

  @override
  bool get shouldRunPub => false;
222
}