build_web_test.dart 7.22 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:args/command_runner.dart';
6 7
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
8 9
import 'package:platform/platform.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/cache.dart';
13
import 'package:flutter_tools/src/commands/build.dart';
14
import 'package:flutter_tools/src/commands/build_web.dart';
15
import 'package:flutter_tools/src/dart/pub.dart';
16
import 'package:flutter_tools/src/device.dart';
17
import 'package:flutter_tools/src/features.dart';
18
import 'package:flutter_tools/src/globals.dart' as globals;
19
import 'package:flutter_tools/src/project.dart';
20
import 'package:flutter_tools/src/build_runner/resident_web_runner.dart';
21 22 23
import 'package:flutter_tools/src/web/compile.dart';
import 'package:mockito/mockito.dart';

24
import '../../src/common.dart';
25
import '../../src/context.dart';
26
import '../../src/mocks.dart';
27
import '../../src/testbed.dart';
28 29

void main() {
30 31 32 33 34 35 36
  FileSystem fileSystem;
  final Platform fakePlatform = FakePlatform(
    operatingSystem: 'linux',
    environment: <String, String>{
      'FLUTTER_ROOT': '/'
    }
  );
37 38

  setUpAll(() {
39
    Cache.flutterRoot = '';
40 41 42 43
    Cache.disableLocking();
  });

  setUp(() {
44 45 46 47 48 49 50
    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);
51 52
  });

53 54
  testUsingContext('Refuses to build for web when missing index.html', () async {
    fileSystem.file(fileSystem.path.join('web', 'index.html')).deleteSync();
55 56 57

    expect(buildWeb(
      FlutterProject.current(),
58
      fileSystem.path.join('lib', 'main.dart'),
59
      BuildInfo.debug,
60
      false,
61
      false,
Dan Field's avatar
Dan Field committed
62
    ), throwsToolExit());
63 64 65 66 67 68 69
  }, overrides: <Type, Generator>{
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
70

71 72
  testUsingContext('Refuses to build using runner when missing index.html', () async {
    fileSystem.file(fileSystem.path.join('web', 'index.html')).deleteSync();
73

74
    final ResidentWebRunner runner = DwdsWebRunnerFactory().createWebRunner(
75
      null,
76 77 78
      flutterProject: FlutterProject.current(),
      ipv6: false,
      debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
79
      stayResident: true,
80
      urlTunneller: null,
81
    ) as ResidentWebRunner;
82
    expect(await runner.run(), 1);
83 84 85 86 87 88 89
  }, overrides: <Type, Generator>{
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
90

91
  testUsingContext('Refuses to build a debug build for web', () async {
92 93 94
    final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());

    expect(() => runner.run(<String>['build', 'web', '--debug']),
95
      throwsA(isA<UsageException>()));
96
  }, overrides: <Type, Generator>{
97 98
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
99
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
100 101 102
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
103

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

107 108 109 110
    expect(
      () => runner.run(<String>['build', 'web']),
      throwsToolExit(),
    );
111
  }, overrides: <Type, Generator>{
112 113
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
114
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
115 116 117
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
118

119
  testUsingContext('Builds a web bundle - end to end', () async {
120 121 122
    final BuildCommand buildCommand = BuildCommand();
    applyMocksToCommand(buildCommand);
    final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
123
    final List<String> dependencies = <String>[
124 125 126 127 128
      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 '),
129
    ];
130
    for (final String dependency in dependencies) {
131
      fileSystem.file(dependency).createSync(recursive: true);
132 133 134
    }

    // Project files.
135
    fileSystem.file('.packages')
136
      .writeAsStringSync('''
137 138 139
foo:lib/
fizz:bar/lib/
''');
140
    fileSystem.file('pubspec.yaml')
141
      .writeAsStringSync('''
142 143 144 145 146 147 148 149 150
name: foo

dependencies:
  flutter:
    sdk: flutter
  fizz:
    path:
      bar/
''');
151
    fileSystem.file(fileSystem.path.join('bar', 'pubspec.yaml'))
152 153 154 155 156 157 158 159 160 161 162
      ..createSync(recursive: true)
      ..writeAsStringSync('''
name: bar

flutter:
  plugin:
    platforms:
      web:
        pluginClass: UrlLauncherPlugin
        fileName: url_launcher_web.dart
''');
163
    fileSystem.file(fileSystem.path.join('bar', 'lib', 'url_launcher_web.dart'))
164 165 166 167
      ..createSync(recursive: true)
      ..writeAsStringSync('''
class UrlLauncherPlugin {}
''');
168
    fileSystem.file(fileSystem.path.join('lib', 'main.dart'))
169
      .writeAsStringSync('void main() { }');
170 171 172

    // Process calls. We're not testing that these invocations are correct because
    // that is covered in targets/web_test.dart.
173
    when(globals.buildSystem.build(any, any)).thenAnswer((Invocation invocation) async {
174 175 176 177
      return BuildResult(success: true);
    });
    await runner.run(<String>['build', 'web']);

178
    expect(fileSystem.file(fileSystem.path.join('lib', 'generated_plugin_registrant.dart')).existsSync(), true);
179
  }, overrides: <Type, Generator>{
180 181
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
182
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
183 184
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
185
    BuildSystem: () => MockBuildSystem(),
186
  });
187

188
  testUsingContext('hidden if feature flag is not enabled', () async {
189 190
    expect(BuildWebCommand().hidden, true);
  }, overrides: <Type, Generator>{
191 192
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
193
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
194 195 196
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
197

198
  testUsingContext('not hidden if feature flag is enabled', () async {
199 200
    expect(BuildWebCommand().hidden, false);
  }, overrides: <Type, Generator>{
201 202
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
203
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
204 205 206
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
207 208
}

209
class MockBuildSystem extends Mock implements BuildSystem {}
210
class MockPub extends Mock implements Pub {}