feat(shorebird_cli): add shorebird cache clean (#239)

Co-authored-by: Felix Angelov <felix@shorebird.dev>
This commit is contained in:
TypicalEgg
2023-04-14 08:39:45 -07:00
committed by GitHub
parent c743f69dd2
commit bc433ac14d
9 changed files with 141 additions and 6 deletions
+17 -6
View File
@@ -52,20 +52,31 @@ class Cache {
}
/// The Shorebird cache directory.
static Directory get shorebirdCacheDirectory => Directory(
p.join(ShorebirdEnvironment.shorebirdRoot.path, 'bin', 'cache'),
);
static Directory get shorebirdCacheDirectory {
return Directory(
p.join(ShorebirdEnvironment.shorebirdRoot.path, 'bin', 'cache'),
);
}
/// The Shorebird cached artifacts directory.
static Directory get shorebirdArtifactsDirectory => Directory(
p.join(shorebirdCacheDirectory.path, 'artifacts'),
);
static Directory get shorebirdArtifactsDirectory {
return Directory(
p.join(shorebirdCacheDirectory.path, 'artifacts'),
);
}
final List<CachedArtifact> _artifacts = [];
String get storageBaseUrl => 'https://storage.googleapis.com';
String get storageBucket => 'download.shorebird.dev';
void clear() {
final cacheDir = shorebirdCacheDirectory;
if (cacheDir.existsSync()) {
cacheDir.deleteSync(recursive: true);
}
}
}
abstract class CachedArtifact {
@@ -38,6 +38,7 @@ class ShorebirdCliCommandRunner extends CompletionCommandRunner<int> {
addCommand(AccountCommand(logger: _logger));
addCommand(AppsCommand(logger: _logger));
addCommand(BuildCommand(logger: _logger));
addCommand(CacheCommand(logger: _logger));
addCommand(ChannelsCommand(logger: _logger));
addCommand(DoctorCommand(logger: _logger));
addCommand(InitCommand(logger: _logger));
@@ -0,0 +1,2 @@
export 'cache_command.dart';
export 'clean_cache_command.dart';
@@ -0,0 +1,19 @@
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/commands/commands.dart';
/// {@template cache_command}
/// `shorebird cache`
/// Manage the Shorebird cache.
/// {@endtemplate}
class CacheCommand extends ShorebirdCommand {
/// {@macro cache_command}
CacheCommand({required super.logger, super.cache}) {
addSubcommand(CleanCacheCommand(logger: logger, cache: cache));
}
@override
String get description => 'Manage the Shorebird cache.';
@override
String get name => 'cache';
}
@@ -0,0 +1,30 @@
import 'dart:async';
import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/shorebird_config_mixin.dart';
/// {@template clean_cache_command}
/// `shorebird cache clean`
/// Clears the Shorebird cache directory.
/// {@endtemplate}
class CleanCacheCommand extends ShorebirdCommand with ShorebirdConfigMixin {
/// {@macro clean_cache_command}
CleanCacheCommand({required super.logger, required super.cache});
@override
String get description => 'Clears the Shorebird cache directory.';
@override
String get name => 'clean';
@override
List<String> get aliases => ['clear'];
@override
Future<int> run() async {
cache.clear();
logger.success('✅ Cleared Cache!');
return ExitCode.success.code;
}
}
@@ -1,6 +1,7 @@
export 'account_command.dart';
export 'apps/apps.dart';
export 'build_command.dart';
export 'cache/cache.dart';
export 'channels/channels.dart';
export 'doctor_command.dart';
export 'init_command.dart';
@@ -84,6 +84,21 @@ void main() {
});
});
group('clear', () {
test('deletes the cache directory', () async {
Cache.shorebirdCacheDirectory.createSync(recursive: true);
expect(Cache.shorebirdCacheDirectory.existsSync(), isTrue);
cache.clear();
expect(Cache.shorebirdCacheDirectory.existsSync(), isFalse);
});
test('does nothing if directory does not exist', () {
expect(Cache.shorebirdCacheDirectory.existsSync(), isFalse);
cache.clear();
expect(Cache.shorebirdCacheDirectory.existsSync(), isFalse);
});
});
group('updateAll', () {
group('patch', () {
test('downloads correct artifacts', () async {
@@ -0,0 +1,22 @@
import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:shorebird_cli/src/commands/cache/cache_command.dart';
import 'package:test/test.dart';
class _MockLogger extends Mock implements Logger {}
void main() {
group('cache', () {
late Logger logger;
late CacheCommand command;
setUp(() {
logger = _MockLogger();
command = CacheCommand(logger: logger);
});
test('has a description', () {
expect(command.description, isNotEmpty);
});
});
}
@@ -0,0 +1,34 @@
import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:shorebird_cli/src/cache.dart';
import 'package:shorebird_cli/src/commands/commands.dart';
import 'package:test/test.dart';
class _MockLogger extends Mock implements Logger {}
class _MockCache extends Mock implements Cache {}
void main() {
group('cache clean', () {
late Cache cache;
late Logger logger;
late CleanCacheCommand command;
setUp(() {
cache = _MockCache();
logger = _MockLogger();
command = CleanCacheCommand(cache: cache, logger: logger);
});
test('has a description', () {
expect(command.description, isNotEmpty);
});
test('clears the cache', () async {
final result = await command.run();
expect(result, equals(ExitCode.success.code));
verify(() => logger.success('✅ Cleared Cache!')).called(1);
verify(cache.clear).called(1);
});
});
}