1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/version.dart';
import 'package:flutter_tools/src/commands/clean.dart';
import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:flutter_tools/src/macos/xcode.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:meta/meta.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
import '../../src/context.dart';
void main() {
group('clean command', () {
Xcode xcode;
FakeXcodeProjectInterpreter xcodeProjectInterpreter;
setUp(() {
xcodeProjectInterpreter = FakeXcodeProjectInterpreter();
xcode = Xcode.test(
processManager: FakeProcessManager.any(),
xcodeProjectInterpreter: xcodeProjectInterpreter,
);
});
group('general', () {
MemoryFileSystem fs;
Directory buildDirectory;
setUp(() {
fs = MemoryFileSystem.test();
final Directory currentDirectory = fs.currentDirectory;
buildDirectory = currentDirectory.childDirectory('build');
buildDirectory.createSync(recursive: true);
});
testUsingContext('$CleanCommand removes build and .dart_tool and ephemeral directories, cleans Xcode for iOS and macOS', () async {
final FlutterProject projectUnderTest = setupProjectUnderTest(fs.currentDirectory);
// Xcode is installed and version satisfactory.
xcodeProjectInterpreter.isInstalled = true;
xcodeProjectInterpreter.version = Version(1000, 0, 0);
await CleanCommand().runCommand();
expect(buildDirectory, isNot(exists));
expect(projectUnderTest.dartTool, isNot(exists));
expect(projectUnderTest.android.ephemeralDirectory, isNot(exists));
expect(projectUnderTest.ios.ephemeralDirectory, isNot(exists));
expect(projectUnderTest.ios.ephemeralModuleDirectory, isNot(exists));
expect(projectUnderTest.ios.generatedXcodePropertiesFile, isNot(exists));
expect(projectUnderTest.ios.generatedEnvironmentVariableExportScript, isNot(exists));
expect(projectUnderTest.ios.deprecatedCompiledDartFramework, isNot(exists));
expect(projectUnderTest.ios.deprecatedProjectFlutterFramework, isNot(exists));
expect(projectUnderTest.ios.flutterPodspec, isNot(exists));
expect(projectUnderTest.linux.ephemeralDirectory, isNot(exists));
expect(projectUnderTest.macos.ephemeralDirectory, isNot(exists));
expect(projectUnderTest.windows.ephemeralDirectory, isNot(exists));
expect(projectUnderTest.flutterPluginsFile, isNot(exists));
expect(projectUnderTest.flutterPluginsDependenciesFile, isNot(exists));
expect(projectUnderTest.packagesFile, isNot(exists));
expect(xcodeProjectInterpreter.workspaces, const <CleanWorkspaceCall>[
CleanWorkspaceCall('/ios/Runner.xcworkspace', 'Runner', false),
CleanWorkspaceCall('/macos/Runner.xcworkspace', 'Runner', false),
]);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Xcode: () => xcode,
XcodeProjectInterpreter: () => xcodeProjectInterpreter,
});
testUsingContext('$CleanCommand cleans Xcode verbosely for iOS and macOS', () async {
setupProjectUnderTest(fs.currentDirectory);
// Xcode is installed and version satisfactory.
xcodeProjectInterpreter.isInstalled = true;
xcodeProjectInterpreter.version = Version(1000, 0, 0);
await CleanCommand(verbose: true).runCommand();
expect(xcodeProjectInterpreter.workspaces, const <CleanWorkspaceCall>[
CleanWorkspaceCall('/ios/Runner.xcworkspace', 'Runner', true),
CleanWorkspaceCall('/macos/Runner.xcworkspace', 'Runner', true),
]);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
Xcode: () => xcode,
XcodeProjectInterpreter: () => xcodeProjectInterpreter,
});
});
group('Windows', () {
FakePlatform windowsPlatform;
MemoryFileSystem fileSystem;
FileExceptionHandler exceptionHandler;
setUp(() {
windowsPlatform = FakePlatform(operatingSystem: 'windows');
exceptionHandler = FileExceptionHandler();
fileSystem = MemoryFileSystem.test(opHandle: exceptionHandler.opHandle);
});
testUsingContext('$CleanCommand prints a helpful error message on Windows', () async {
xcodeProjectInterpreter.isInstalled = false;
final File file = fileSystem.file('file')..createSync();
exceptionHandler.addError(
file,
FileSystemOp.delete,
const FileSystemException('Deletion failed'),
);
final CleanCommand command = CleanCommand();
command.deleteFile(file);
expect(testLogger.errorText, contains('A program may still be using a file'));
}, overrides: <Type, Generator>{
Platform: () => windowsPlatform,
Xcode: () => xcode,
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('$CleanCommand handles missing delete permissions', () async {
final FileExceptionHandler handler = FileExceptionHandler();
final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
final File throwingFile = fileSystem.file('bad')
..createSync();
handler.addError(throwingFile, FileSystemOp.delete, const FileSystemException('OS error: Access Denied'));
xcodeProjectInterpreter.isInstalled = false;
final CleanCommand command = CleanCommand();
command.deleteFile(throwingFile);
expect(testLogger.errorText, contains('Failed to remove bad. A program may still be using a file in the directory or the directory itself'));
expect(throwingFile, exists);
}, overrides: <Type, Generator>{
Platform: () => windowsPlatform,
Xcode: () => xcode,
});
});
});
}
FlutterProject setupProjectUnderTest(Directory currentDirectory) {
// This needs to be run within testWithoutContext and not setUp since FlutterProject uses context.
final FlutterProject projectUnderTest = FlutterProject.fromDirectory(currentDirectory);
projectUnderTest.ios.xcodeWorkspace.createSync(recursive: true);
projectUnderTest.macos.xcodeWorkspace.createSync(recursive: true);
projectUnderTest.dartTool.createSync(recursive: true);
projectUnderTest.packagesFile.createSync(recursive: true);
projectUnderTest.android.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.ios.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.ios.ephemeralModuleDirectory.createSync(recursive: true);
projectUnderTest.ios.generatedXcodePropertiesFile.createSync(recursive: true);
projectUnderTest.ios.generatedEnvironmentVariableExportScript.createSync(recursive: true);
projectUnderTest.ios.deprecatedCompiledDartFramework.createSync(recursive: true);
projectUnderTest.ios.deprecatedProjectFlutterFramework.createSync(recursive: true);
projectUnderTest.ios.flutterPodspec.createSync(recursive: true);
projectUnderTest.linux.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.macos.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.windows.ephemeralDirectory.createSync(recursive: true);
projectUnderTest.flutterPluginsFile.createSync(recursive: true);
projectUnderTest.flutterPluginsDependenciesFile.createSync(recursive: true);
return projectUnderTest;
}
class FakeXcodeProjectInterpreter extends Fake implements XcodeProjectInterpreter {
@override
bool isInstalled = true;
@override
Version version = Version(0, 0, 0);
@override
Future<XcodeProjectInfo> getInfo(String projectPath, {String projectFilename}) async {
return XcodeProjectInfo(null, null, <String>['Runner'], BufferLogger.test());
}
final List<CleanWorkspaceCall> workspaces = <CleanWorkspaceCall>[];
@override
Future<void> cleanWorkspace(String workspacePath, String scheme, {bool verbose = false}) async {
workspaces.add(CleanWorkspaceCall(workspacePath, scheme, verbose));
return;
}
}
@immutable
class CleanWorkspaceCall {
const CleanWorkspaceCall(this.workspacePath, this.scheme, this.verbose);
final String workspacePath;
final String scheme;
final bool verbose;
@override
bool operator ==(Object other) => other is CleanWorkspaceCall &&
workspacePath == other.workspacePath &&
scheme == other.scheme &&
verbose == other.verbose;
@override
int get hashCode => Object.hash(workspacePath, scheme, verbose);
@override
String toString() => '{$workspacePath, $scheme, $verbose}';
}