Skip to content

Commit dfdd5f8

Browse files
authored
Fix flutter test in workspace (#153919)
Follow-up to #153754.
1 parent 2ed3dab commit dfdd5f8

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

packages/flutter_tools/lib/src/build_system/targets/common.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class KernelSnapshotProgram extends Target {
133133

134134
@override
135135
List<Source> get inputs => const <Source>[
136-
Source.pattern('{PROJECT_DIR}/.dart_tool/package_config_subset'),
136+
Source.pattern('{WORKSPACE_DIR}/.dart_tool/package_config_subset'),
137137
Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/common.dart'),
138138
Source.artifact(Artifact.platformKernelDill),
139139
Source.artifact(Artifact.engineDartBinary),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
425425
flavor: buildInfo.flavor,
426426
impellerStatus: debuggingOptions.enableImpeller,
427427
buildMode: debuggingOptions.buildInfo.mode,
428+
packageConfigPath: buildInfo.packageConfigPath,
428429
);
429430
testAssetDirectory = globals.fs.path.
430431
join(flutterProject.directory.path, 'build', 'unit_test_assets');
@@ -691,12 +692,11 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
691692
required String? flavor,
692693
required ImpellerStatus impellerStatus,
693694
required BuildMode buildMode,
695+
required String packageConfigPath,
694696
}) async {
695697
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
696-
// TODO(sigurdm): parametrize packageConfigPath to support testing
697-
// workspaces.
698698
final int build = await assetBundle.build(
699-
packageConfigPath: '.dart_tool/package_config.json',
699+
packageConfigPath: packageConfigPath,
700700
flavor: flavor,
701701
);
702702
if (build != 0) {

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,14 @@ class NativeAssetsBuildRunnerImpl implements NativeAssetsBuildRunner {
138138

139139
@override
140140
Future<bool> hasPackageConfig() {
141-
final File packageConfigJson =
142-
fileSystem.directory(projectUri.toFilePath()).childDirectory('.dart_tool').childFile('package_config.json');
143-
return packageConfigJson.exists();
141+
return fileSystem.file(packageConfigPath).exists();
144142
}
145143

146144
@override
147145
Future<List<Package>> packagesWithNativeAssets() async {
148146
final PackageLayout packageLayout = PackageLayout.fromPackageConfig(
149147
packageConfig,
150-
projectUri.resolve('.dart_tool/package_config.json'),
148+
Uri.file(packageConfigPath),
151149
);
152150
// It suffices to only check for build hooks. If no packages have a build
153151
// hook. Then no build hook will output any assets for any link hook, and
@@ -164,7 +162,7 @@ class NativeAssetsBuildRunnerImpl implements NativeAssetsBuildRunner {
164162
}) {
165163
final PackageLayout packageLayout = PackageLayout.fromPackageConfig(
166164
packageConfig,
167-
projectUri.resolve('.dart_tool/package_config.json'),
165+
Uri.file(packageConfigPath),
168166
);
169167
return _buildRunner.buildDryRun(
170168
includeParentEnvironment: includeParentEnvironment,
@@ -192,7 +190,7 @@ class NativeAssetsBuildRunnerImpl implements NativeAssetsBuildRunner {
192190
}) {
193191
final PackageLayout packageLayout = PackageLayout.fromPackageConfig(
194192
packageConfig,
195-
projectUri.resolve('.dart_tool/package_config.json'),
193+
Uri.file(packageConfigPath),
196194
);
197195
return _buildRunner.build(
198196
buildMode: buildMode,
@@ -221,7 +219,7 @@ class NativeAssetsBuildRunnerImpl implements NativeAssetsBuildRunner {
221219
}) {
222220
final PackageLayout packageLayout = PackageLayout.fromPackageConfig(
223221
packageConfig,
224-
projectUri.resolve('.dart_tool/package_config.json'),
222+
Uri.file(packageConfigPath),
225223
);
226224
return _buildRunner.linkDryRun(
227225
includeParentEnvironment: includeParentEnvironment,
@@ -249,7 +247,7 @@ class NativeAssetsBuildRunnerImpl implements NativeAssetsBuildRunner {
249247
}) {
250248
final PackageLayout packageLayout = PackageLayout.fromPackageConfig(
251249
packageConfig,
252-
projectUri.resolve('.dart_tool/package_config.json'),
250+
Uri.file(packageConfigPath),
253251
);
254252
return _buildRunner.link(
255253
buildMode: buildMode,

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ void main() {
112112
(package.childDirectory('.dart_tool')
113113
.childFile('package_config.json')
114114
..createSync(recursive: true))
115-
.writeAsString(_packageConfigContents);
115+
.writeAsStringSync(_packageConfigContents);
116116
package.childDirectory('test').childFile('some_test.dart').createSync(recursive: true);
117117
package.childDirectory('integration_test').childFile('some_integration_test.dart').createSync(recursive: true);
118118

119+
119120
final File flutterToolsPackageConfigFile = fs.directory(
120121
fs.path.join(
121122
getFlutterRoot(),
@@ -1430,6 +1431,53 @@ dev_dependencies:
14301431
ProcessManager: () => FakeProcessManager.any(),
14311432
});
14321433
});
1434+
1435+
testUsingContext('Can test in a pub workspace',
1436+
() async {
1437+
final String root = fs.path.rootPrefix(fs.currentDirectory.absolute.path);
1438+
final Directory package = fs.directory('${root}package').absolute;
1439+
package.childFile('pubspec.yaml').createSync(recursive: true);
1440+
package.childFile('pubspec.yaml').writeAsStringSync('''
1441+
workspace:
1442+
- app/
1443+
''');
1444+
1445+
final Directory app = package.childDirectory('app');
1446+
app.createSync();
1447+
app.childFile('pubspec.yaml').writeAsStringSync('''
1448+
$_pubspecContents
1449+
resolution: workspace
1450+
''');
1451+
app.childDirectory('test').childFile('some_test.dart').createSync(recursive: true);
1452+
app.childDirectory('integration_test').childFile('some_integration_test.dart').createSync(recursive: true);
1453+
1454+
fs.currentDirectory = app;
1455+
1456+
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
1457+
final FakePackageTest fakePackageTest = FakePackageTest();
1458+
final TestCommand testCommand = TestCommand(
1459+
testWrapper: fakePackageTest,
1460+
testRunner: testRunner,
1461+
);
1462+
final CommandRunner<void> commandRunner =
1463+
createTestCommandRunner(testCommand);
1464+
1465+
await commandRunner.run(const <String>[
1466+
'test',
1467+
'--no-pub',
1468+
]);
1469+
expect(
1470+
testRunner.lastDebuggingOptionsValue.buildInfo.packageConfigPath,
1471+
package
1472+
.childDirectory('.dart_tool')
1473+
.childFile('package_config.json')
1474+
.path,
1475+
);
1476+
}, overrides: <Type, Generator>{
1477+
FileSystem: () => fs,
1478+
ProcessManager: () => FakeProcessManager.any(),
1479+
Logger: () => logger,
1480+
});
14331481
}
14341482

14351483
class FakeFlutterTestRunner implements FlutterTestRunner {

0 commit comments

Comments
 (0)