IOOverrides abstract base#
Facilities for 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
...
}
void main() {
IOOverrides.runZoned(() {
...
// Operations will use MyDirectory instead of dart:io's Directory
// implementation whenever Directory is used.
...
}, createDirectory: (String path) => new MyDirectory(path));
}
Constructors#
IOOverrides()#
Properties#
hashCode no setter inherited#
The hash code for this object.
A hash code is a single integer which represents the state of the object that affects operator == comparisons.
All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).
If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.
Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.
Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.
If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.
Inherited from Object.
Implementation
external int get hashCode;
runtimeType no setter inherited#
A representation of the runtime type of the object.
Inherited from Object.
Implementation
external Type get runtimeType;
stderr no setter#
The standard output stream of errors written by this program.
When this override is installed, this getter overrides the behavior of
the top-level stderr getter.
Implementation
Stdout get stderr {
return _stderr;
}
stdin no setter#
The standard input stream of data read by this program.
When this override is installed, this getter overrides the behavior of
the top-level stdin getter.
Implementation
Stdin get stdin {
return _stdin;
}
stdout no setter#
The standard output stream of data written by this program.
When this override is installed, this getter overrides the behavior of
the top-level stdout getter.
Implementation
Stdout get stdout {
return _stdout;
}
Methods#
createDirectory()#
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().
Implementation
Directory createDirectory(String path) => _Directory(path);
createFile()#
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().
Implementation
File createFile(String path) => _File(path);
createLink()#
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().
Implementation
Link createLink(String path) => _Link(path);
exit()#
Exit the Dart VM process immediately with the given exit code.
When this override is installed, this function overrides the behavior of
exit(...).
Implementation
@Since("3.10")
Never exit(int code) {
if (!_EmbedderConfig._mayExit) {
throw new UnsupportedError(
"This embedder disallows calling dart:io's exit()",
);
}
_ProcessUtils._exit(code);
}
fseGetType()#
Asynchronously returns the FileSystemEntityType
for path.
When this override is installed, this function overrides the behavior of
FileSystemEntity.type.
Implementation
Future<FileSystemEntityType> fseGetType(String path, bool followLinks) {
return FileSystemEntity._getTypeRequest(utf8.encode(path), followLinks);
}
fseGetTypeSync()#
Returns the FileSystemEntityType
for path.
When this override is installed, this function overrides the behavior of
FileSystemEntity.typeSync.
Implementation
FileSystemEntityType fseGetTypeSync(String path, bool followLinks) {
return FileSystemEntity._getTypeSyncHelper(utf8.encode(path), followLinks);
}
fseIdentical()#
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.
Implementation
Future<bool> fseIdentical(String path1, String path2) {
return FileSystemEntity._identical(path1, path2);
}
fseIdenticalSync()#
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.
Implementation
bool fseIdenticalSync(String path1, String path2) {
return FileSystemEntity._identicalSync(path1, path2);
}
fsWatch()#
Returns a Stream of FileSystemEvents.
When this override is installed, this function overrides the behavior of
FileSystemEntity.watch().
Implementation
Stream<FileSystemEvent> fsWatch(String path, int events, bool recursive) {
return _FileSystemWatcher._watch(path, events, recursive);
}
fsWatchIsSupported()#
Returns true when FileSystemEntity.watch
is supported.
When this override is installed, this function overrides the behavior of
FileSystemEntity.isWatchSupported.
Implementation
bool fsWatchIsSupported() => _FileSystemWatcher.isSupported;
getCurrentDirectory()#
Returns the current working directory.
When this override is installed, this function overrides the behavior of
the static getter Directory.current
Implementation
Directory getCurrentDirectory() => _Directory.current;
getSystemTempDirectory()#
Returns the system temporary directory.
When this override is installed, this function overrides the behavior of
Directory.systemTemp.
Implementation
Directory getSystemTempDirectory() => _Directory.systemTemp;
noSuchMethod() inherited#
Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dynamic object = 1;
object.add(42); // Statically allowed, run-time error
This invalid code will invoke the noSuchMethod method
of the integer 1 with an Invocation
representing the
.add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}
This code has no compile-time warnings or errors even though
the MockList class has no concrete implementation of
any of the List interface methods.
Calls to List methods are forwarded to noSuchMethod,
so this code will log an invocation similar to
Invocation.method(#add, [42])
and then throw.
If a value is returned from noSuchMethod,
it becomes the result of the original invocation.
If the value is not of a type that can be returned by the original
invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);
serverSocketBind()#
Asynchronously returns a ServerSocket that connects to the given address and port when successful.
When this override is installed, this function overrides the behavior of
ServerSocket.bind(...).
Implementation
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,
);
}
setCurrentDirectory()#
Sets the current working directory to be path.
When this override is installed, this function overrides the behavior of
the setter Directory.current.
Implementation
void setCurrentDirectory(String path) {
_Directory.current = path;
}
socketConnect()#
Asynchronously returns a Socket connected to the given host and port.
When this override is installed, this function overrides the behavior of
Socket.connect(...).
Implementation
Future<Socket> socketConnect(
host,
int port, {
sourceAddress,
int sourcePort = 0,
Duration? timeout,
}) {
return Socket._connect(
host,
port,
sourceAddress: sourceAddress,
sourcePort: sourcePort,
timeout: timeout,
);
}
socketStartConnect()#
Asynchronously returns a ConnectionTask that connects to the given host and port when successful.
When this override is installed, this function overrides the behavior of
Socket.startConnect(...).
Implementation
Future<ConnectionTask<Socket>> socketStartConnect(
host,
int port, {
sourceAddress,
int sourcePort = 0,
}) {
return Socket._startConnect(
host,
port,
sourceAddress: sourceAddress,
sourcePort: sourcePort,
);
}
stat()#
Asynchronously returns FileStat
information for path.
When this override is installed, this function overrides the behavior of
FileStat.stat().
Implementation
Future<FileStat> stat(String path) {
return FileStat._stat(path);
}
statSync()#
Returns FileStat information for
path.
When this override is installed, this function overrides the behavior of
FileStat.statSync().
Implementation
FileStat statSync(String path) {
return FileStat._statSyncInternal(path);
}
toString() inherited#
A string representation of this object.
Some classes have a default textual representation,
often paired with a static parse function (like int.parse).
These classes will provide the textual representation as
their string representation.
Other classes have no meaningful textual representation
that a program will care about.
Such classes will typically override toString to provide
useful information when inspecting the object,
mainly for debugging or logging.
Inherited from Object.
Implementation
external String toString();
Operators#
operator ==() inherited#
The equality operator.
The default behavior for all Objects is to return true if and
only if this object and other are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
Total: It must return a boolean for all arguments. It should never throw.
Reflexive: For all objects
o,o == omust be true.-
Symmetric: For all objects
o1ando2,o1 == o2ando2 == o1must either both be true, or both be false. -
Transitive: For all objects
o1,o2, ando3, ifo1 == o2ando2 == o3are true, theno1 == o3must be true.
The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.
If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.
Inherited from Object.
Implementation
external bool operator ==(Object other);
Static Properties#
current no setter#
Implementation
static IOOverrides? get current {
return Zone.current[_ioOverridesToken] ?? _global;
}
global no getter#
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 also do not set IOOverrides.
Implementation
static set global(IOOverrides? overrides) {
_global = overrides;
}
Static Methods#
runWithIOOverrides()#
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.
Implementation
static R runWithIOOverrides<R>(R body(), IOOverrides overrides) {
return dart_async.runZoned<R>(
body,
zoneValues: {_ioOverridesToken: overrides},
);
}
runZoned()#
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.
Implementation
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,
int sourcePort,
Duration? timeout,
})?
socketConnect,
Future<ConnectionTask<Socket>> Function(
dynamic,
int, {
dynamic sourceAddress,
int sourcePort,
})?
socketStartConnect,
// ServerSocket
Future<ServerSocket> Function(
dynamic,
int, {
int backlog,
bool v6Only,
bool shared,
})?
serverSocketBind,
// Standard Streams
Stdin Function()? stdin,
Stdout Function()? stdout,
Stdout Function()? stderr,
// Process exit
@Since("3.10") Never Function(int)? exit,
}) {
// Avoid building chains of override scopes. Just copy outer scope's
// functions and `_previous`.
var current = IOOverrides.current;
_IOOverridesScope? currentScope;
if (current is _IOOverridesScope) {
currentScope = current;
current = currentScope._previous;
}
IOOverrides overrides = _IOOverridesScope(
current,
// Directory
createDirectory ?? currentScope?._createDirectory,
getCurrentDirectory ?? currentScope?._getCurrentDirectory,
setCurrentDirectory ?? currentScope?._setCurrentDirectory,
getSystemTempDirectory ?? currentScope?._getSystemTempDirectory,
// File
createFile ?? currentScope?._createFile,
// FileStat
stat ?? currentScope?._stat,
statSync ?? currentScope?._statSync,
// FileSystemEntity
fseIdentical ?? currentScope?._fseIdentical,
fseIdenticalSync ?? currentScope?._fseIdenticalSync,
fseGetType ?? currentScope?._fseGetType,
fseGetTypeSync ?? currentScope?._fseGetTypeSync,
// _FileSystemWatcher
fsWatch ?? currentScope?._fsWatch,
fsWatchIsSupported ?? currentScope?._fsWatchIsSupported,
// Link
createLink ?? currentScope?._createLink,
// Socket
socketConnect ?? currentScope?._socketConnect,
socketStartConnect ?? currentScope?._socketStartConnect,
// ServerSocket
serverSocketBind ?? currentScope?._serverSocketBind,
// Standard streams
stdin ?? currentScope?._stdin,
stdout ?? currentScope?._stdout,
stderr ?? currentScope?._stderr,
// Process exit
exit ?? currentScope?._exit,
);
return dart_async.runZoned<R>(
body,
zoneValues: {_ioOverridesToken: overrides},
);
}