build_web_test.dart 6.43 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
import 'package:flutter_tools/src/base/platform.dart';
9
import 'package:flutter_tools/src/build_info.dart';
10
import 'package:flutter_tools/src/build_system/build_system.dart';
11
import 'package:flutter_tools/src/cache.dart';
12
import 'package:flutter_tools/src/commands/build.dart';
13
import 'package:flutter_tools/src/commands/build_web.dart';
14
import 'package:flutter_tools/src/dart/pub.dart';
15
import 'package:flutter_tools/src/features.dart';
16
import 'package:flutter_tools/src/globals.dart' as globals;
17 18 19 20
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/web/compile.dart';
import 'package:mockito/mockito.dart';

21
import '../../src/common.dart';
22
import '../../src/context.dart';
23
import '../../src/mocks.dart';
24
import '../../src/testbed.dart';
25 26

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

  setUpAll(() {
36
    Cache.flutterRoot = '';
37 38 39 40
    Cache.disableLocking();
  });

  setUp(() {
41 42 43 44 45 46 47
    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);
48 49
  });

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

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

69
  testUsingContext('Refuses to build a debug build for web', () async {
70 71 72
    final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());

    expect(() => runner.run(<String>['build', 'web', '--debug']),
73
      throwsA(isA<UsageException>()));
74
  }, overrides: <Type, Generator>{
75 76
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
77
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
78 79 80
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
81

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

85 86 87 88
    expect(
      () => runner.run(<String>['build', 'web']),
      throwsToolExit(),
    );
89
  }, overrides: <Type, Generator>{
90 91
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
92
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
93 94 95
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
96

97
  testUsingContext('Builds a web bundle - end to end', () async {
98 99 100
    final BuildCommand buildCommand = BuildCommand();
    applyMocksToCommand(buildCommand);
    final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
101
    final List<String> dependencies = <String>[
102 103 104 105 106
      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 '),
107
    ];
108
    for (final String dependency in dependencies) {
109
      fileSystem.file(dependency).createSync(recursive: true);
110 111 112
    }

    // Project files.
113
    fileSystem.file('.packages')
114
      .writeAsStringSync('''
115 116 117
foo:lib/
fizz:bar/lib/
''');
118
    fileSystem.file('pubspec.yaml')
119
      .writeAsStringSync('''
120 121 122 123 124 125 126 127 128
name: foo

dependencies:
  flutter:
    sdk: flutter
  fizz:
    path:
      bar/
''');
129
    fileSystem.file(fileSystem.path.join('bar', 'pubspec.yaml'))
130 131 132 133 134 135 136 137 138 139 140
      ..createSync(recursive: true)
      ..writeAsStringSync('''
name: bar

flutter:
  plugin:
    platforms:
      web:
        pluginClass: UrlLauncherPlugin
        fileName: url_launcher_web.dart
''');
141
    fileSystem.file(fileSystem.path.join('bar', 'lib', 'url_launcher_web.dart'))
142 143 144 145
      ..createSync(recursive: true)
      ..writeAsStringSync('''
class UrlLauncherPlugin {}
''');
146
    fileSystem.file(fileSystem.path.join('lib', 'main.dart'))
147
      .writeAsStringSync('void main() { }');
148 149 150

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

156
    expect(fileSystem.file(fileSystem.path.join('lib', 'generated_plugin_registrant.dart')).existsSync(), true);
157
  }, overrides: <Type, Generator>{
158 159
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
160
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
161 162
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
163
    BuildSystem: () => MockBuildSystem(),
164
  });
165

166
  testUsingContext('hidden if feature flag is not enabled', () async {
167
    expect(BuildWebCommand(verboseHelp: false).hidden, true);
168
  }, overrides: <Type, Generator>{
169 170
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
171
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
172 173 174
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
175

176
  testUsingContext('not hidden if feature flag is enabled', () async {
177
    expect(BuildWebCommand(verboseHelp: false).hidden, false);
178
  }, overrides: <Type, Generator>{
179 180
    Platform: () => fakePlatform,
    FileSystem: () => fileSystem,
181
    FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
182 183 184
    Pub: () => MockPub(),
    ProcessManager: () => FakeProcessManager.any(),
  });
185 186
}

187
class MockBuildSystem extends Mock implements BuildSystem {}
188
class MockPub extends Mock implements Pub {}