diff --git a/dev/conductor/core/lib/src/clean.dart b/dev/conductor/core/lib/src/clean.dart index 30324bc20d..b5792f298a 100644 --- a/dev/conductor/core/lib/src/clean.dart +++ b/dev/conductor/core/lib/src/clean.dart @@ -49,12 +49,11 @@ class CleanCommand extends Command { 'This will abort a work in progress release.'; @override - void run() { + Future run() { final ArgResults argumentResults = argResults!; final File stateFile = checkouts.fileSystem.file(argumentResults[kStateOption]); if (!stateFile.existsSync()) { - throw ConductorException( - 'No persistent state file found at ${stateFile.path}!'); + throw ConductorException('No persistent state file found at ${stateFile.path}!'); } if (!(argumentResults[kYesFlag] as bool)) { @@ -67,10 +66,28 @@ class CleanCommand extends Command { // Only proceed if the first character of stdin is 'y' or 'Y' if (response.isEmpty || response[0].toLowerCase() != 'y') { stdio.printStatus('Aborting clean operation.'); - return; } } stdio.printStatus('Deleting persistent state file ${stateFile.path}...'); - stateFile.deleteSync(); + + final RunContext context = RunContext( + stateFile: stateFile, + ); + return context.run(); + } +} + +/// Context for cleaning up persistent state file. +/// +/// This is a frontend-agnostic implementation. +class RunContext { + RunContext({ + required this.stateFile, + }); + + final File stateFile; + + Future run() { + return stateFile.delete(); } } diff --git a/dev/conductor/core/test/clean_test.dart b/dev/conductor/core/test/clean_test.dart index 2ba7a95494..c736b67556 100644 --- a/dev/conductor/core/test/clean_test.dart +++ b/dev/conductor/core/test/clean_test.dart @@ -15,6 +15,7 @@ void main() { group('clean command', () { const String flutterRoot = '/flutter'; const String checkoutsParentDirectory = '$flutterRoot/dev/tools/'; + const String stateFilePath = '/state-file.json'; late MemoryFileSystem fileSystem; late FakePlatform platform; @@ -47,24 +48,36 @@ void main() { }); test('throws if no state file found', () async { - const String stateFile = '/state-file.json'; - await expectLater( () async => runner.run([ 'clean', '--$kStateOption', - stateFile, + stateFilePath, '--$kYesFlag', ]), throwsExceptionWith( - 'No persistent state file found at $stateFile', + 'No persistent state file found at $stateFilePath', ), ); }); - test('deletes state file', () async { - final File stateFile = fileSystem.file('/state-file.json'); - stateFile.writeAsStringSync('{}'); + test('deletes an empty state file', () async { + final File stateFile = fileSystem.file(stateFilePath); + stateFile.writeAsStringSync(''); + + await runner.run([ + 'clean', + '--$kStateOption', + stateFile.path, + '--$kYesFlag', + ]); + + expect(stateFile.existsSync(), false); + }); + + test('deletes a state file with content', () async { + final File stateFile = fileSystem.file(stateFilePath); + stateFile.writeAsStringSync('{status: pending}'); await runner.run([ 'clean',