|
3 | 3 | // found in the LICENSE file.
|
4 | 4 |
|
5 | 5 | import 'dart:async';
|
| 6 | + |
6 | 7 | import 'package:fake_async/fake_async.dart';
|
| 8 | +import 'package:flutter_tools/src/base/io.dart'; |
| 9 | +import 'package:flutter_tools/src/convert.dart' show utf8; |
7 | 10 |
|
8 | 11 | import '../src/common.dart';
|
9 | 12 | import '../src/fake_process_manager.dart';
|
@@ -172,4 +175,149 @@ void main() {
|
172 | 175 | expect(stdout, 'stdout'.codeUnits);
|
173 | 176 | });
|
174 | 177 | });
|
| 178 | + |
| 179 | + group(FakeProcessManager, () { |
| 180 | + late FakeProcessManager manager; |
| 181 | + |
| 182 | + setUp(() { |
| 183 | + manager = FakeProcessManager.empty(); |
| 184 | + }); |
| 185 | + |
| 186 | + group('start', () { |
| 187 | + testWithoutContext('can run a fake command', () async { |
| 188 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 189 | + |
| 190 | + final Process process = await manager.start(<String>['faketool']); |
| 191 | + expect(await process.exitCode, 0); |
| 192 | + expect(await utf8.decodeStream(process.stdout), isEmpty); |
| 193 | + expect(await utf8.decodeStream(process.stderr), isEmpty); |
| 194 | + }); |
| 195 | + |
| 196 | + testWithoutContext('outputFollowsExit delays stderr, stdout until after process exit', () async { |
| 197 | + manager.addCommand(const FakeCommand( |
| 198 | + command: <String>['faketool'], |
| 199 | + stderr: 'hello', |
| 200 | + stdout: 'world', |
| 201 | + outputFollowsExit: true, |
| 202 | + )); |
| 203 | + |
| 204 | + final List<int> stderrBytes = <int>[]; |
| 205 | + final List<int> stdoutBytes = <int>[]; |
| 206 | + |
| 207 | + // Start the process. |
| 208 | + final Process process = await manager.start(<String>['faketool']); |
| 209 | + final StreamSubscription<List<int>> stderrSubscription = process.stderr.listen((List<int> chunk) { stderrBytes.addAll(chunk); }); |
| 210 | + final StreamSubscription<List<int>> stdoutSubscription = process.stdout.listen((List<int> chunk) { stdoutBytes.addAll(chunk); }); |
| 211 | + |
| 212 | + // Immediately after exit, no output is emitted. |
| 213 | + await process.exitCode; |
| 214 | + expect(utf8.decode(stderrBytes), isEmpty); |
| 215 | + expect(utf8.decode(stdoutBytes), isEmpty); |
| 216 | + |
| 217 | + // Output is emitted asynchronously after process exit. |
| 218 | + await Future.wait(<Future<void>>[ |
| 219 | + stderrSubscription.asFuture(), |
| 220 | + stdoutSubscription.asFuture(), |
| 221 | + ]); |
| 222 | + expect(utf8.decode(stderrBytes), 'hello'); |
| 223 | + expect(utf8.decode(stdoutBytes), 'world'); |
| 224 | + |
| 225 | + // Clean up stream subscriptions. |
| 226 | + await stderrSubscription.cancel(); |
| 227 | + await stdoutSubscription.cancel(); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + group('run', () { |
| 232 | + testWithoutContext('can run a fake command', () async { |
| 233 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 234 | + |
| 235 | + final ProcessResult result = await manager.run(<String>['faketool']); |
| 236 | + expect(result.exitCode, 0); |
| 237 | + expect(result.stdout, isEmpty); |
| 238 | + expect(result.stderr, isEmpty); |
| 239 | + }); |
| 240 | + |
| 241 | + testWithoutContext('stderr, stdout are String if encoding is unspecified', () async { |
| 242 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 243 | + |
| 244 | + final ProcessResult result = await manager.run(<String>['faketool']); |
| 245 | + expect(result.exitCode, 0); |
| 246 | + expect(result.stdout, isA<String>()); |
| 247 | + expect(result.stderr, isA<String>()); |
| 248 | + }); |
| 249 | + |
| 250 | + testWithoutContext('stderr, stdout are List<int> if encoding is null', () async { |
| 251 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 252 | + |
| 253 | + final ProcessResult result = await manager.run( |
| 254 | + <String>['faketool'], |
| 255 | + stderrEncoding: null, |
| 256 | + stdoutEncoding: null, |
| 257 | + ); |
| 258 | + expect(result.exitCode, 0); |
| 259 | + expect(result.stdout, isA<List<int>>()); |
| 260 | + expect(result.stderr, isA<List<int>>()); |
| 261 | + }); |
| 262 | + |
| 263 | + testWithoutContext('stderr, stdout are String if encoding is specified', () async { |
| 264 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 265 | + |
| 266 | + final ProcessResult result = await manager.run( |
| 267 | + <String>['faketool'], |
| 268 | + stderrEncoding: utf8, |
| 269 | + stdoutEncoding: utf8, |
| 270 | + ); |
| 271 | + expect(result.exitCode, 0); |
| 272 | + expect(result.stdout, isA<String>()); |
| 273 | + expect(result.stderr, isA<String>()); |
| 274 | + }); |
| 275 | + }); |
| 276 | + |
| 277 | + group('runSync', () { |
| 278 | + testWithoutContext('can run a fake command', () { |
| 279 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 280 | + |
| 281 | + final ProcessResult result = manager.runSync(<String>['faketool']); |
| 282 | + expect(result.exitCode, 0); |
| 283 | + expect(result.stdout, isEmpty); |
| 284 | + expect(result.stderr, isEmpty); |
| 285 | + }); |
| 286 | + |
| 287 | + testWithoutContext('stderr, stdout are String if encoding is unspecified', () { |
| 288 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 289 | + |
| 290 | + final ProcessResult result = manager.runSync(<String>['faketool']); |
| 291 | + expect(result.exitCode, 0); |
| 292 | + expect(result.stdout, isA<String>()); |
| 293 | + expect(result.stderr, isA<String>()); |
| 294 | + }); |
| 295 | + |
| 296 | + testWithoutContext('stderr, stdout are List<int> if encoding is null', () { |
| 297 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 298 | + |
| 299 | + final ProcessResult result = manager.runSync( |
| 300 | + <String>['faketool'], |
| 301 | + stderrEncoding: null, |
| 302 | + stdoutEncoding: null, |
| 303 | + ); |
| 304 | + expect(result.exitCode, 0); |
| 305 | + expect(result.stdout, isA<List<int>>()); |
| 306 | + expect(result.stderr, isA<List<int>>()); |
| 307 | + }); |
| 308 | + |
| 309 | + testWithoutContext('stderr, stdout are String if encoding is specified', () { |
| 310 | + manager.addCommand(const FakeCommand(command: <String>['faketool'])); |
| 311 | + |
| 312 | + final ProcessResult result = manager.runSync( |
| 313 | + <String>['faketool'], |
| 314 | + stderrEncoding: utf8, |
| 315 | + stdoutEncoding: utf8, |
| 316 | + ); |
| 317 | + expect(result.exitCode, 0); |
| 318 | + expect(result.stdout, isA<String>()); |
| 319 | + expect(result.stderr, isA<String>()); |
| 320 | + }); |
| 321 | + }); |
| 322 | + }); |
175 | 323 | }
|
0 commit comments