@@ -809,7 +809,40 @@ foo:foomain''',
809809 );
810810 });
811811
812- test ('Removes lock files and package configs from workspace members' ,
812+ test ('Reports error if pubspec inside workspace is not part of the workspace' ,
813+ () async {
814+ await dir (appPath, [
815+ libPubspec (
816+ 'myapp' ,
817+ '1.2.3' ,
818+ extras: {
819+ 'workspace' : ['pkgs/a' , 'pkgs/a/example' ],
820+ },
821+ sdk: '^3.5.0' ,
822+ ),
823+ dir ('pkgs' , [
824+ libPubspec ('not_in_workspace' , '1.0.0' ),
825+ dir (
826+ 'a' ,
827+ [
828+ libPubspec ('a' , '1.1.1' , resolutionWorkspace: true ),
829+ dir ('example' , [
830+ libPubspec ('example' , '0.0.0' , resolutionWorkspace: true ),
831+ ]),
832+ ],
833+ ),
834+ ]),
835+ ]).create ();
836+ await pubGet (
837+ environment: {'_PUB_TEST_SDK_VERSION' : '3.5.0' },
838+ error: contains (
839+ 'The file `.${s }pkgs${s }pubspec.yaml` '
840+ 'is located in a directory between the workspace root' ,
841+ ),
842+ );
843+ });
844+
845+ test ('Removes lock files and package configs from inside the workspace' ,
813846 () async {
814847 await dir (appPath, [
815848 libPubspec (
@@ -825,34 +858,75 @@ foo:foomain''',
825858 'a' ,
826859 [
827860 libPubspec ('a' , '1.1.1' , resolutionWorkspace: true ),
861+ dir ('test_data' , []),
828862 ],
829863 ),
830864 ]),
831865 ]).create ();
866+ // Directories outside the workspace should not be affected.
867+ final outideWorkpace = sandbox;
868+ // Directories of worksace packages should be cleaned.
832869 final aDir = p.join (sandbox, appPath, 'pkgs' , 'a' );
870+ // Directories between workspace root and workspace packages should
871+ // be cleaned.
833872 final pkgsDir = p.join (sandbox, appPath, 'pkgs' );
834- final strayLockFile = File (p.join (aDir, 'pubspec.lock' ));
835- final strayPackageConfig =
836- File (p.join (aDir, '.dart_tool' , 'package_config.json' ));
873+ // Directories inside a workspace package should not be cleaned.
874+ final inside = p.join (aDir, 'test_data' );
837875
838- final unmanagedLockFile = File (p. join (pkgsDir, 'pubspec.lock' ));
839- final unmanagedPackageConfig =
840- File (p.join (pkgsDir , '.dart_tool' , 'package_config.json' ));
841- strayPackageConfig .createSync (recursive: true );
842- strayLockFile. createSync (recursive : true );
876+ void createLockFileAndPackageConfig ( String dir) {
877+ File (p. join (dir, 'pubspec.lock' )). createSync (recursive : true );
878+ File (p.join (dir , '.dart_tool' , 'package_config.json' ))
879+ .createSync (recursive: true );
880+ }
843881
844- unmanagedPackageConfig.createSync (recursive: true );
845- unmanagedLockFile.createSync (recursive: true );
882+ void validateLockFileAndPackageConfig (
883+ String dir,
884+ FileSystemEntityType state,
885+ ) {
886+ expect (
887+ File (p.join (dir, 'pubspec.lock' )).statSync ().type,
888+ state,
889+ );
890+ expect (
891+ File (p.join (dir, '.dart_tool' , 'package_config.json' )).statSync ().type,
892+ state,
893+ );
894+ }
846895
847- await pubGet (environment: {'_PUB_TEST_SDK_VERSION' : '3.5.0' });
896+ createLockFileAndPackageConfig (sandbox);
897+ createLockFileAndPackageConfig (aDir);
898+ createLockFileAndPackageConfig (pkgsDir);
899+ createLockFileAndPackageConfig (inside);
848900
849- expect (strayLockFile.statSync ().type, FileSystemEntityType .notFound);
850- expect (strayPackageConfig.statSync ().type, FileSystemEntityType .notFound);
901+ await pubGet (
902+ environment: {'_PUB_TEST_SDK_VERSION' : '3.5.0' },
903+ warning: allOf (
904+ contains ('Deleting old lock-file: `.${s }pkgs/a${s }pubspec.lock' ),
905+ contains (
906+ 'Deleting old package config: '
907+ '`.${s }pkgs/a$s .dart_tool${s }package_config.json`' ,
908+ ),
909+ contains ('Deleting old lock-file: `.${s }pkgs${s }pubspec.lock' ),
910+ contains (
911+ 'Deleting old package config: '
912+ '`.${s }pkgs$s .dart_tool${s }package_config.json`' ,
913+ ),
914+ contains (
915+ 'See https://dart.dev/go/workspaces-stray-files for details.' ,
916+ ),
917+ ),
918+ );
851919
852- // We only delete stray files from directories that contain an actual
853- // package.
854- expect (unmanagedLockFile.statSync ().type, FileSystemEntityType .file);
855- expect (unmanagedPackageConfig.statSync ().type, FileSystemEntityType .file);
920+ validateLockFileAndPackageConfig (
921+ outideWorkpace,
922+ FileSystemEntityType .file,
923+ );
924+ validateLockFileAndPackageConfig (aDir, FileSystemEntityType .notFound);
925+ validateLockFileAndPackageConfig (pkgsDir, FileSystemEntityType .notFound);
926+ validateLockFileAndPackageConfig (
927+ inside,
928+ FileSystemEntityType .file,
929+ );
856930 });
857931
858932 test ('Reports error if workspace doesn\' t form a tree.' , () async {
0 commit comments