Unverified Commit 9295b348 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

cleanup of tool build tests (#50904)

parent 1793108b
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:platform/platform.dart'; import 'package:platform/platform.dart';
import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_info.dart';
...@@ -15,18 +17,22 @@ import 'package:flutter_tools/src/device.dart'; ...@@ -15,18 +17,22 @@ import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/features.dart'; import 'package:flutter_tools/src/features.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/build_runner/resident_web_runner.dart'; import 'package:flutter_tools/src/build_runner/resident_web_runner.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:flutter_tools/src/web/compile.dart'; import 'package:flutter_tools/src/web/compile.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/mocks.dart'; import '../../src/mocks.dart';
import '../../src/testbed.dart'; import '../../src/testbed.dart';
void main() { void main() {
Testbed testbed; FileSystem fileSystem;
MockPlatform mockPlatform; final Platform fakePlatform = FakePlatform(
operatingSystem: 'linux',
environment: <String, String>{
'FLUTTER_ROOT': '/'
}
);
setUpAll(() { setUpAll(() {
Cache.flutterRoot = ''; Cache.flutterRoot = '';
...@@ -34,36 +40,36 @@ void main() { ...@@ -34,36 +40,36 @@ void main() {
}); });
setUp(() { setUp(() {
testbed = Testbed(setup: () { fileSystem = MemoryFileSystem.test();
globals.fs.file('pubspec.yaml') fileSystem.file('pubspec.yaml')
..createSync() ..createSync()
..writeAsStringSync('name: foo\n'); ..writeAsStringSync('name: foo\n');
globals.fs.file('.packages').createSync(); fileSystem.file('.packages').createSync();
globals.fs.file(globals.fs.path.join('web', 'index.html')).createSync(recursive: true); fileSystem.file(fileSystem.path.join('web', 'index.html')).createSync(recursive: true);
globals.fs.file(globals.fs.path.join('lib', 'main.dart')).createSync(recursive: true); fileSystem.file(fileSystem.path.join('lib', 'main.dart')).createSync(recursive: true);
}, overrides: <Type, Generator>{
Platform: () => mockPlatform,
FlutterVersion: () => MockFlutterVersion(),
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
Pub: () => MockPub(),
});
}); });
test('Refuses to build for web when missing index.html', () => testbed.run(() async { testUsingContext('Refuses to build for web when missing index.html', () async {
globals.fs.file(globals.fs.path.join('web', 'index.html')).deleteSync(); fileSystem.file(fileSystem.path.join('web', 'index.html')).deleteSync();
expect(buildWeb( expect(buildWeb(
FlutterProject.current(), FlutterProject.current(),
globals.fs.path.join('lib', 'main.dart'), fileSystem.path.join('lib', 'main.dart'),
BuildInfo.debug, BuildInfo.debug,
false, false,
const <String>[], const <String>[],
false, false,
), throwsToolExit()); ), throwsToolExit());
})); }, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
Pub: () => MockPub(),
ProcessManager: () => FakeProcessManager.any(),
});
test('Refuses to build using runner when missing index.html', () => testbed.run(() async { testUsingContext('Refuses to build using runner when missing index.html', () async {
globals.fs.file(globals.fs.path.join('web', 'index.html')).deleteSync(); fileSystem.file(fileSystem.path.join('web', 'index.html')).deleteSync();
final ResidentWebRunner runner = DwdsWebRunnerFactory().createWebRunner( final ResidentWebRunner runner = DwdsWebRunnerFactory().createWebRunner(
null, null,
...@@ -75,48 +81,64 @@ void main() { ...@@ -75,48 +81,64 @@ void main() {
urlTunneller: null, urlTunneller: null,
) as ResidentWebRunner; ) as ResidentWebRunner;
expect(await runner.run(), 1); expect(await runner.run(), 1);
})); }, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
Pub: () => MockPub(),
ProcessManager: () => FakeProcessManager.any(),
});
test('Refuses to build a debug build for web', () => testbed.run(() async { testUsingContext('Refuses to build a debug build for web', () async {
final CommandRunner<void> runner = createTestCommandRunner(BuildCommand()); final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());
expect(() => runner.run(<String>['build', 'web', '--debug']), expect(() => runner.run(<String>['build', 'web', '--debug']),
throwsA(isA<UsageException>())); throwsA(isA<UsageException>()));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true), FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
})); Pub: () => MockPub(),
ProcessManager: () => FakeProcessManager.any(),
});
test('Refuses to build for web when feature is disabled', () => testbed.run(() async { testUsingContext('Refuses to build for web when feature is disabled', () async {
final CommandRunner<void> runner = createTestCommandRunner(BuildCommand()); final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());
expect(() => runner.run(<String>['build', 'web']), expect(
throwsToolExit()); () => runner.run(<String>['build', 'web']),
throwsToolExit(),
);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: false), FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
})); Pub: () => MockPub(),
ProcessManager: () => FakeProcessManager.any(),
});
test('Builds a web bundle - end to end', () => testbed.run(() async { testUsingContext('Builds a web bundle - end to end', () async {
final BuildCommand buildCommand = BuildCommand(); final BuildCommand buildCommand = BuildCommand();
applyMocksToCommand(buildCommand); applyMocksToCommand(buildCommand);
final CommandRunner<void> runner = createTestCommandRunner(buildCommand); final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
final List<String> dependencies = <String>[ final List<String> dependencies = <String>[
globals.fs.path.join('packages', 'flutter_tools', 'lib', 'src', 'build_system', 'targets', 'web.dart'), fileSystem.path.join('packages', 'flutter_tools', 'lib', 'src', 'build_system', 'targets', 'web.dart'),
globals.fs.path.join('bin', 'cache', 'flutter_web_sdk'), fileSystem.path.join('bin', 'cache', 'flutter_web_sdk'),
globals.fs.path.join('bin', 'cache', 'dart-sdk', 'bin', 'snapshots', 'dart2js.dart.snapshot'), fileSystem.path.join('bin', 'cache', 'dart-sdk', 'bin', 'snapshots', 'dart2js.dart.snapshot'),
globals.fs.path.join('bin', 'cache', 'dart-sdk', 'bin', 'dart'), fileSystem.path.join('bin', 'cache', 'dart-sdk', 'bin', 'dart'),
globals.fs.path.join('bin', 'cache', 'dart-sdk '), fileSystem.path.join('bin', 'cache', 'dart-sdk '),
]; ];
for (final String dependency in dependencies) { for (final String dependency in dependencies) {
globals.fs.file(dependency).createSync(recursive: true); fileSystem.file(dependency).createSync(recursive: true);
} }
// Project files. // Project files.
globals.fs.file('.packages') fileSystem.file('.packages')
..writeAsStringSync(''' ..writeAsStringSync('''
foo:lib/ foo:lib/
fizz:bar/lib/ fizz:bar/lib/
'''); ''');
globals.fs.file('pubspec.yaml') fileSystem.file('pubspec.yaml')
..writeAsStringSync(''' ..writeAsStringSync('''
name: foo name: foo
...@@ -127,7 +149,7 @@ dependencies: ...@@ -127,7 +149,7 @@ dependencies:
path: path:
bar/ bar/
'''); ''');
globals.fs.file(globals.fs.path.join('bar', 'pubspec.yaml')) fileSystem.file(fileSystem.path.join('bar', 'pubspec.yaml'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(''' ..writeAsStringSync('''
name: bar name: bar
...@@ -139,12 +161,12 @@ flutter: ...@@ -139,12 +161,12 @@ flutter:
pluginClass: UrlLauncherPlugin pluginClass: UrlLauncherPlugin
fileName: url_launcher_web.dart fileName: url_launcher_web.dart
'''); ''');
globals.fs.file(globals.fs.path.join('bar', 'lib', 'url_launcher_web.dart')) fileSystem.file(fileSystem.path.join('bar', 'lib', 'url_launcher_web.dart'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(''' ..writeAsStringSync('''
class UrlLauncherPlugin {} class UrlLauncherPlugin {}
'''); ''');
globals.fs.file(globals.fs.path.join('lib', 'main.dart')) fileSystem.file(fileSystem.path.join('lib', 'main.dart'))
..writeAsStringSync('void main() { }'); ..writeAsStringSync('void main() { }');
// Process calls. We're not testing that these invocations are correct because // Process calls. We're not testing that these invocations are correct because
...@@ -154,35 +176,36 @@ class UrlLauncherPlugin {} ...@@ -154,35 +176,36 @@ class UrlLauncherPlugin {}
}); });
await runner.run(<String>['build', 'web']); await runner.run(<String>['build', 'web']);
expect(globals.fs.file(globals.fs.path.join('lib', 'generated_plugin_registrant.dart')).existsSync(), true); expect(fileSystem.file(fileSystem.path.join('lib', 'generated_plugin_registrant.dart')).existsSync(), true);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true), FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
Pub: () => MockPub(),
ProcessManager: () => FakeProcessManager.any(),
BuildSystem: () => MockBuildSystem(), BuildSystem: () => MockBuildSystem(),
})); });
test('hidden if feature flag is not enabled', () => testbed.run(() async { testUsingContext('hidden if feature flag is not enabled', () async {
expect(BuildWebCommand().hidden, true); expect(BuildWebCommand().hidden, true);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: false), FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
})); Pub: () => MockPub(),
ProcessManager: () => FakeProcessManager.any(),
});
test('not hidden if feature flag is enabled', () => testbed.run(() async { testUsingContext('not hidden if feature flag is enabled', () async {
expect(BuildWebCommand().hidden, false); expect(BuildWebCommand().hidden, false);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => fakePlatform,
FileSystem: () => fileSystem,
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true), FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
})); Pub: () => MockPub(),
ProcessManager: () => FakeProcessManager.any(),
});
} }
class MockBuildSystem extends Mock implements BuildSystem {} class MockBuildSystem extends Mock implements BuildSystem {}
class MockWebCompilationProxy extends Mock implements WebCompilationProxy {}
class MockPlatform extends Mock implements Platform {
@override
Map<String, String> environment = <String, String>{
'FLUTTER_ROOT': '/',
};
}
class MockFlutterVersion extends Mock implements FlutterVersion {
@override
bool get isMaster => true;
}
class MockPub extends Mock implements Pub {} class MockPub extends Mock implements Pub {}
...@@ -8,7 +8,6 @@ import 'dart:io' as io show ProcessSignal; ...@@ -8,7 +8,6 @@ import 'dart:io' as io show ProcessSignal;
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:process/process.dart'; import 'package:process/process.dart';
import 'common.dart'; import 'common.dart';
export 'package:process/process.dart' show ProcessManager; export 'package:process/process.dart' show ProcessManager;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment