Skip to content

Commit c311842

Browse files
committed
Allow --dry-run
1 parent 9ae2871 commit c311842

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/src/command/cache_gc.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ class CacheGcCommand extends PubCommand {
3636
help: 'Also delete recent files',
3737
hideNegatedUsage: true,
3838
);
39+
argParser.addFlag(
40+
'dry-run',
41+
help: 'Print list of files that would be deleted',
42+
hideNegatedUsage: true,
43+
);
3944
}
4045

4146
@override
4247
Future<void> runProtected() async {
48+
final dryRun = argResults.flag('dry-run');
4349
final activeRoots = cache.activeRoots();
4450
final validActiveRoots = <String>[];
4551
final paths = <String>{};
@@ -131,11 +137,17 @@ class CacheGcCommand extends PubCommand {
131137
log.message('');
132138
log.message(
133139
'''
134-
All other projects will need to run `$topLevelProgram pub get` again to work correctly.''',
140+
All other projects ${dryRun ? 'would' : 'will'} need to run `$topLevelProgram pub get` again to work correctly.''',
135141
);
136-
log.message('Will recover ${readableFileSize(sum)}.');
137-
138-
if (argResults.flag('force') ||
142+
log.message(
143+
'${dryRun ? 'Would' : 'Will'} recover ${readableFileSize(sum)}.',
144+
);
145+
if (dryRun) {
146+
log.message('Would delete:');
147+
for (final path in allPathsToGC..sort()) {
148+
log.message(path);
149+
}
150+
} else if (argResults.flag('force') ||
139151
await confirm('Are you sure you want to continue?')) {
140152
await log.progress('Deleting unused cache entries', () async {
141153
for (final path in allPathsToGC..sort()) {

test/cache/gc_test.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ void main() async {
153153
output: allOf(matchesAppPath, contains('No unused cache entries found')),
154154
);
155155
final s = RegExp.escape(p.separator);
156+
await runPub(
157+
args: ['cache', 'gc', '--force', '--ignore-timestamp', '--dry-run'],
158+
output: allOf([
159+
matchesAppPath,
160+
contains(RegExp('Would recover [0-9]{3} KB.')),
161+
contains(RegExp('.*git.*cache${s}git1-.*')),
162+
contains(RegExp('.*git.*cache${s}git_with_path1-.*')),
163+
contains(RegExp('.*git.*git1-.*')),
164+
contains(RegExp('.*git.*git_with_path1-.*')),
165+
contains(RegExp('.*hosted-hashes.*hosted1-1.0.0.sha256')),
166+
contains(RegExp('.*hosted.*hosted1-1.0.0.')),
167+
isNot(contains(RegExp('.*hosted2'))),
168+
isNot(contains(RegExp('.*git2'))),
169+
isNot(contains(RegExp('.*git_with_path2'))),
170+
]),
171+
);
156172
await runPub(
157173
args: ['cache', 'gc', '--force', '--ignore-timestamp'],
158174
output: allOf(
@@ -166,9 +182,7 @@ void main() async {
166182
),
167183
contains(RegExp('Deleting directory .*git.*git1-.*')),
168184
contains(RegExp('Deleting directory .*git.*git_with_path1-.*')),
169-
contains(
170-
RegExp('Deleting file .*hosted-hashes.*hosted1-1.0.0.sha256.'),
171-
),
185+
contains(RegExp('Deleting file .*hosted-hashes.*hosted1-1.0.0.sha256')),
172186
contains(RegExp('Deleting directory .*hosted.*hosted1-1.0.0.')),
173187
isNot(contains(RegExp('Deleting.*hosted2'))),
174188
isNot(contains(RegExp('Deleting.*git2'))),

0 commit comments

Comments
 (0)