Unverified Commit 0407a318 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Replace clean command test functions with test groups (#51508)

parent cd97f23a
...@@ -18,86 +18,99 @@ import '../../src/common.dart'; ...@@ -18,86 +18,99 @@ import '../../src/common.dart';
import '../../src/context.dart'; import '../../src/context.dart';
void main() { void main() {
void test1() { group('clean command', () {
final MemoryFileSystem fs = MemoryFileSystem(); MockXcode mockXcode;
final MockXcodeProjectInterpreter mockXcodeProjectInterpreter = MockXcodeProjectInterpreter(); setUp(() {
final MockXcode mockXcode = MockXcode(); mockXcode = MockXcode();
final Directory currentDirectory = fs.currentDirectory;
final Directory buildDirectory = currentDirectory.childDirectory('build');
buildDirectory.createSync(recursive: true);
final FlutterProject projectUnderTest = FlutterProject.fromDirectory(currentDirectory);
projectUnderTest.ios.xcodeWorkspace.createSync(recursive: true);
projectUnderTest.macos.xcodeWorkspace.createSync(recursive: true);
projectUnderTest.dartTool.createSync(recursive: true);
projectUnderTest.android.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.ios.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.linux.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.macos.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.windows.ephemeralDirectory.createSync(recursive: true);
testUsingContext('$CleanCommand removes build and .dart_tool and ephemeral directories, cleans Xcode', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
await CleanCommand().runCommand();
expect(buildDirectory.existsSync(), isFalse);
expect(projectUnderTest.dartTool.existsSync(), isFalse);
expect(projectUnderTest.android.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.ios.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.linux.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.macos.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.windows.ephemeralDirectory.existsSync(), isFalse);
verify(xcodeProjectInterpreter.cleanWorkspace(any, 'Runner')).called(2);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Xcode: () => mockXcode,
XcodeProjectInterpreter: () => mockXcodeProjectInterpreter,
}); });
}
void test2() { group('general', () {
final MockXcode mockXcode = MockXcode(); MemoryFileSystem fs;
final MockPlatform windowsPlatform = MockPlatform(); MockXcodeProjectInterpreter mockXcodeProjectInterpreter;
testUsingContext('$CleanCommand prints a helpful error message on Windows', () async { Directory buildDirectory;
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false); FlutterProject projectUnderTest;
when(windowsPlatform.isWindows).thenReturn(true);
setUp(() {
final MockFile mockFile = MockFile(); fs = MemoryFileSystem();
when(mockFile.existsSync()).thenReturn(true); mockXcodeProjectInterpreter = MockXcodeProjectInterpreter();
when(mockFile.deleteSync(recursive: true)).thenThrow(const FileSystemException('Deletion failed')); final Directory currentDirectory = fs.currentDirectory;
final CleanCommand command = CleanCommand(); buildDirectory = currentDirectory.childDirectory('build');
command.deleteFile(mockFile); buildDirectory.createSync(recursive: true);
expect(testLogger.errorText, contains('A program may still be using a file'));
verify(mockFile.deleteSync(recursive: true)).called(1); projectUnderTest = FlutterProject.fromDirectory(currentDirectory);
}, overrides: <Type, Generator>{ projectUnderTest.ios.xcodeWorkspace.createSync(recursive: true);
Platform: () => windowsPlatform, projectUnderTest.macos.xcodeWorkspace.createSync(recursive: true);
Xcode: () => mockXcode,
projectUnderTest.dartTool.createSync(recursive: true);
projectUnderTest.android.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.ios.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.linux.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.macos.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.windows.ephemeralDirectory.createSync(recursive: true);
});
testUsingContext('$CleanCommand removes build and .dart_tool and ephemeral directories, cleans Xcode', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
await CleanCommand().runCommand();
expect(buildDirectory.existsSync(), isFalse);
expect(projectUnderTest.dartTool.existsSync(), isFalse);
expect(projectUnderTest.android.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.ios.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.linux.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.macos.ephemeralDirectory.existsSync(), isFalse);
expect(projectUnderTest.windows.ephemeralDirectory.existsSync(), isFalse);
verify(xcodeProjectInterpreter.cleanWorkspace(any, 'Runner')).called(2);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Xcode: () => mockXcode,
XcodeProjectInterpreter: () => mockXcodeProjectInterpreter,
});
}); });
testUsingContext('$CleanCommand handles missing permissions;', () async { group('Windows', () {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false); MockPlatform windowsPlatform;
setUp(() {
final MockFile mockFile = MockFile(); windowsPlatform = MockPlatform();
when(mockFile.existsSync()).thenThrow(const FileSystemException('OS error: Access Denied')); });
when(mockFile.path).thenReturn('foo.dart');
testUsingContext('$CleanCommand prints a helpful error message on Windows', () async {
final CleanCommand command = CleanCommand(); when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
command.deleteFile(mockFile); when(windowsPlatform.isWindows).thenReturn(true);
expect(testLogger.errorText, contains('Cannot clean foo.dart'));
verifyNever(mockFile.deleteSync(recursive: true)); final MockFile mockFile = MockFile();
}, overrides: <Type, Generator>{ when(mockFile.existsSync()).thenReturn(true);
Platform: () => windowsPlatform,
Xcode: () => mockXcode, when(mockFile.deleteSync(recursive: true)).thenThrow(const FileSystemException('Deletion failed'));
final CleanCommand command = CleanCommand();
command.deleteFile(mockFile);
expect(testLogger.errorText, contains('A program may still be using a file'));
verify(mockFile.deleteSync(recursive: true)).called(1);
}, overrides: <Type, Generator>{
Platform: () => windowsPlatform,
Xcode: () => mockXcode,
});
testUsingContext('$CleanCommand handles missing permissions;', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
final MockFile mockFile = MockFile();
when(mockFile.existsSync()).thenThrow(const FileSystemException('OS error: Access Denied'));
when(mockFile.path).thenReturn('foo.dart');
final CleanCommand command = CleanCommand();
command.deleteFile(mockFile);
expect(testLogger.errorText, contains('Cannot clean foo.dart'));
verifyNever(mockFile.deleteSync(recursive: true));
}, overrides: <Type, Generator>{
Platform: () => windowsPlatform,
Xcode: () => mockXcode,
});
}); });
} });
test1();
test2();
} }
class MockFile extends Mock implements File {} class MockFile extends Mock implements File {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment