1+ import 'dart:convert' ;
12import 'dart:io' as io;
23
34import 'package:analyzer/file_system/file_system.dart' ;
@@ -20,7 +21,12 @@ File createAnalysisOptions(String content) {
2021 return PhysicalResourceProvider .INSTANCE .getFile (ioFile.path);
2122}
2223
23- Future <String > createTempProject (String projectName, String tempDirPath) async {
24+ Future <String > createTempProject ({
25+ required String tempDirPath,
26+ required String projectName,
27+ String ? packageConfig,
28+ String ? workspaceRef,
29+ }) async {
2430 final projectPath = join (tempDirPath, projectName);
2531
2632 final dir = io.Directory (projectPath);
@@ -36,6 +42,28 @@ custom_lint:
3642 - from_package
3743 ''' );
3844
45+ final projectDartToolPath = join (projectPath, '.dart_tool' );
46+ await io.Directory (projectDartToolPath).create (recursive: true );
47+
48+ if (packageConfig != null ) {
49+ final String packageConfigPath;
50+ if (workspaceRef != null ) {
51+ final workspaceDartToolPath = join (tempDirPath, '.dart_tool' );
52+ await io.Directory (workspaceDartToolPath).create (recursive: true );
53+ packageConfigPath = join (workspaceDartToolPath, 'package_config.json' );
54+ } else {
55+ packageConfigPath = join (projectDartToolPath, 'package_config.json' );
56+ }
57+ await io.File (packageConfigPath).writeAsString (packageConfig);
58+ }
59+
60+ if (workspaceRef != null ) {
61+ final pubPath = join (projectDartToolPath, 'pub' );
62+ await io.Directory (pubPath).create (recursive: true );
63+ final workspaceRefPath = join (pubPath, 'workspace_ref.json' );
64+ await io.File (workspaceRefPath).writeAsString (workspaceRef);
65+ }
66+
3967 return dir.path;
4068}
4169
@@ -205,7 +233,10 @@ custom_lint:
205233include: package:$testPackageName /analysis_options.yaml
206234 ''' );
207235
208- final tempProjectDir = await createTempProject (dir.path, testPackageName);
236+ final tempProjectDir = await createTempProject (
237+ tempDirPath: dir.path,
238+ projectName: testPackageName,
239+ );
209240
210241 final patchedPackageConfig = patchPackageConfig (
211242 packageConfig,
@@ -225,7 +256,10 @@ include: package:$testPackageName/$notExistingFileName
225256 ''' );
226257 final dir = createDir ();
227258
228- final tempProjectDir = await createTempProject (dir.path, testPackageName);
259+ final tempProjectDir = await createTempProject (
260+ tempDirPath: dir.path,
261+ projectName: testPackageName,
262+ );
229263 final patchedPackageConfig = patchPackageConfig (
230264 packageConfig,
231265 testPackageName,
@@ -243,7 +277,10 @@ include: package:$testPackageName/$notExistingFileName
243277include: package:$notExistingPackage /analysis_options.yaml
244278 ''' );
245279 final dir = createDir ();
246- final tempProjectDir = await createTempProject (dir.path, testPackageName);
280+ final tempProjectDir = await createTempProject (
281+ tempDirPath: dir.path,
282+ projectName: testPackageName,
283+ );
247284
248285 final patchedPackageConfig = patchPackageConfig (
249286 packageConfig,
@@ -300,5 +337,33 @@ foo:
300337 expect (configs, CustomLintConfigs .empty);
301338 });
302339 });
340+
341+ group ('package config' , () {
342+ test ('single package' , () async {
343+ final dir = createDir ();
344+ final projectPath = await createTempProject (
345+ tempDirPath: dir.path,
346+ projectName: testPackageName,
347+ packageConfig: jsonEncode (PackageConfig .toJson (packageConfig)),
348+ );
349+ final projectDir = io.Directory (projectPath);
350+ final parsed = parsePackageConfig (projectDir);
351+ expect (parsed, isNotNull);
352+ });
353+
354+ test ('workspace' , () async {
355+ final dir = createDir ();
356+ print (dir);
357+ final projectPath = await createTempProject (
358+ tempDirPath: dir.path,
359+ projectName: testPackageName,
360+ packageConfig: jsonEncode (PackageConfig .toJson (packageConfig)),
361+ workspaceRef: jsonEncode ({'workspaceRoot' : '../../..' }),
362+ );
363+ final projectDir = io.Directory (projectPath);
364+ final parsed = parsePackageConfig (projectDir);
365+ expect (parsed, isNotNull);
366+ });
367+ });
303368 });
304369}
0 commit comments