-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
overrides.dart
527 lines (444 loc) · 16.1 KB
/
overrides.dart
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. 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.6
part of dart.io;
final _ioOverridesToken = new Object();
const _asyncRunZoned = runZoned;
/// This class facilitates overriding various APIs of dart:io with mock
/// implementations.
///
/// This abstract base class should be extended with overrides for the
/// operations needed to construct mocks. The implementations in this base class
/// default to the actual dart:io implementation. For example:
///
/// ```
/// class MyDirectory implements Directory {
/// ...
/// // An implementation of the Directory interface
/// ...
/// }
///
/// main() {
/// IOOverrides.runZoned(() {
/// ...
/// // Operations will use MyDirectory instead of dart:io's Directory
/// // implementation whenever Directory is used.
/// ...
/// }, createDirectory: (String path) => new MyDirectory(path));
/// }
/// ```
abstract class IOOverrides {
static IOOverrides _global;
static IOOverrides get current {
return Zone.current[_ioOverridesToken] ?? _global;
}
/// The [IOOverrides] to use in the root [Zone].
///
/// These are the [IOOverrides] that will be used in the root Zone, and in
/// Zone's that do not set [IOOverrides] and whose ancestors up to the root
/// Zone do not set [IOOverrides].
static set global(IOOverrides overrides) {
_global = overrides;
}
/// Runs [body] in a fresh [Zone] using the provided overrides.
///
/// See the documentation on the corresponding methods of IOOverrides for
/// information about what the optional arguments do.
static R runZoned<R>(R body(),
{
// Directory
Directory Function(String) createDirectory,
Directory Function() getCurrentDirectory,
void Function(String) setCurrentDirectory,
Directory Function() getSystemTempDirectory,
// File
File Function(String) createFile,
// FileStat
Future<FileStat> Function(String) stat,
FileStat Function(String) statSync,
// FileSystemEntity
Future<bool> Function(String, String) fseIdentical,
bool Function(String, String) fseIdenticalSync,
Future<FileSystemEntityType> Function(String, bool) fseGetType,
FileSystemEntityType Function(String, bool) fseGetTypeSync,
// _FileSystemWatcher
Stream<FileSystemEvent> Function(String, int, bool) fsWatch,
bool Function() fsWatchIsSupported,
// Link
Link Function(String) createLink,
// Socket
Future<Socket> Function(dynamic, int,
{dynamic sourceAddress, Duration timeout})
socketConnect,
Future<ConnectionTask<Socket>> Function(dynamic, int,
{dynamic sourceAddress})
socketStartConnect,
// ServerSocket
Future<ServerSocket> Function(dynamic, int,
{int backlog, bool v6Only, bool shared})
serverSocketBind,
// Optional Zone parameters
ZoneSpecification zoneSpecification,
Function onError}) {
IOOverrides overrides = new _IOOverridesScope(
// Directory
createDirectory,
getCurrentDirectory,
setCurrentDirectory,
getSystemTempDirectory,
// File
createFile,
// FileStat
stat,
statSync,
// FileSystemEntity
fseIdentical,
fseIdenticalSync,
fseGetType,
fseGetTypeSync,
// _FileSystemWatcher
fsWatch,
fsWatchIsSupported,
// Link
createLink,
// Socket
socketConnect,
socketStartConnect,
// ServerSocket
serverSocketBind,
);
return _asyncRunZoned<R>(body,
zoneValues: {_ioOverridesToken: overrides},
zoneSpecification: zoneSpecification,
onError: onError);
}
/// Runs [body] in a fresh [Zone] using the overrides found in [overrides].
///
/// Note that [overrides] should be an instance of a class that extends
/// [IOOverrides].
static R runWithIOOverrides<R>(R body(), IOOverrides overrides,
{ZoneSpecification zoneSpecification, Function onError}) {
return _asyncRunZoned<R>(body,
zoneValues: {_ioOverridesToken: overrides},
zoneSpecification: zoneSpecification,
onError: onError);
}
// Directory
/// Creates a new [Directory] object for the given [path].
///
/// When this override is installed, this function overrides the behavior of
/// `new Directory()` and `new Directory.fromUri()`.
Directory createDirectory(String path) => new _Directory(path);
/// Returns the current working directory.
///
/// When this override is installed, this function overrides the behavior of
/// the static getter `Directory.current`
Directory getCurrentDirectory() => _Directory.current;
/// Sets the current working directory to be [path].
///
/// When this override is installed, this function overrides the behavior of
/// the setter `Directory.current`.
void setCurrentDirectory(String path) {
_Directory.current = path;
}
/// Returns the system temporary directory.
///
/// When this override is installed, this function overrides the behavior of
/// `Directory.systemTemp`.
Directory getSystemTempDirectory() => _Directory.systemTemp;
// File
/// Creates a new [File] object for the given [path].
///
/// When this override is installed, this function overrides the behavior of
/// `new File()` and `new File.fromUri()`.
File createFile(String path) => new _File(path);
// FileStat
/// Asynchronously returns [FileStat] information for [path].
///
/// When this override is installed, this function overrides the behavior of
/// `FileStat.stat()`.
Future<FileStat> stat(String path) {
return FileStat._stat(path);
}
/// Returns [FileStat] information for [path].
///
/// When this override is installed, this function overrides the behavior of
/// `FileStat.statSync()`.
FileStat statSync(String path) {
return FileStat._statSyncInternal(path);
}
// FileSystemEntity
/// Asynchronously returns `true` if [path1] and [path2] are paths to the
/// same file system object.
///
/// When this override is installed, this function overrides the behavior of
/// `FileSystemEntity.identical`.
Future<bool> fseIdentical(String path1, String path2) {
return FileSystemEntity._identical(path1, path2);
}
/// Returns `true` if [path1] and [path2] are paths to the
/// same file system object.
///
/// When this override is installed, this function overrides the behavior of
/// `FileSystemEntity.identicalSync`.
bool fseIdenticalSync(String path1, String path2) {
return FileSystemEntity._identicalSync(path1, path2);
}
/// Asynchronously returns the [FileSystemEntityType] for [path].
///
/// When this override is installed, this function overrides the behavior of
/// `FileSystemEntity.type`.
Future<FileSystemEntityType> fseGetType(String path, bool followLinks) {
return FileSystemEntity._getTypeRequest(utf8.encode(path), followLinks);
}
/// Returns the [FileSystemEntityType] for [path].
///
/// When this override is installed, this function overrides the behavior of
/// `FileSystemEntity.typeSync`.
FileSystemEntityType fseGetTypeSync(String path, bool followLinks) {
return FileSystemEntity._getTypeSyncHelper(utf8.encode(path), followLinks);
}
// _FileSystemWatcher
/// Returns a [Stream] of [FileSystemEvent]s.
///
/// When this override is installed, this function overrides the behavior of
/// `FileSystemEntity.watch()`.
Stream<FileSystemEvent> fsWatch(String path, int events, bool recursive) {
return _FileSystemWatcher._watch(path, events, recursive);
}
/// Returns `true` when [FileSystemEntity.watch] is supported.
///
/// When this override is installed, this function overrides the behavior of
/// `FileSystemEntity.isWatchSupported`.
bool fsWatchIsSupported() => _FileSystemWatcher.isSupported;
// Link
/// Returns a new [Link] object for the given [path].
///
/// When this override is installed, this function overrides the behavior of
/// `new Link()` and `new Link.fromUri()`.
Link createLink(String path) => new _Link(path);
// Socket
/// Asynchronously returns a [Socket] connected to the given host and port.
///
/// When this override is installed, this functions overrides the behavior of
/// `Socket.connect(...)`.
Future<Socket> socketConnect(host, int port,
{sourceAddress, Duration timeout}) {
return Socket._connect(host, port,
sourceAddress: sourceAddress, timeout: timeout);
}
/// Asynchronously returns a [ConnectionTask] that connects to the given host
/// and port when successful.
///
/// When this override is installed, this functions overrides the behavior of
/// `Socket.startConnect(...)`.
Future<ConnectionTask<Socket>> socketStartConnect(host, int port,
{sourceAddress}) {
return Socket._startConnect(host, port, sourceAddress: sourceAddress);
}
// ServerSocket
/// Asynchronously returns a [ServerSocket] that connects to the given address
/// and port when successful.
///
/// When this override is installed, this functions overrides the behavior of
/// `ServerSocket.bind(...)`.
Future<ServerSocket> serverSocketBind(address, int port,
{int backlog: 0, bool v6Only: false, bool shared: false}) {
return ServerSocket._bind(address, port,
backlog: backlog, v6Only: v6Only, shared: shared);
}
}
class _IOOverridesScope extends IOOverrides {
final IOOverrides _previous = IOOverrides.current;
// Directory
Directory Function(String) _createDirectory;
Directory Function() _getCurrentDirectory;
void Function(String) _setCurrentDirectory;
Directory Function() _getSystemTempDirectory;
// File
File Function(String) _createFile;
// FileStat
Future<FileStat> Function(String) _stat;
FileStat Function(String) _statSync;
// FileSystemEntity
Future<bool> Function(String, String) _fseIdentical;
bool Function(String, String) _fseIdenticalSync;
Future<FileSystemEntityType> Function(String, bool) _fseGetType;
FileSystemEntityType Function(String, bool) _fseGetTypeSync;
// _FileSystemWatcher
Stream<FileSystemEvent> Function(String, int, bool) _fsWatch;
bool Function() _fsWatchIsSupported;
// Link
Link Function(String) _createLink;
// Socket
Future<Socket> Function(dynamic, int,
{dynamic sourceAddress, Duration timeout}) _socketConnect;
Future<ConnectionTask<Socket>> Function(dynamic, int, {dynamic sourceAddress})
_socketStartConnect;
// ServerSocket
Future<ServerSocket> Function(dynamic, int,
{int backlog, bool v6Only, bool shared}) _serverSocketBind;
_IOOverridesScope(
// Directory
this._createDirectory,
this._getCurrentDirectory,
this._setCurrentDirectory,
this._getSystemTempDirectory,
// File
this._createFile,
// FileStat
this._stat,
this._statSync,
// FileSystemEntity
this._fseIdentical,
this._fseIdenticalSync,
this._fseGetType,
this._fseGetTypeSync,
// _FileSystemWatcher
this._fsWatch,
this._fsWatchIsSupported,
// Link
this._createLink,
// Socket
this._socketConnect,
this._socketStartConnect,
// ServerSocket
this._serverSocketBind,
);
// Directory
@override
Directory createDirectory(String path) {
if (_createDirectory != null) return _createDirectory(path);
if (_previous != null) return _previous.createDirectory(path);
return super.createDirectory(path);
}
@override
Directory getCurrentDirectory() {
if (_getCurrentDirectory != null) return _getCurrentDirectory();
if (_previous != null) return _previous.getCurrentDirectory();
return super.getCurrentDirectory();
}
@override
void setCurrentDirectory(String path) {
if (_setCurrentDirectory != null)
_setCurrentDirectory(path);
else if (_previous != null)
_previous.setCurrentDirectory(path);
else
super.setCurrentDirectory(path);
}
@override
Directory getSystemTempDirectory() {
if (_getSystemTempDirectory != null) return _getSystemTempDirectory();
if (_previous != null) return _previous.getSystemTempDirectory();
return super.getSystemTempDirectory();
}
// File
@override
File createFile(String path) {
if (_createFile != null) return _createFile(path);
if (_previous != null) return _previous.createFile(path);
return super.createFile(path);
}
// FileStat
@override
Future<FileStat> stat(String path) {
if (_stat != null) return _stat(path);
if (_previous != null) return _previous.stat(path);
return super.stat(path);
}
@override
FileStat statSync(String path) {
if (_stat != null) return _statSync(path);
if (_previous != null) return _previous.statSync(path);
return super.statSync(path);
}
// FileSystemEntity
@override
Future<bool> fseIdentical(String path1, String path2) {
if (_fseIdentical != null) return _fseIdentical(path1, path2);
if (_previous != null) return _previous.fseIdentical(path1, path2);
return super.fseIdentical(path1, path2);
}
@override
bool fseIdenticalSync(String path1, String path2) {
if (_fseIdenticalSync != null) return _fseIdenticalSync(path1, path2);
if (_previous != null) return _previous.fseIdenticalSync(path1, path2);
return super.fseIdenticalSync(path1, path2);
}
@override
Future<FileSystemEntityType> fseGetType(String path, bool followLinks) {
if (_fseGetType != null) return _fseGetType(path, followLinks);
if (_previous != null) return _previous.fseGetType(path, followLinks);
return super.fseGetType(path, followLinks);
}
@override
FileSystemEntityType fseGetTypeSync(String path, bool followLinks) {
if (_fseGetTypeSync != null) return _fseGetTypeSync(path, followLinks);
if (_previous != null) return _previous.fseGetTypeSync(path, followLinks);
return super.fseGetTypeSync(path, followLinks);
}
// _FileSystemWatcher
@override
Stream<FileSystemEvent> fsWatch(String path, int events, bool recursive) {
if (_fsWatch != null) return _fsWatch(path, events, recursive);
if (_previous != null) return _previous.fsWatch(path, events, recursive);
return super.fsWatch(path, events, recursive);
}
@override
bool fsWatchIsSupported() {
if (_fsWatchIsSupported != null) return _fsWatchIsSupported();
if (_previous != null) return _previous.fsWatchIsSupported();
return super.fsWatchIsSupported();
}
// Link
@override
Link createLink(String path) {
if (_createLink != null) return _createLink(path);
if (_previous != null) return _previous.createLink(path);
return super.createLink(path);
}
// Socket
@override
Future<Socket> socketConnect(host, int port,
{sourceAddress, Duration timeout}) {
if (_socketConnect != null) {
return _socketConnect(host, port,
sourceAddress: sourceAddress, timeout: timeout);
}
if (_previous != null) {
return _previous.socketConnect(host, port,
sourceAddress: sourceAddress, timeout: timeout);
}
return super.socketConnect(host, port,
sourceAddress: sourceAddress, timeout: timeout);
}
@override
Future<ConnectionTask<Socket>> socketStartConnect(host, int port,
{sourceAddress}) {
if (_socketStartConnect != null) {
return _socketStartConnect(host, port, sourceAddress: sourceAddress);
}
if (_previous != null) {
return _previous.socketStartConnect(host, port,
sourceAddress: sourceAddress);
}
return super.socketStartConnect(host, port, sourceAddress: sourceAddress);
}
// ServerSocket
@override
Future<ServerSocket> serverSocketBind(address, int port,
{int backlog: 0, bool v6Only: false, bool shared: false}) {
if (_serverSocketBind != null) {
return _serverSocketBind(address, port,
backlog: backlog, v6Only: v6Only, shared: shared);
}
if (_previous != null) {
return _previous.serverSocketBind(address, port,
backlog: backlog, v6Only: v6Only, shared: shared);
}
return super.serverSocketBind(address, port,
backlog: backlog, v6Only: v6Only, shared: shared);
}
}