diff --git a/packages/shorebird_cli/lib/src/cache.dart b/packages/shorebird_cli/lib/src/cache.dart index 609a7fa2..8f8d264b 100644 --- a/packages/shorebird_cli/lib/src/cache.dart +++ b/packages/shorebird_cli/lib/src/cache.dart @@ -109,6 +109,11 @@ class Cache { if (cacheDir.existsSync()) { cacheDir.deleteSync(recursive: true); } + + final logsDirectory = shorebirdEnv.logsDirectory; + if (logsDirectory.existsSync()) { + logsDirectory.deleteSync(recursive: true); + } } } diff --git a/packages/shorebird_cli/lib/src/commands/cache/clean_cache_command.dart b/packages/shorebird_cli/lib/src/commands/cache/clean_cache_command.dart index fc320d16..4eff797c 100644 --- a/packages/shorebird_cli/lib/src/commands/cache/clean_cache_command.dart +++ b/packages/shorebird_cli/lib/src/commands/cache/clean_cache_command.dart @@ -34,6 +34,7 @@ class CleanCacheCommand extends ShorebirdCommand { progress.fail( '''Failed to delete cache directory $cachePath: $error''', ); + if (!platform.isWindows) { return ExitCode.software.code; } diff --git a/packages/shorebird_cli/lib/src/logger.dart b/packages/shorebird_cli/lib/src/logger.dart index 1eaca141..363bd1af 100644 --- a/packages/shorebird_cli/lib/src/logger.dart +++ b/packages/shorebird_cli/lib/src/logger.dart @@ -18,11 +18,9 @@ class ShorebirdLogger extends Logger { late final File logFile = (() { final timestamp = DateTime.now().millisecondsSinceEpoch; - final file = File( p.join( - shorebirdEnv.shorebirdRoot.path, - 'logs', + shorebirdEnv.logsDirectory.path, '${timestamp}_$_logFileName', ), ); diff --git a/packages/shorebird_cli/lib/src/shorebird_env.dart b/packages/shorebird_cli/lib/src/shorebird_env.dart index 88eb8896..12445620 100644 --- a/packages/shorebird_cli/lib/src/shorebird_env.dart +++ b/packages/shorebird_cli/lib/src/shorebird_env.dart @@ -1,9 +1,11 @@ import 'dart:io' hide Platform; import 'package:checked_yaml/checked_yaml.dart'; +import 'package:cli_util/cli_util.dart'; import 'package:path/path.dart' as p; import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:scoped/scoped.dart'; +import 'package:shorebird_cli/src/command_runner.dart'; import 'package:shorebird_cli/src/config/shorebird_yaml.dart'; import 'package:shorebird_cli/src/platform.dart'; import 'package:shorebird_code_push_client/shorebird_code_push_client.dart'; @@ -29,6 +31,16 @@ class ShorebirdEnv { final String? _flutterRevisionOverride; + /// The application config directory for the Shorebird CLI. + Directory get configDirectory { + return Directory(applicationConfigHome(executableName)); + } + + /// The directory where shorebird logs are stored. + Directory get logsDirectory { + return Directory(p.join(configDirectory.path, 'logs')); + } + /// The root directory of the Shorebird install. /// /// Assumes we are running from $ROOT/bin/cache. diff --git a/packages/shorebird_cli/test/src/cache_test.dart b/packages/shorebird_cli/test/src/cache_test.dart index 81459fc3..abd184f4 100644 --- a/packages/shorebird_cli/test/src/cache_test.dart +++ b/packages/shorebird_cli/test/src/cache_test.dart @@ -25,6 +25,7 @@ void main() { late ArtifactManager artifactManager; late Directory shorebirdRoot; + late Directory logsDirectory; late http.Client httpClient; late ShorebirdLogger logger; late Platform platform; @@ -77,6 +78,7 @@ void main() { shorebirdProcess = MockShorebirdProcess(); shorebirdRoot = Directory.systemTemp.createTempSync(); + logsDirectory = Directory.systemTemp.createTempSync(); when( () => artifactManager.extractZip( zipFile: any(named: 'zipFile'), @@ -90,6 +92,7 @@ void main() { () => shorebirdEnv.shorebirdEngineRevision, ).thenReturn(shorebirdEngineRevision); when(() => shorebirdEnv.shorebirdRoot).thenReturn(shorebirdRoot); + when(() => shorebirdEnv.logsDirectory).thenReturn(logsDirectory); when(() => platform.environment).thenReturn({}); setMockPlatform(Platform.macOS); @@ -163,18 +166,28 @@ void main() { final shorebirdCacheDirectory = runWithOverrides( () => Cache.shorebirdCacheDirectory, )..createSync(recursive: true); + final logsDirectory = runWithOverrides( + () => shorebirdEnv.logsDirectory, + )..createSync(recursive: true); expect(shorebirdCacheDirectory.existsSync(), isTrue); + expect(logsDirectory.existsSync(), isTrue); runWithOverrides(cache.clear); expect(shorebirdCacheDirectory.existsSync(), isFalse); + expect(logsDirectory.existsSync(), isFalse); }); test('does nothing if directory does not exist', () { final shorebirdCacheDirectory = runWithOverrides( () => Cache.shorebirdCacheDirectory, ); + final logsDirectory = runWithOverrides( + () => shorebirdEnv.logsDirectory, + )..deleteSync(recursive: true); expect(shorebirdCacheDirectory.existsSync(), isFalse); + expect(logsDirectory.existsSync(), isFalse); runWithOverrides(cache.clear); expect(shorebirdCacheDirectory.existsSync(), isFalse); + expect(logsDirectory.existsSync(), isFalse); }); }); diff --git a/packages/shorebird_cli/test/src/commands/release/release_command_test.dart b/packages/shorebird_cli/test/src/commands/release/release_command_test.dart index 8cd61e15..0a71816d 100644 --- a/packages/shorebird_cli/test/src/commands/release/release_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/release/release_command_test.dart @@ -96,8 +96,9 @@ void main() { when(cache.updateAll).thenAnswer((_) async => {}); - when(() => codePushClientWrapper.getApp(appId: any(named: 'appId'))) - .thenAnswer((_) async => appMetadata); + when( + () => codePushClientWrapper.getApp(appId: any(named: 'appId')), + ).thenAnswer((_) async => appMetadata); when( () => codePushClientWrapper.maybeGetRelease( appId: any(named: 'appId'), @@ -127,8 +128,9 @@ void main() { when(() => releaser.assertPreconditions()).thenAnswer((_) async => {}); when(() => releaser.assertArgsAreValid()).thenAnswer((_) async => {}); - when(() => releaser.buildReleaseArtifacts()) - .thenAnswer((_) async => File('')); + when( + () => releaser.buildReleaseArtifacts(), + ).thenAnswer((_) async => File('')); when( () => releaser.getReleaseVersion( releaseArtifactRoot: any(named: 'releaseArtifactRoot'), @@ -140,11 +142,13 @@ void main() { appId: any(named: 'appId'), ), ).thenAnswer((_) async => {}); - when(() => releaser.postReleaseInstructions) - .thenReturn(postReleaseInstructions); + when( + () => releaser.postReleaseInstructions, + ).thenReturn(postReleaseInstructions); when(() => releaser.releaseType).thenReturn(ReleaseType.android); - when(() => releaser.releaseMetadata()) - .thenAnswer((_) async => UpdateReleaseMetadata.forTest()); + when( + () => releaser.releaseMetadata(), + ).thenAnswer((_) async => UpdateReleaseMetadata.forTest()); when(() => releaser.requiresReleaseVersionArg).thenReturn(false); when(() => shorebirdEnv.getShorebirdYaml()).thenReturn(shorebirdYaml); diff --git a/packages/shorebird_cli/test/src/logger_test.dart b/packages/shorebird_cli/test/src/logger_test.dart index 23baf0a1..e9af838b 100644 --- a/packages/shorebird_cli/test/src/logger_test.dart +++ b/packages/shorebird_cli/test/src/logger_test.dart @@ -12,32 +12,29 @@ import 'mocks.dart'; void main() { group('ShorebirdLogger', () { - late Directory shorebirdRootDir; + late Directory logsDirectory; late ShorebirdEnv shorebirdEnv; late ShorebirdLogger shorebirdLogger; setUp(() { - shorebirdRootDir = Directory.systemTemp.createTempSync('shorebird_logs'); + logsDirectory = Directory.systemTemp.createTempSync( + 'shorebird_logs', + ); shorebirdEnv = MockShorebirdEnv(); - when(() => shorebirdEnv.shorebirdRoot).thenReturn(shorebirdRootDir); + when(() => shorebirdEnv.logsDirectory).thenReturn(logsDirectory); // Setting to quiet so we don't spam the stdout/stderr while testing shorebirdLogger = ShorebirdLogger(level: Level.quiet); }); String readLogFile() { - final logFile = Directory( - p.join(shorebirdRootDir.path, 'logs'), - ).listSync().first; + final logFile = Directory(p.join(logsDirectory.path)).listSync().first; return File(logFile.path).readAsStringSync(); } test('can be instantiated', () { - expect( - ShorebirdLogger.new, - returnsNormally, - ); + expect(ShorebirdLogger.new, returnsNormally); }); test('info', () { diff --git a/packages/shorebird_cli/test/src/shorebird_env_test.dart b/packages/shorebird_cli/test/src/shorebird_env_test.dart index 25460cb1..49c3a7f4 100644 --- a/packages/shorebird_cli/test/src/shorebird_env_test.dart +++ b/packages/shorebird_cli/test/src/shorebird_env_test.dart @@ -5,6 +5,7 @@ import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:scoped/scoped.dart'; +import 'package:shorebird_cli/src/command_runner.dart'; import 'package:shorebird_cli/src/platform.dart'; import 'package:shorebird_cli/src/shorebird_env.dart'; import 'package:test/test.dart'; @@ -64,6 +65,24 @@ void main() { }); }); + group('configDirectory', () { + test('returns correct directory', () { + expect( + runWithOverrides(() => shorebirdEnv.configDirectory.path), + endsWith(executableName), + ); + }); + }); + + group('logsDirectory', () { + test('returns correct directory', () { + expect( + runWithOverrides(() => shorebirdEnv.logsDirectory.path), + endsWith(p.join(executableName, 'logs')), + ); + }); + }); + group('getShorebirdYamlFile', () { test('returns correct file', () { final tempDir = Directory.systemTemp.createTempSync();