Skip to content

Commit 25be1ab

Browse files
committed
[flutter_tools] Bundle data assets from hooks in flutter test
1 parent 62199c4 commit 25be1ab

7 files changed

Lines changed: 168 additions & 26 deletions

File tree

‎dev/bots/test.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ Future<void> _runIntegrationToolTests() async {
252252

253253
await runFlutterTest(path.join(flutterRoot, 'dev', 'integration_tests', 'hook_user_defines'));
254254
await runFlutterTest(path.join(flutterRoot, 'dev', 'integration_tests', 'link_hook'));
255-
await runFlutterTest(path.join(flutterRoot, 'dev', 'integration_tests', 'data_asset_app'));
255+
await runFlutterTest(
256+
path.join(flutterRoot, 'dev', 'integration_tests', 'data_asset_app'),
257+
environment: <String, String>{'FLUTTER_DART_DATA_ASSETS': 'true'},
258+
);
256259
}
257260

258261
Future<void> _runWidgetPreviewScaffoldToolTests() async {

‎dev/integration_tests/data_asset_app/test/data_asset_app_test.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ void main() {
99
testWidgets('Data Asset Demo smoke test', (WidgetTester tester) async {
1010
await tester.pumpWidget(const MyApp());
1111
expect(find.text('Data Asset Demo'), findsWidgets);
12+
await tester.pump(const Duration(milliseconds: 500));
13+
expect(
14+
find.textContaining('FOUND "packages/data_asset_app/data/id1.txt": "content1"'),
15+
findsOneWidget,
16+
);
17+
expect(find.textContaining('package_content1'), findsOneWidget);
1218
});
1319
}

‎packages/flutter_tools/lib/src/commands/test.dart‎

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -503,32 +503,39 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
503503
uninstallApp: boolArg('uninstall'),
504504
);
505505

506-
final Uri? nativeAssetsJson = _isIntegrationTest
507-
? null // Don't build for host when running integration tests.
508-
: await nativeAssetsBuilder?.build(buildInfo);
506+
final (:Uri? nativeAssetsManifest, :FlutterHookResult? flutterHookResult) = _isIntegrationTest
507+
? (
508+
nativeAssetsManifest: null,
509+
flutterHookResult: null,
510+
) // Don't build for host when running integration tests.
511+
: (await nativeAssetsBuilder?.buildWithHookResult(buildInfo) ??
512+
(nativeAssetsManifest: null, flutterHookResult: null));
509513
String? testAssetPath;
510514
if (buildTestAssets) {
511515
await _buildTestAsset(
512516
flavor: buildInfo.flavor,
513517
impellerStatus: debuggingOptions.enableImpeller,
514518
buildMode: debuggingOptions.buildInfo.mode,
515519
packageConfigPath: buildInfo.packageConfigPath,
520+
flutterHookResult: flutterHookResult,
516521
);
517522
}
518-
if (buildTestAssets || nativeAssetsJson != null) {
523+
if (buildTestAssets || nativeAssetsManifest != null) {
519524
testAssetPath = globals.fs.path.join(
520525
flutterProject.directory.path,
521526
'build',
522527
'unit_test_assets',
523528
);
524529
}
525-
if (nativeAssetsJson != null) {
530+
if (nativeAssetsManifest != null) {
526531
final Directory testAssetDirectory = globals.fs.directory(testAssetPath);
527532
if (!testAssetDirectory.existsSync()) {
528533
await testAssetDirectory.create(recursive: true);
529534
}
530-
final File nativeAssetsManifest = testAssetDirectory.childFile('NativeAssetsManifest.json');
531-
await globals.fs.file(nativeAssetsJson).copy(nativeAssetsManifest.path);
535+
final File copiedNativeAssetsManifest = testAssetDirectory.childFile(
536+
'NativeAssetsManifest.json',
537+
);
538+
await globals.fs.file(nativeAssetsManifest).copy(copiedNativeAssetsManifest.path);
532539
}
533540

534541
final String? concurrencyString = stringArg('concurrency');
@@ -802,9 +809,11 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
802809
required ImpellerStatus impellerStatus,
803810
required BuildMode buildMode,
804811
required String packageConfigPath,
812+
FlutterHookResult? flutterHookResult,
805813
}) async {
806814
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
807815
final int build = await assetBundle.build(
816+
flutterHookResult: flutterHookResult,
808817
packageConfigPath: packageConfigPath,
809818
flavor: flavor,
810819
includeAssetsFromDevDependencies: true,
@@ -813,7 +822,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
813822
if (build != 0) {
814823
throwToolExit('Error: Failed to build asset bundle');
815824
}
816-
if (_needsRebuild(assetBundle.entries, flavor)) {
825+
if (_needsRebuild(assetBundle.entries, flavor, flutterHookResult)) {
817826
await writeBundle(
818827
globals.fs.directory(
819828
globals.fs.path.join(getBuildDirectory(globals.config, globals.fs), 'unit_test_assets'),
@@ -846,32 +855,46 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
846855
}
847856
}
848857

849-
bool _needsRebuild(Map<String, AssetBundleEntry> entries, String? flavor) {
858+
bool _needsRebuild(
859+
Map<String, AssetBundleEntry> entries,
860+
String? flavor,
861+
FlutterHookResult? flutterHookResult,
862+
) {
850863
// TODO(andrewkolos): This logic might fail in the future if we change the
851864
// schema of the contents of the asset manifest file and the user does not
852865
// perform a `flutter clean` after upgrading.
853866
// See https://github.com/flutter/flutter/issues/128563.
867+
final String unitTestAssetsDir = globals.fs.path.join(
868+
getBuildDirectory(globals.config, globals.fs),
869+
'unit_test_assets',
870+
);
854871
final File manifest = globals.fs.file(
855-
globals.fs.path.join(
856-
getBuildDirectory(globals.config, globals.fs),
857-
'unit_test_assets',
858-
'AssetManifest.bin',
859-
),
872+
globals.fs.path.join(unitTestAssetsDir, 'AssetManifest.bin'),
860873
);
861874
if (!manifest.existsSync()) {
862875
return true;
863876
}
864877
final DateTime lastModified = manifest.lastModifiedSync();
865878
final File pub = globals.fs.file('pubspec.yaml');
866-
if (pub.lastModifiedSync().isAfter(lastModified)) {
879+
if (pub.existsSync() && pub.lastModifiedSync().isAfter(lastModified)) {
867880
return true;
868881
}
869882

870-
final Iterable<DevFSFileContent> files = entries.values
871-
.map((AssetBundleEntry asset) => asset.content)
872-
.whereType<DevFSFileContent>();
873-
for (final entry in files) {
874-
if (entry.isModifiedAfter(lastModified)) {
883+
for (final MapEntry<String, AssetBundleEntry> entry in entries.entries) {
884+
final DevFSContent content = entry.value.content;
885+
if (content is DevFSFileContent) {
886+
if (!globals.fs.file(globals.fs.path.join(unitTestAssetsDir, entry.key)).existsSync()) {
887+
return true;
888+
}
889+
if (content.isModifiedAfter(lastModified)) {
890+
return true;
891+
}
892+
}
893+
}
894+
895+
for (final Uri dependency in flutterHookResult?.dependencies ?? const <Uri>[]) {
896+
final File depFile = globals.fs.file(dependency);
897+
if (depFile.existsSync() && depFile.lastModifiedSync().isAfter(lastModified)) {
875898
return true;
876899
}
877900
}

‎packages/flutter_tools/lib/src/isolated/native_assets/test/native_assets.dart‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import 'package:code_assets/code_assets.dart' show OS;
88
import 'package:package_config/package_config_types.dart';
99

10+
import '../../../asset.dart';
1011
import '../../../base/platform.dart';
1112
import '../../../build_info.dart';
1213
import '../../../globals.dart' as globals;
@@ -19,7 +20,13 @@ class TestCompilerNativeAssetsBuilderImpl implements TestCompilerNativeAssetsBui
1920
const TestCompilerNativeAssetsBuilderImpl();
2021

2122
@override
22-
Future<Uri?> build(BuildInfo buildInfo) => testCompilerBuildNativeAssets(buildInfo);
23+
Future<Uri?> build(BuildInfo buildInfo) async =>
24+
(await buildWithHookResult(buildInfo)).nativeAssetsManifest;
25+
26+
@override
27+
Future<({Uri? nativeAssetsManifest, FlutterHookResult? flutterHookResult})> buildWithHookResult(
28+
BuildInfo buildInfo,
29+
) => testCompilerBuildNativeAssets(buildInfo);
2330

2431
@override
2532
String windowsBuildDirectory(FlutterProject project) {
@@ -28,9 +35,10 @@ class TestCompilerNativeAssetsBuilderImpl implements TestCompilerNativeAssetsBui
2835
}
2936
}
3037

31-
Future<Uri?> testCompilerBuildNativeAssets(BuildInfo buildInfo) async {
38+
Future<({Uri? nativeAssetsManifest, FlutterHookResult? flutterHookResult})>
39+
testCompilerBuildNativeAssets(BuildInfo buildInfo) async {
3240
if (!buildInfo.buildNativeAssets) {
33-
return null;
41+
return (nativeAssetsManifest: null, flutterHookResult: null);
3442
}
3543
final Uri projectUri = FlutterProject.current().directory.uri;
3644
final String runPackageName = buildInfo.packageConfig.packages
@@ -57,7 +65,7 @@ Future<Uri?> testCompilerBuildNativeAssets(BuildInfo buildInfo) async {
5765
globals.fs,
5866
buildRunner,
5967
);
60-
return null;
68+
return (nativeAssetsManifest: null, flutterHookResult: null);
6169
}
6270

6371
// Only `flutter test` uses the
@@ -98,5 +106,8 @@ Future<Uri?> testCompilerBuildNativeAssets(BuildInfo buildInfo) async {
98106
);
99107
assert(globals.fs.file(nativeAssetsFileUri).existsSync());
100108

101-
return nativeAssetsFileUri;
109+
return (
110+
nativeAssetsManifest: nativeAssetsFileUri,
111+
flutterHookResult: dartHookResult.asFlutterResult,
112+
);
102113
}

‎packages/flutter_tools/lib/src/native_assets.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'asset.dart';
56
import 'build_info.dart';
67
import 'project.dart';
78

@@ -10,6 +11,10 @@ import 'project.dart';
1011
abstract class TestCompilerNativeAssetsBuilder {
1112
Future<Uri?> build(BuildInfo buildInfo);
1213

14+
Future<({Uri? nativeAssetsManifest, FlutterHookResult? flutterHookResult})> buildWithHookResult(
15+
BuildInfo buildInfo,
16+
) async => (nativeAssetsManifest: await build(buildInfo), flutterHookResult: null);
17+
1318
/// Returns the Windows native assets build directory.
1419
///
1520
/// By prepending the PATH environment variable for the flutter tester with the

‎packages/flutter_tools/test/commands.shard/hermetic/test_test.dart‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:convert';
88
import 'package:args/command_runner.dart';
99
import 'package:file/memory.dart';
1010
import 'package:file_testing/file_testing.dart';
11+
import 'package:flutter_tools/src/asset.dart';
1112
import 'package:flutter_tools/src/base/async_guard.dart';
1213
import 'package:flutter_tools/src/base/common.dart';
1314
import 'package:flutter_tools/src/base/file_system.dart';
@@ -1453,6 +1454,52 @@ dev_dependencies:
14531454
},
14541455
);
14551456

1457+
testUsingContext(
1458+
'builds asset bundle with data assets from hook',
1459+
() async {
1460+
final testRunner = FakeFlutterTestRunner(0);
1461+
final File dataAssetFile = fs.file('data/hook_asset.txt')
1462+
..createSync(recursive: true)
1463+
..writeAsStringSync('from_hook');
1464+
fs.file('pubspec.yaml').writeAsStringSync(_pubspecContents);
1465+
final nativeAssetsBuilder = _FakeNativeAssetsBuilderWithDataAssets(
1466+
FlutterHookResult(
1467+
buildStart: DateTime.now(),
1468+
buildEnd: DateTime.now(),
1469+
dataAssets: <HookAsset>[
1470+
HookAsset(file: dataAssetFile.uri, name: 'data/hook_asset.txt', package: 'my_app'),
1471+
],
1472+
dependencies: <Uri>[dataAssetFile.uri],
1473+
),
1474+
);
1475+
final testCommand = TestCommand(
1476+
testRunner: testRunner,
1477+
nativeAssetsBuilder: nativeAssetsBuilder,
1478+
);
1479+
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);
1480+
1481+
await commandRunner.run(const <String>['test', '--no-pub']);
1482+
1483+
final File bundledDataAsset = fs.file(
1484+
globals.fs.path.join(
1485+
'build',
1486+
'unit_test_assets',
1487+
'packages',
1488+
'my_app',
1489+
'data',
1490+
'hook_asset.txt',
1491+
),
1492+
);
1493+
expect(bundledDataAsset, exists);
1494+
expect(bundledDataAsset.readAsStringSync(), 'from_hook');
1495+
},
1496+
overrides: <Type, Generator>{
1497+
FileSystem: () => fs,
1498+
ProcessManager: () => FakeProcessManager.empty(),
1499+
DeviceManager: () => _FakeDeviceManager(<Device>[]),
1500+
},
1501+
);
1502+
14561503
group('Fatal Logs', () {
14571504
testUsingContext(
14581505
"doesn't fail when --fatal-warnings is set and no warning output",
@@ -1977,3 +2024,18 @@ class _FakeDeviceManager extends DeviceManager {
19772024
return _connectedDevices;
19782025
}
19792026
}
2027+
2028+
class _FakeNativeAssetsBuilderWithDataAssets extends Fake
2029+
implements TestCompilerNativeAssetsBuilder {
2030+
_FakeNativeAssetsBuilderWithDataAssets(this._hookResult);
2031+
2032+
final FlutterHookResult _hookResult;
2033+
2034+
@override
2035+
Future<Uri?> build(BuildInfo buildInfo) async => null;
2036+
2037+
@override
2038+
Future<({Uri? nativeAssetsManifest, FlutterHookResult? flutterHookResult})> buildWithHookResult(
2039+
BuildInfo buildInfo,
2040+
) async => (nativeAssetsManifest: null, flutterHookResult: _hookResult);
2041+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import '../../src/common.dart';
6+
import '../test_utils.dart' show platform;
7+
import '../transition_test_utils.dart';
8+
import 'dart_data_asset_utils.dart';
9+
10+
void main() {
11+
if (!platform.isMacOS && !platform.isLinux && !platform.isWindows) {
12+
return;
13+
}
14+
setUpAll(setUpAllDataAssets);
15+
setUp(setUpDataAssets);
16+
tearDown(tearDownDataAssets);
17+
18+
group('dart data assets', () {
19+
testWithoutContext('flutter test', () async {
20+
final ProcessTestResult result = await runFlutter(
21+
<String>['test', '-v'],
22+
appRoot.path,
23+
<Transition>[Barrier(RegExp('.* All tests passed!'))],
24+
);
25+
if (result.exitCode != 0) {
26+
throw Exception(
27+
'flutter test failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}',
28+
);
29+
}
30+
});
31+
});
32+
}

0 commit comments

Comments
 (0)