Appearance
IOOverrides abstract base
abstract base class IOOverridesFacilities 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:
dart
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()
IOOverrides()Properties
hashCode no setter inherited
int get hashCodeThe 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
dart
external int get hashCode;runtimeType no setter inherited
Type get runtimeTypeA representation of the runtime type of the object.
Inherited from Object.
Implementation
dart
external Type get runtimeType;stderr no setter
Stdout get stderrThe 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
dart
Stdout get stderr {
return _stderr;
}stdin no setter
Stdin get stdinThe 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
dart
Stdin get stdin {
return _stdin;
}stdout no setter
Stdout get stdoutThe 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
dart
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
dart
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
dart
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
dart
Link createLink(String path) => _Link(path);exit()
Never exit(int code)Exit the Dart VM process immediately with the given exit code.
When this override is installed, this function overrides the behavior of exit(...).
Implementation
dart
@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()
Future<FileSystemEntityType> fseGetType(String path, bool followLinks)Asynchronously returns the FileSystemEntityType for path.
When this override is installed, this function overrides the behavior of FileSystemEntity.type.
Implementation
dart
Future<FileSystemEntityType> fseGetType(String path, bool followLinks) {
return FileSystemEntity._getTypeRequest(utf8.encode(path), followLinks);
}fseGetTypeSync()
FileSystemEntityType fseGetTypeSync(String path, bool followLinks)Returns the FileSystemEntityType for path.
When this override is installed, this function overrides the behavior of FileSystemEntity.typeSync.
Implementation
dart
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
dart
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
dart
bool fseIdenticalSync(String path1, String path2) {
return FileSystemEntity._identicalSync(path1, path2);
}fsWatch()
Stream<FileSystemEvent> fsWatch(String path, int events, bool recursive)Returns a Stream of FileSystemEvents.
When this override is installed, this function overrides the behavior of FileSystemEntity.watch().
Implementation
dart
Stream<FileSystemEvent> fsWatch(String path, int events, bool recursive) {
return _FileSystemWatcher._watch(path, events, recursive);
}fsWatchIsSupported()
bool fsWatchIsSupported()Returns true when FileSystemEntity.watch is supported.
When this override is installed, this function overrides the behavior of FileSystemEntity.isWatchSupported.
Implementation
dart
bool fsWatchIsSupported() => _FileSystemWatcher.isSupported;getCurrentDirectory()
Directory getCurrentDirectory()Returns the current working directory.
When this override is installed, this function overrides the behavior of the static getter Directory.current
Implementation
dart
Directory getCurrentDirectory() => _Directory.current;getSystemTempDirectory()
Directory getSystemTempDirectory()Returns the system temporary directory.
When this override is installed, this function overrides the behavior of Directory.systemTemp.
Implementation
dart
Directory getSystemTempDirectory() => _Directory.systemTemp;noSuchMethod() inherited
dynamic noSuchMethod(Invocation invocation)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:
dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis 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:
dart
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
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);serverSocketBind()
Future<ServerSocket> serverSocketBind(
dynamic address,
int port, {
int backlog = 0,
bool v6Only = false,
bool shared = false,
})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
dart
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()
void setCurrentDirectory(String path)Sets the current working directory to be path.
When this override is installed, this function overrides the behavior of the setter Directory.current.
Implementation
dart
void setCurrentDirectory(String path) {
_Directory.current = path;
}socketConnect()
Future<Socket> socketConnect(
dynamic host,
int port, {
dynamic sourceAddress,
int sourcePort = 0,
Duration? timeout,
})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
dart
Future<Socket> socketConnect(
host,
int port, {
sourceAddress,
int sourcePort = 0,
Duration? timeout,
}) {
return Socket._connect(
host,
port,
sourceAddress: sourceAddress,
sourcePort: sourcePort,
timeout: timeout,
);
}socketStartConnect()
Future<ConnectionTask<Socket>> socketStartConnect(
dynamic host,
int port, {
dynamic sourceAddress,
int sourcePort = 0,
})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
dart
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
dart
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
dart
FileStat statSync(String path) {
return FileStat._statSyncInternal(path);
}toString() inherited
String toString()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
dart
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
dart
external bool operator ==(Object other);Static Properties
current no setter
IOOverrides? get currentImplementation
dart
static IOOverrides? get current {
return Zone.current[_ioOverridesToken] ?? _global;
}global no getter
set global(IOOverrides? value)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
dart
static set global(IOOverrides? overrides) {
_global = overrides;
}Static Methods
runWithIOOverrides()
R runWithIOOverrides<R>(R Function() body, IOOverrides overrides)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
dart
static R runWithIOOverrides<R>(R body(), IOOverrides overrides) {
return dart_async.runZoned<R>(
body,
zoneValues: {_ioOverridesToken: overrides},
);
}runZoned()
R runZoned<R>(
R Function() body, {
(Directory Function(String))? createDirectory,
(Directory Function())? getCurrentDirectory,
(void Function(String))? setCurrentDirectory,
(Directory Function())? getSystemTempDirectory,
(File Function(String))? createFile,
(Future<FileStat> Function(String))? stat,
(FileStat Function(String))? statSync,
(Future<bool> Function(String, String))? fseIdentical,
(bool Function(String, String))? fseIdenticalSync,
(Future<FileSystemEntityType> Function(String, bool))? fseGetType,
(FileSystemEntityType Function(String, bool))? fseGetTypeSync,
(Stream<FileSystemEvent> Function(String, int, bool))? fsWatch,
(bool Function())? fsWatchIsSupported,
(Link Function(String))? createLink,
(Future<Socket> Function(dynamic, int, {dynamic sourceAddress, int sourcePort, Duration? timeout}))? socketConnect,
(Future<ConnectionTask<Socket>> Function(dynamic, int, {dynamic sourceAddress, int sourcePort}))? socketStartConnect,
(Future<ServerSocket> Function(dynamic, int, {int backlog, bool shared, bool v6Only}))? serverSocketBind,
(Stdin Function())? stdin,
(Stdout Function())? stdout,
(Stdout Function())? stderr,
(Never Function(int))? exit,
})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
dart
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},
);
}