From 14437390fc7e217d5dd6e8a0d56bc8d2fce19352 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Wed, 2 Jul 2025 11:54:00 -0700 Subject: [PATCH] Switch to using package exceptions instead of dart:io exceptions. --- pkgs/io_file/constants.json | 4 + pkgs/io_file/lib/io_file.dart | 1 + pkgs/io_file/lib/src/constant_bindings.g.dart | 12 ++ pkgs/io_file/lib/src/constants.g.dart | 36 ++++ pkgs/io_file/lib/src/exceptions.dart | 73 +++---- .../io_file/lib/src/vm_posix_file_system.dart | 142 +++++++++---- .../lib/src/vm_windows_file_system.dart | 192 +++++++++++++----- pkgs/io_file/src/constants.g.c | 28 +++ pkgs/io_file/src/constants.g.h | 8 + pkgs/io_file/test/create_directory_test.dart | 79 +++---- .../test/create_temporary_directory_test.dart | 72 +++---- pkgs/io_file/test/current_directory_test.dart | 18 +- pkgs/io_file/test/exceptions_test.dart | 31 ++- pkgs/io_file/test/metadata_test.dart | 50 +++-- pkgs/io_file/test/read_as_bytes_test.dart | 51 +++-- pkgs/io_file/test/remove_directory_test.dart | 103 +++++----- .../test/remove_directory_tree_test.dart | 149 ++++++++------ pkgs/io_file/test/rename_test.dart | 66 +++--- pkgs/io_file/test/same_test.dart | 120 ++++++----- pkgs/io_file/test/write_as_bytes_test.dart | 65 +++--- pkgs/io_file/test/write_as_string_test.dart | 87 ++++---- 21 files changed, 818 insertions(+), 569 deletions(-) diff --git a/pkgs/io_file/constants.json b/pkgs/io_file/constants.json index 47de19f0..ca63abde 100644 --- a/pkgs/io_file/constants.json +++ b/pkgs/io_file/constants.json @@ -38,7 +38,11 @@ "EACCES", "EEXIST", "EINTR", + "EMFILE", "ENOENT", + "ENOSPC", + "ENOTDIR", + "ENOTEMPTY", "EPERM" ], "": [ diff --git a/pkgs/io_file/lib/io_file.dart b/pkgs/io_file/lib/io_file.dart index 39028cc0..88636fca 100644 --- a/pkgs/io_file/lib/io_file.dart +++ b/pkgs/io_file/lib/io_file.dart @@ -2,6 +2,7 @@ // 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. +export 'src/exceptions.dart'; export 'src/file_system.dart'; export 'src/vm_file_system_property.dart' if (dart.library.html) 'src/web_file_system_property.dart'; diff --git a/pkgs/io_file/lib/src/constant_bindings.g.dart b/pkgs/io_file/lib/src/constant_bindings.g.dart index 935b29f4..ea236814 100644 --- a/pkgs/io_file/lib/src/constant_bindings.g.dart +++ b/pkgs/io_file/lib/src/constant_bindings.g.dart @@ -40,9 +40,21 @@ external int get_EEXIST(); @ffi.Native(symbol: 'libc_shim_get_EINTR') external int get_EINTR(); +@ffi.Native(symbol: 'libc_shim_get_EMFILE') +external int get_EMFILE(); + @ffi.Native(symbol: 'libc_shim_get_ENOENT') external int get_ENOENT(); +@ffi.Native(symbol: 'libc_shim_get_ENOSPC') +external int get_ENOSPC(); + +@ffi.Native(symbol: 'libc_shim_get_ENOTDIR') +external int get_ENOTDIR(); + +@ffi.Native(symbol: 'libc_shim_get_ENOTEMPTY') +external int get_ENOTEMPTY(); + @ffi.Native(symbol: 'libc_shim_get_EPERM') external int get_EPERM(); diff --git a/pkgs/io_file/lib/src/constants.g.dart b/pkgs/io_file/lib/src/constants.g.dart index 4ebc46e6..3196b33c 100644 --- a/pkgs/io_file/lib/src/constants.g.dart +++ b/pkgs/io_file/lib/src/constants.g.dart @@ -104,6 +104,15 @@ int get EINTR { } } +int get EMFILE { + final v = get_EMFILE(); + if (v == libc_shim_UNDEFINED) { + throw UnsupportedError('EMFILE'); + } else { + return v; + } +} + int get ENOENT { final v = get_ENOENT(); if (v == libc_shim_UNDEFINED) { @@ -113,6 +122,33 @@ int get ENOENT { } } +int get ENOSPC { + final v = get_ENOSPC(); + if (v == libc_shim_UNDEFINED) { + throw UnsupportedError('ENOSPC'); + } else { + return v; + } +} + +int get ENOTDIR { + final v = get_ENOTDIR(); + if (v == libc_shim_UNDEFINED) { + throw UnsupportedError('ENOTDIR'); + } else { + return v; + } +} + +int get ENOTEMPTY { + final v = get_ENOTEMPTY(); + if (v == libc_shim_UNDEFINED) { + throw UnsupportedError('ENOTEMPTY'); + } else { + return v; + } +} + int get EPERM { final v = get_EPERM(); if (v == libc_shim_UNDEFINED) { diff --git a/pkgs/io_file/lib/src/exceptions.dart b/pkgs/io_file/lib/src/exceptions.dart index 2be184ce..e0ffb2ef 100644 --- a/pkgs/io_file/lib/src/exceptions.dart +++ b/pkgs/io_file/lib/src/exceptions.dart @@ -12,28 +12,16 @@ import 'file_system.dart'; // There is no exception corresponding to the POSIX `EISDIR` error code because // there is no corresponding Windows error code. -/// An error related to call to the operating system or an intermediary library, -/// such as libc. -class SystemCallError { - /// The name of the system call, such as `open` or `CreateFile`. - final String systemCall; - - /// The operating-system defined code for the error, such as 2 for - /// `ERROR_FILE_NOT_FOUND` on Windows. - final int errorCode; - - /// The operating-system description of the error, such as - /// "The system cannot find the file specified." - final String message; - - const SystemCallError(this.systemCall, this.errorCode, this.message); -} - /// Exception thrown when a file operation fails. class IOFileException implements Exception { - // A description of the failed operation. + // A description of the failed operation, such as + //`'No such file or directory'`. final String message; + /// The operating-system defined code for the error, such as 2 for + /// `ERROR_FILE_NOT_FOUND` on Windows. + final int? errorCode; + /// A path provided in a failed file operation. /// /// For file operations that involve a single path @@ -60,28 +48,37 @@ class IOFileException implements Exception { /// The underlying system call that failed. /// /// Can be `null` if the exception is not raised due to a failed system call. - final SystemCallError? systemCall; + final String? systemCall; const IOFileException( this.message, { this.path1, this.path2, this.systemCall, + this.errorCode, }); String _toStringHelper(String className) { final sb = StringBuffer('$className: $message'); + // IOFileException: No such file or directory if (path1 != null) { - sb.write(', path1="$path1"'); + // IOFileException: No such file or directory: "a" + sb.write(': "$path1"'); } if (path2 != null) { - sb.write(', path2="$path2"'); - } - if (systemCall case final call?) { - sb.write( - ' (${call.systemCall}: ${call.message}, errorCode=${call.errorCode})', - ); + // IOFileException: No such file or directory: "a" -> "b" + sb.write(' -> "$path2"'); } + sb.write(switch ((systemCall, errorCode)) { + // ... or directory: "a" -> "b" + (null, null) => '', + // ... or directory: "a" -> "b" [errorCode: 2] + (null, final error?) => ' [errorCode: $error]', + // ... or directory: "a" -> "b" [renameat failed] + (final call?, null) => ' [$call failed]', + // ... or directory: "a" -> "b" [renameat failed with errorCode: 2] + (final error?, final call?) => ' [$error failed with errorCode: $call]', + }); return sb.toString(); } @@ -101,6 +98,7 @@ class DirectoryNotEmptyException extends IOFileException { super.path1, super.path2, super.systemCall, + super.errorCode, }); @override @@ -119,29 +117,13 @@ class DiskFullException extends IOFileException { super.path1, super.path2, super.systemCall, + super.errorCode, }); @override String toString() => _toStringHelper('DiskFullException'); } -/// Exception thrown when a file operation (such as -/// `FileSystem.remove`) is requested on directory. -/// -/// This exception corresponds to errors such as `EISDIR` on POSIX systems and -/// `ERROR_DIRECTORY` on Windows. -class IsADirectoryException extends IOFileException { - const IsADirectoryException( - super.message, { - super.path1, - super.path2, - super.systemCall, - }); - - @override - String toString() => _toStringHelper('IsADirectoryException'); -} - /// Exception thrown when a directory operation (such as /// [FileSystem.removeDirectory]) is requested on a non-directory. /// @@ -153,6 +135,7 @@ class NotADirectoryException extends IOFileException { super.path1, super.path2, super.systemCall, + super.errorCode, }); @override @@ -170,6 +153,7 @@ class PathAccessException extends IOFileException { super.path1, super.path2, super.systemCall, + super.errorCode, }); @override @@ -187,6 +171,7 @@ class PathExistsException extends IOFileException { super.path1, super.path2, super.systemCall, + super.errorCode, }); @override @@ -204,6 +189,7 @@ class PathNotFoundException extends IOFileException { super.path1, super.path2, super.systemCall, + super.errorCode, }); @override @@ -220,6 +206,7 @@ class TooManyOpenFilesException extends IOFileException { super.path1, super.path2, super.systemCall, + super.errorCode, }); @override diff --git a/pkgs/io_file/lib/src/vm_posix_file_system.dart b/pkgs/io_file/lib/src/vm_posix_file_system.dart index 2b9c8f78..0481c388 100644 --- a/pkgs/io_file/lib/src/vm_posix_file_system.dart +++ b/pkgs/io_file/lib/src/vm_posix_file_system.dart @@ -11,6 +11,7 @@ import 'dart:typed_data'; import 'package:ffi/ffi.dart' as ffi; import 'package:path/path.dart' as p; +import 'exceptions.dart'; import 'file_system.dart'; import 'internal_constants.dart'; import 'libc.dart' as libc; @@ -26,28 +27,82 @@ const _nanosecondsPerSecond = 1000000000; bool _isDotOrDotDot(Pointer s) => // ord('.') == 46 s[0] == 46 && ((s[1] == 0) || (s[1] == 46 && s[2] == 0)); -Exception _getError(int err, String message, [String? path]) { - // TODO(brianquinlan): In the long-term, do we need to avoid exceptions that - // are part of `dart:io`? Can we move those exceptions into a different - // namespace? - final osError = io.OSError( - libc.strerror(err).cast().toDartString(), - err, - ); - - if (path != null) { - if (err == libc.EPERM || err == libc.EACCES) { - return io.PathAccessException(path, osError, message); - } else if (err == libc.EEXIST) { - return io.PathExistsException(path, osError, message); - } else if (err == libc.ENOENT) { - return io.PathNotFoundException(path, osError, message); - } else { - return io.FileSystemException(message, path, osError); - } - } else { - return io.FileSystemException(message, path, osError); +Exception _getError( + int errorCode, { + required String systemCall, + String? path1, + String? path2, +}) { + final strError = libc.strerror(errorCode); + final message = + strError == nullptr + ? 'unknown error' + : strError.cast().toDartString(); + + if (errorCode == libc.ENOTEMPTY) { + return DirectoryNotEmptyException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + } else if (errorCode == libc.ENOSPC) { + return DiskFullException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + } else if (errorCode == libc.ENOTDIR) { + return NotADirectoryException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + } else if (errorCode == libc.EPERM || errorCode == libc.EACCES) { + return PathAccessException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + } else if (errorCode == libc.EEXIST) { + return PathExistsException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + } else if (errorCode == libc.ENOENT) { + return PathNotFoundException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + } else if (errorCode == libc.EMFILE) { + return TooManyOpenFilesException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); } + return IOFileException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); } /// Return the given function until the result is not `EINTR`. @@ -220,13 +275,13 @@ final class PosixFileSystem extends FileSystem { final stat1 = arena(); if (libc.stat(path1.toNativeUtf8(allocator: arena).cast(), stat1) == -1) { final errno = libc.errno; - throw _getError(errno, 'stat failed', path1); + throw _getError(errno, systemCall: 'stat', path1: path1); } final stat2 = arena(); if (libc.stat(path2.toNativeUtf8(allocator: arena).cast(), stat2) == -1) { final errno = libc.errno; - throw _getError(errno, 'stat failed', path2); + throw _getError(errno, systemCall: 'stat', path1: path2); } return (stat1.ref.st_ino == stat2.ref.st_ino) && @@ -241,7 +296,7 @@ final class PosixFileSystem extends FileSystem { ) == -1) { final errno = libc.errno; - throw _getError(errno, 'create directory failed', path); + throw _getError(errno, systemCall: 'mkdir', path1: path); } }); @@ -249,7 +304,7 @@ final class PosixFileSystem extends FileSystem { set currentDirectory(String path) => ffi.using((arena) { if (libc.chdir(path.toNativeUtf8(allocator: arena).cast()) == -1) { final errno = libc.errno; - throw _getError(errno, 'chdir failed', path); + throw _getError(errno, systemCall: 'chdir', path1: path); } }); @@ -258,7 +313,7 @@ final class PosixFileSystem extends FileSystem { final buffer = arena(libc.PATH_MAX); if (libc.getcwd(buffer, libc.PATH_MAX) == nullptr) { final errno = libc.errno; - throw _getError(errno, 'getcwd failed', null); + throw _getError(errno, systemCall: 'getcwd'); } return buffer.cast().toDartString(); }); @@ -274,7 +329,7 @@ final class PosixFileSystem extends FileSystem { ); if (path == nullptr) { final errno = libc.errno; - throw _getError(errno, 'mkdtemp failed', template); + throw _getError(errno, systemCall: 'mkdtemp', path1: template); } return path.cast().toDartString(); }); @@ -285,7 +340,7 @@ final class PosixFileSystem extends FileSystem { if (libc.lstat(path.toNativeUtf8(allocator: arena).cast(), stat) == -1) { final errno = libc.errno; - throw _getError(errno, 'stat failed', path); + throw _getError(errno, systemCall: 'lstat', path1: path); } return PosixMetadata.fromFileAttributes( @@ -313,7 +368,7 @@ final class PosixFileSystem extends FileSystem { ) == -1) { final errno = libc.errno; - throw _getError(errno, 'remove directory failed', path); + throw _getError(errno, systemCall: 'unlinkat', path1: path); } }); @@ -330,13 +385,13 @@ final class PosixFileSystem extends FileSystem { ); if (fd == -1) { final errno = libc.errno; - throw _getError(errno, 'openat failed', path); + throw _getError(errno, systemCall: 'openat', path1: path); } try { final dir = libc.fdopendir(fd); if (dir == nullptr) { final errno = libc.errno; - throw _getError(errno, 'fdopendir failed', path); + throw _getError(errno, systemCall: 'fdopendir', path1: path); } try { // `readdir` returns `NULL` but leaves `errno` unchanged if the end of @@ -366,7 +421,7 @@ final class PosixFileSystem extends FileSystem { /// DT_UNKNOWN. if (libc.fstatat(fd, child, stat, libc.AT_SYMLINK_NOFOLLOW) == -1) { final errno = libc.errno; - throw _getError(errno, 'fstatat failed', childPath); + throw _getError(errno, systemCall: 'fstatat', path1: childPath); } type = stat.ref.st_mode & libc.S_IFMT == libc.S_IFDIR @@ -378,7 +433,7 @@ final class PosixFileSystem extends FileSystem { } else { if (libc.unlinkat(fd, child, 0) == -1) { final errno = libc.errno; - throw _getError(errno, 'unlinkat failed', childPath); + throw _getError(errno, systemCall: 'unlinkat', path1: childPath); } } libc.errno = 0; @@ -386,11 +441,11 @@ final class PosixFileSystem extends FileSystem { } if (libc.errno != 0) { final errno = libc.errno; - throw _getError(errno, 'readdir failed', path); + throw _getError(errno, systemCall: 'readdir', path1: path); } if (libc.unlinkat(parentfd, name.cast(), libc.AT_REMOVEDIR) == -1) { final errno = libc.errno; - throw _getError(errno, 'unlinkat failed', path); + throw _getError(errno, systemCall: 'unlinkat', path1: path); } } finally { libc.closedir(dir); @@ -418,7 +473,12 @@ final class PosixFileSystem extends FileSystem { ) != 0) { final errno = libc.errno; - throw _getError(errno, 'rename failed', oldPath); + throw _getError( + errno, + systemCall: 'rename', + path1: oldPath, + path2: newPath, + ); } }); @@ -433,7 +493,7 @@ final class PosixFileSystem extends FileSystem { ); if (fd == -1) { final errno = libc.errno; - throw _getError(errno, 'open failed', path); + throw _getError(errno, systemCall: 'open', path1: path); } try { final stat = arena(); @@ -461,7 +521,7 @@ final class PosixFileSystem extends FileSystem { switch (r) { case -1: final errno = libc.errno; - throw _getError(errno, 'read failed', path); + throw _getError(errno, systemCall: 'read', path1: path); case 0: return builder.takeBytes(); default: @@ -489,7 +549,7 @@ final class PosixFileSystem extends FileSystem { switch (r) { case -1: final errno = libc.errno; - throw _getError(errno, 'read failed', path); + throw _getError(errno, systemCall: 'read', path1: path); case 0: return buffer.asTypedList( bufferOffset, @@ -546,7 +606,7 @@ final class PosixFileSystem extends FileSystem { try { if (fd == -1) { final errno = libc.errno; - throw _getError(errno, 'open failed', path); + throw _getError(errno, systemCall: 'open', path1: path); } ffi.using((arena) { @@ -563,7 +623,7 @@ final class PosixFileSystem extends FileSystem { ); if (w == -1) { final errno = libc.errno; - throw _getError(errno, 'write failed', path); + throw _getError(errno, systemCall: 'write', path1: path); } remaining -= w; buffer += w; diff --git a/pkgs/io_file/lib/src/vm_windows_file_system.dart b/pkgs/io_file/lib/src/vm_windows_file_system.dart index 0fdb8ed6..5b23e967 100644 --- a/pkgs/io_file/lib/src/vm_windows_file_system.dart +++ b/pkgs/io_file/lib/src/vm_windows_file_system.dart @@ -4,7 +4,6 @@ import 'dart:convert'; import 'dart:ffi'; -import 'dart:io' as io; import 'dart:math'; import 'dart:typed_data'; @@ -13,6 +12,7 @@ import 'package:ffi/ffi.dart' as ffi; import 'package:path/path.dart' as p; import 'package:win32/win32.dart' as win32; +import 'exceptions.dart'; import 'file_system.dart'; import 'internal_constants.dart'; @@ -88,7 +88,7 @@ Pointer _extendedPath(String path, Allocator allocator) { final result = win32.GetFullPathName(utf16Path, length, buffer, nullptr); if (result == 0) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'GetFullPathName failed', path); + throw _getError(errorCode, systemCall: 'GetFullPathName', path1: path); } if (result < length) { final canonicalPath = allocator>(); @@ -130,37 +130,94 @@ String _formatMessage(int errorCode) { } } -Exception _getError(int errorCode, String message, [String? path]) { - final osError = io.OSError(_formatMessage(errorCode), errorCode); - - if (path != null) { - switch (errorCode) { - case win32.ERROR_ACCESS_DENIED: - case win32.ERROR_CURRENT_DIRECTORY: - case win32.ERROR_WRITE_PROTECT: - case win32.ERROR_BAD_LENGTH: - case win32.ERROR_SHARING_VIOLATION: - case win32.ERROR_LOCK_VIOLATION: - case win32.ERROR_NETWORK_ACCESS_DENIED: - case win32.ERROR_DRIVE_LOCKED: - return io.PathAccessException(path, osError, message); - case win32.ERROR_FILE_EXISTS: - case win32.ERROR_ALREADY_EXISTS: - return io.PathExistsException(path, osError, message); - case win32.ERROR_FILE_NOT_FOUND: - case win32.ERROR_PATH_NOT_FOUND: - case win32.ERROR_INVALID_DRIVE: - case win32.ERROR_INVALID_NAME: - case win32.ERROR_NO_MORE_FILES: - case win32.ERROR_BAD_NETPATH: - case win32.ERROR_BAD_NET_NAME: - case win32.ERROR_BAD_PATHNAME: - return io.PathNotFoundException(path, osError, message); - default: - return io.FileSystemException(message, path, osError); - } - } else { - return io.FileSystemException(message, path, osError); +Exception _getError( + int errorCode, { + required String systemCall, + String? path1, + String? path2, +}) { + final message = _formatMessage(errorCode); + + switch (errorCode) { + case win32.ERROR_DIR_NOT_EMPTY: + return DirectoryNotEmptyException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + case win32.ERROR_DISK_FULL: + return DiskFullException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + case win32.ERROR_DIRECTORY: + return NotADirectoryException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + case win32.ERROR_ACCESS_DENIED: + case win32.ERROR_CURRENT_DIRECTORY: + case win32.ERROR_WRITE_PROTECT: + case win32.ERROR_BAD_LENGTH: + case win32.ERROR_SHARING_VIOLATION: + case win32.ERROR_LOCK_VIOLATION: + case win32.ERROR_NETWORK_ACCESS_DENIED: + case win32.ERROR_DRIVE_LOCKED: + return PathAccessException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + case win32.ERROR_FILE_EXISTS: + case win32.ERROR_ALREADY_EXISTS: + return PathExistsException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + case win32.ERROR_FILE_NOT_FOUND: + case win32.ERROR_PATH_NOT_FOUND: + case win32.ERROR_INVALID_DRIVE: + case win32.ERROR_INVALID_NAME: + case win32.ERROR_NO_MORE_FILES: + case win32.ERROR_BAD_NETPATH: + case win32.ERROR_BAD_NET_NAME: + case win32.ERROR_BAD_PATHNAME: + return PathNotFoundException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + case win32.ERROR_TOO_MANY_OPEN_FILES: + return TooManyOpenFilesException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); + default: + return IOFileException( + message, + errorCode: errorCode, + path1: path1, + path2: path2, + systemCall: systemCall, + ); } } @@ -365,13 +422,17 @@ final class WindowsFileSystem extends FileSystem { ); if (h == win32.INVALID_HANDLE_VALUE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'CreateFile failed', path); + throw _getError(errorCode, systemCall: 'CreateFile', path1: path); } try { final info = arena(); if (win32.GetFileInformationByHandle(h, info) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'GetFileInformationByHandle failed', path); + throw _getError( + errorCode, + systemCall: 'GetFileInformationByHandle', + path1: path, + ); } return info.ref; } finally { @@ -386,7 +447,7 @@ final class WindowsFileSystem extends FileSystem { if (win32.CreateDirectory(_extendedPath(path, arena), nullptr) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'create directory failed', path); + throw _getError(errorCode, systemCall: 'CreateDirectory', path1: path); } }); @@ -407,7 +468,11 @@ final class WindowsFileSystem extends FileSystem { if (win32.SetCurrentDirectory(path.toNativeUtf16()) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'SetCurrentDirectory failed', path); + throw _getError( + errorCode, + systemCall: 'SetCurrentDirectory', + path1: path, + ); } }); @@ -437,7 +502,7 @@ final class WindowsFileSystem extends FileSystem { if (win32.RemoveDirectory(_extendedPath(path, arena)) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'remove directory failed', path); + throw _getError(errorCode, systemCall: 'RemoveDirectory', path1: path); } }); @@ -455,7 +520,7 @@ final class WindowsFileSystem extends FileSystem { if (findHandle == win32.INVALID_HANDLE_VALUE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'FindFirstFile failed', path); + throw _getError(errorCode, systemCall: 'FindFirstFile', path1: path); } do { @@ -475,15 +540,19 @@ final class WindowsFileSystem extends FileSystem { final errorCode = win32.GetLastError(); throw _getError( errorCode, - 'RemoveDirectory failed for link', - fullPath, + systemCall: 'RemoveDirectory', + path1: fullPath, ); } } else { if (win32.DeleteFile(fullPath.toNativeUtf16(allocator: arena)) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'DeleteFile failed for link', fullPath); + throw _getError( + errorCode, + systemCall: 'DeleteFile', + path1: fullPath, + ); } } } else if ((attributes & win32.FILE_ATTRIBUTE_DIRECTORY) != 0) { @@ -492,20 +561,20 @@ final class WindowsFileSystem extends FileSystem { if (win32.DeleteFile(fullPath.toNativeUtf16(allocator: arena)) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'DeleteFile failed', fullPath); + throw _getError(errorCode, systemCall: 'DeleteFile', path1: fullPath); } } } while (win32.FindNextFile(findHandle, findData) != win32.FALSE); final errorCode = win32.GetLastError(); if (errorCode != win32.ERROR_NO_MORE_FILES) { - throw _getError(errorCode, 'FindNextFile failed', path); + throw _getError(errorCode, systemCall: 'FindNextFile', path1: path); } if (win32.RemoveDirectory(path.toNativeUtf16(allocator: arena)) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'remove directory failed', path); + throw _getError(errorCode, systemCall: 'RemoveDirectory', path1: path); } }); @@ -520,7 +589,12 @@ final class WindowsFileSystem extends FileSystem { ) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'rename failed', oldPath); + throw _getError( + errorCode, + systemCall: 'MoveFileEx', + path1: oldPath, + path2: newPath, + ); } }); @@ -565,7 +639,11 @@ final class WindowsFileSystem extends FileSystem { ) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'set metadata failed', path); + throw _getError( + errorCode, + systemCall: 'GetFileAttributesEx', + path1: path, + ); } attributes = fileInfo.ref.dwFileAttributes; } else { @@ -614,7 +692,7 @@ final class WindowsFileSystem extends FileSystem { } if (win32.SetFileAttributes(nativePath, attributes) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'set metadata failed', path); + throw _getError(errorCode, systemCall: 'SetFileAttributes', path1: path); } }); @@ -631,7 +709,11 @@ final class WindowsFileSystem extends FileSystem { ) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'metadata failed', path); + throw _getError( + errorCode, + systemCall: 'GetFileAttributesEx', + path1: path, + ); } final info = fileInfo.ref; final attributes = info.dwFileAttributes; @@ -688,7 +770,7 @@ final class WindowsFileSystem extends FileSystem { ); if (f == win32.INVALID_HANDLE_VALUE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'open failed', path); + throw _getError(errorCode, systemCall: 'CreateFile', path1: path); } try { // The result of `GetFileSize` is not defined for non-seeking devices @@ -732,7 +814,7 @@ final class WindowsFileSystem extends FileSystem { errorCode == win32.ERROR_SUCCESS) { return builder.takeBytes(); } - throw _getError(errorCode, 'read failed', path); + throw _getError(errorCode, systemCall: 'ReadFile', path1: path); } if (bytesRead.value == 0) { @@ -763,7 +845,7 @@ final class WindowsFileSystem extends FileSystem { ) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'read failed', path); + throw _getError(errorCode, systemCall: 'ReadFile', path1: path); } bufferOffset += bytesRead.value; if (bytesRead.value == 0) { @@ -789,7 +871,7 @@ final class WindowsFileSystem extends FileSystem { final length = win32.GetTempPath2(maxLength, buffer); if (length == 0) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'GetTempPath failed'); + throw _getError(errorCode, systemCall: 'GetTempPath2'); } return p.canonicalize(buffer.toDartString()); } finally { @@ -826,7 +908,7 @@ final class WindowsFileSystem extends FileSystem { ); if (f == win32.INVALID_HANDLE_VALUE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'open failed', path); + throw _getError(errorCode, systemCall: 'CreateFile', path1: path); } try { @@ -847,7 +929,7 @@ final class WindowsFileSystem extends FileSystem { ) == win32.FALSE) { final errorCode = win32.GetLastError(); - throw _getError(errorCode, 'write failed', path); + throw _getError(errorCode, systemCall: 'WriteFile', path1: path); } remaining -= bytesWritten.value; diff --git a/pkgs/io_file/src/constants.g.c b/pkgs/io_file/src/constants.g.c index 1108bb4c..a5f7aa53 100644 --- a/pkgs/io_file/src/constants.g.c +++ b/pkgs/io_file/src/constants.g.c @@ -87,6 +87,13 @@ int64_t libc_shim_get_EINTR(void) { #endif return libc_shim_UNDEFINED; } +int64_t libc_shim_get_EMFILE(void) { +#ifdef EMFILE + assert(EMFILE != libc_shim_UNDEFINED); + return EMFILE; +#endif + return libc_shim_UNDEFINED; +} int64_t libc_shim_get_ENOENT(void) { #ifdef ENOENT assert(ENOENT != libc_shim_UNDEFINED); @@ -94,6 +101,27 @@ int64_t libc_shim_get_ENOENT(void) { #endif return libc_shim_UNDEFINED; } +int64_t libc_shim_get_ENOSPC(void) { +#ifdef ENOSPC + assert(ENOSPC != libc_shim_UNDEFINED); + return ENOSPC; +#endif + return libc_shim_UNDEFINED; +} +int64_t libc_shim_get_ENOTDIR(void) { +#ifdef ENOTDIR + assert(ENOTDIR != libc_shim_UNDEFINED); + return ENOTDIR; +#endif + return libc_shim_UNDEFINED; +} +int64_t libc_shim_get_ENOTEMPTY(void) { +#ifdef ENOTEMPTY + assert(ENOTEMPTY != libc_shim_UNDEFINED); + return ENOTEMPTY; +#endif + return libc_shim_UNDEFINED; +} int64_t libc_shim_get_EPERM(void) { #ifdef EPERM assert(EPERM != libc_shim_UNDEFINED); diff --git a/pkgs/io_file/src/constants.g.h b/pkgs/io_file/src/constants.g.h index cb135b22..e27e9ddb 100644 --- a/pkgs/io_file/src/constants.g.h +++ b/pkgs/io_file/src/constants.g.h @@ -32,8 +32,16 @@ int64_t libc_shim_get_EEXIST(void); __attribute__((visibility("default"))) __attribute__((used)) int64_t libc_shim_get_EINTR(void); __attribute__((visibility("default"))) __attribute__((used)) +int64_t libc_shim_get_EMFILE(void); +__attribute__((visibility("default"))) __attribute__((used)) int64_t libc_shim_get_ENOENT(void); __attribute__((visibility("default"))) __attribute__((used)) +int64_t libc_shim_get_ENOSPC(void); +__attribute__((visibility("default"))) __attribute__((used)) +int64_t libc_shim_get_ENOTDIR(void); +__attribute__((visibility("default"))) __attribute__((used)) +int64_t libc_shim_get_ENOTEMPTY(void); +__attribute__((visibility("default"))) __attribute__((used)) int64_t libc_shim_get_EPERM(void); __attribute__((visibility("default"))) __attribute__((used)) int64_t libc_shim_get_AT_FDCWD(void); diff --git a/pkgs/io_file/test/create_directory_test.dart b/pkgs/io_file/test/create_directory_test.dart index a6afc35c..44c5ad7e 100644 --- a/pkgs/io_file/test/create_directory_test.dart +++ b/pkgs/io_file/test/create_directory_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:io_file/io_file.dart'; import 'package:path/path.dart' as p; @@ -35,7 +35,7 @@ void main() { final path = '$tmp/dir'; fileSystem.createDirectory(path); - expect(FileSystemEntity.isDirectorySync(path), isTrue); + expect(io.FileSystemEntity.isDirectorySync(path), isTrue); }); test('absolute path, long directory name', () { @@ -43,11 +43,11 @@ void main() { // When using an API to create a directory, the specified path cannot be // so long that you cannot append an 8.3 file name (that is, the directory // name cannot exceed MAX_PATH minus 12). - final dirname = 'd' * (Platform.isWindows ? win32.MAX_PATH - 12 : 255); + final dirname = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); final path = p.join(tmp, dirname); fileSystem.createDirectory(path); - expect(FileSystemEntity.isDirectorySync(path), isTrue); + expect(io.FileSystemEntity.isDirectorySync(path), isTrue); }); test('absolute path, too long directory name', () { @@ -56,13 +56,12 @@ void main() { expect( () => fileSystem.createDirectory(path), throwsA( - isA() - .having((e) => e.message, 'message', 'create directory failed') - .having((e) => e.path, 'path', path) + isA() + .having((e) => e.path1, 'path1', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows + io.Platform.isWindows ? win32.ERROR_INVALID_NAME : errors.enametoolong, ), @@ -75,10 +74,10 @@ void main() { // When using an API to create a directory, the specified path cannot be // so long that you cannot append an 8.3 file name (that is, the directory // name cannot exceed MAX_PATH minus 12). - final path = 'd' * (Platform.isWindows ? win32.MAX_PATH - 12 : 255); + final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); fileSystem.createDirectory(path); - expect(FileSystemEntity.isDirectorySync(path), isTrue); + expect(io.FileSystemEntity.isDirectorySync(path), isTrue); }); test('relative path, too long directory name', () { @@ -87,13 +86,12 @@ void main() { expect( () => fileSystem.createDirectory(path), throwsA( - isA() - .having((e) => e.message, 'message', 'create directory failed') - .having((e) => e.path, 'path', path) + isA() + .having((e) => e.path1, 'path1', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows + io.Platform.isWindows ? win32.ERROR_INVALID_NAME : errors.enametoolong, ), @@ -108,12 +106,13 @@ void main() { () => fileSystem.createDirectory(path), throwsA( isA() - .having((e) => e.message, 'message', 'create directory failed') - .having((e) => e.path, 'path', path) + .having((e) => e.path1, 'path', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_PATH_NOT_FOUND + : errors.enoent, ), ), ); @@ -121,18 +120,19 @@ void main() { test('create over existing directory', () { final path = '$tmp/dir'; - Directory(path).createSync(); + io.Directory(path).createSync(); expect( () => fileSystem.createDirectory(path), throwsA( isA() - .having((e) => e.message, 'message', 'create directory failed') - .having((e) => e.path, 'path', path) + .having((e) => e.path1, 'path1', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_ALREADY_EXISTS : errors.eexist, + io.Platform.isWindows + ? win32.ERROR_ALREADY_EXISTS + : errors.eexist, ), ), ); @@ -144,12 +144,13 @@ void main() { () => fileSystem.createDirectory(path), throwsA( isA() - .having((e) => e.message, 'message', 'create directory failed') - .having((e) => e.path, 'path', path) + .having((e) => e.path1, 'path1', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_ALREADY_EXISTS : errors.eexist, + io.Platform.isWindows + ? win32.ERROR_ALREADY_EXISTS + : errors.eexist, ), ), ); @@ -161,12 +162,13 @@ void main() { () => fileSystem.createDirectory(path), throwsA( isA() - .having((e) => e.message, 'message', 'create directory failed') - .having((e) => e.path, 'path', path) + .having((e) => e.path1, 'path1', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_ALREADY_EXISTS : errors.eexist, + io.Platform.isWindows + ? win32.ERROR_ALREADY_EXISTS + : errors.eexist, ), ), ); @@ -174,18 +176,19 @@ void main() { test('create over existing file', () { final path = '$tmp/file'; - File(path).createSync(); + io.File(path).createSync(); expect( () => fileSystem.createDirectory(path), throwsA( isA() - .having((e) => e.message, 'message', 'create directory failed') - .having((e) => e.path, 'path', path) + .having((e) => e.path1, 'path1', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_ALREADY_EXISTS : errors.eexist, + io.Platform.isWindows + ? win32.ERROR_ALREADY_EXISTS + : errors.eexist, ), ), ); diff --git a/pkgs/io_file/test/create_temporary_directory_test.dart b/pkgs/io_file/test/create_temporary_directory_test.dart index ed267b30..2e267e69 100644 --- a/pkgs/io_file/test/create_temporary_directory_test.dart +++ b/pkgs/io_file/test/create_temporary_directory_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:io_file/io_file.dart'; import 'package:path/path.dart' as p; @@ -33,54 +33,54 @@ void main() { test('no arguments', () { final tmp1 = fileSystem.createTemporaryDirectory(); - addTearDown(() => Directory(tmp1).deleteSync()); + addTearDown(() => io.Directory(tmp1).deleteSync()); final tmp2 = fileSystem.createTemporaryDirectory(); - addTearDown(() => Directory(tmp2).deleteSync()); + addTearDown(() => io.Directory(tmp2).deleteSync()); expect(fileSystem.same(tmp1, tmp2), isFalse); - expect(Directory(tmp1).existsSync(), isTrue); - expect(Directory(tmp2).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp2).existsSync(), isTrue); }); test('prefix', () { final tmp1 = fileSystem.createTemporaryDirectory(prefix: 'myprefix'); - addTearDown(() => Directory(tmp1).deleteSync()); + addTearDown(() => io.Directory(tmp1).deleteSync()); final tmp2 = fileSystem.createTemporaryDirectory(prefix: 'myprefix'); - addTearDown(() => Directory(tmp2).deleteSync()); + addTearDown(() => io.Directory(tmp2).deleteSync()); expect(tmp1, contains('myprefix')); expect(tmp2, contains('myprefix')); expect(fileSystem.same(tmp1, tmp2), isFalse); - expect(Directory(tmp1).existsSync(), isTrue); - expect(Directory(tmp2).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp2).existsSync(), isTrue); }); test('prefix is empty string', () { final tmp1 = fileSystem.createTemporaryDirectory(prefix: ''); - addTearDown(() => Directory(tmp1).deleteSync()); + addTearDown(() => io.Directory(tmp1).deleteSync()); final tmp2 = fileSystem.createTemporaryDirectory(prefix: ''); - addTearDown(() => Directory(tmp2).deleteSync()); + addTearDown(() => io.Directory(tmp2).deleteSync()); expect(fileSystem.same(tmp1, tmp2), isFalse); - expect(Directory(tmp1).existsSync(), isTrue); - expect(Directory(tmp2).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp2).existsSync(), isTrue); }); test('prefix contains XXXXXX', () { final tmp1 = fileSystem.createTemporaryDirectory( prefix: 'myprefix-XXXXXX', ); - addTearDown(() => Directory(tmp1).deleteSync()); + addTearDown(() => io.Directory(tmp1).deleteSync()); final tmp2 = fileSystem.createTemporaryDirectory( prefix: 'myprefix-XXXXXX', ); - addTearDown(() => Directory(tmp2).deleteSync()); + addTearDown(() => io.Directory(tmp2).deleteSync()); expect(tmp1, contains('myprefix-')); expect(tmp2, contains('myprefix-')); expect(fileSystem.same(tmp1, tmp2), isFalse); - expect(Directory(tmp1).existsSync(), isTrue); - expect(Directory(tmp2).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp2).existsSync(), isTrue); }); test('parent', () { @@ -90,8 +90,8 @@ void main() { expect(tmp1, startsWith(tmp)); expect(tmp2, startsWith(tmp)); expect(fileSystem.same(tmp1, tmp2), isFalse); - expect(Directory(tmp1).existsSync(), isTrue); - expect(Directory(tmp2).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp2).existsSync(), isTrue); }); test('parent has a long directory name', () { @@ -99,9 +99,9 @@ void main() { // When using an API to create a directory, the specified path cannot be // so long that you cannot append an 8.3 file name (that is, the directory // name cannot exceed MAX_PATH minus 12). - final dirname = 'd' * (Platform.isWindows ? win32.MAX_PATH - 12 : 255); + final dirname = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); final parent = p.join(tmp, dirname); - Directory(parent).createSync(); + io.Directory(parent).createSync(); final tmp1 = fileSystem.createTemporaryDirectory(parent: parent); final tmp2 = fileSystem.createTemporaryDirectory(parent: parent); @@ -109,21 +109,21 @@ void main() { expect(tmp1, startsWith(tmp)); expect(tmp2, startsWith(tmp)); expect(fileSystem.same(tmp1, tmp2), isFalse); - expect(Directory(tmp1).existsSync(), isTrue); - expect(Directory(tmp2).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp2).existsSync(), isTrue); }); test('parent is empty string', () { final tmp1 = fileSystem.createTemporaryDirectory(parent: ''); - addTearDown(() => Directory(tmp1).deleteSync()); + addTearDown(() => io.Directory(tmp1).deleteSync()); final tmp2 = fileSystem.createTemporaryDirectory(parent: ''); - addTearDown(() => Directory(tmp2).deleteSync()); + addTearDown(() => io.Directory(tmp2).deleteSync()); expect(p.isRelative(tmp1), isTrue); expect(p.isRelative(tmp2), isTrue); expect(fileSystem.same(tmp1, tmp2), isFalse); - expect(Directory(tmp1).existsSync(), isTrue); - expect(Directory(tmp2).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp2).existsSync(), isTrue); }); test('parent does not exist', () { @@ -131,9 +131,9 @@ void main() { () => fileSystem.createTemporaryDirectory(parent: '/foo/bar/baz'), throwsA( isA().having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, + io.Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, ), ), ); @@ -141,7 +141,7 @@ void main() { test('prefix is absolute path inside of parent', () { final subdir1 = '$tmp/dir1'; - Directory(subdir1).createSync(); + io.Directory(subdir1).createSync(); final tmp1 = fileSystem.createTemporaryDirectory( parent: subdir1, @@ -154,8 +154,8 @@ void main() { test('prefix is absolute path outside of parent', () { final subdir1 = '$tmp/dir1'; final subdir2 = '$tmp/dir2'; - Directory(subdir1).createSync(); - Directory(subdir2).createSync(); + io.Directory(subdir1).createSync(); + io.Directory(subdir2).createSync(); final tmp1 = fileSystem.createTemporaryDirectory( parent: subdir1, @@ -170,9 +170,9 @@ void main() { () => fileSystem.createTemporaryDirectory(prefix: 'subdir/file'), throwsA( isA().having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, + io.Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, ), ), ); @@ -180,14 +180,14 @@ void main() { test('prefix is existant path inside temp directory', () { final subdir1 = '$tmp/dir1'; - Directory(subdir1).createSync(); + io.Directory(subdir1).createSync(); final tmp1 = fileSystem.createTemporaryDirectory( parent: tmp, prefix: 'dir1/file', ); expect(p.canonicalize(tmp1), startsWith(p.canonicalize(subdir1))); - expect(Directory(tmp1).existsSync(), isTrue); + expect(io.Directory(tmp1).existsSync(), isTrue); }); }); } diff --git a/pkgs/io_file/test/current_directory_test.dart b/pkgs/io_file/test/current_directory_test.dart index 6f746b11..9482d033 100644 --- a/pkgs/io_file/test/current_directory_test.dart +++ b/pkgs/io_file/test/current_directory_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:io_file/io_file.dart'; import 'package:path/path.dart' as p; @@ -30,11 +30,9 @@ void main() { deleteTemp(tmp); }); - //TODO(brianquinlan): test with a very long path. - test('absolute path', () { final path = '$tmp/dir'; - Directory(path).createSync(recursive: true); + io.Directory(path).createSync(recursive: true); fileSystem.currentDirectory = path; @@ -55,27 +53,27 @@ void main() { test('absolute path, too long path', () { // On Windows, limited to MAX_PATH (260) characters. final path = p.join(tmp, 'a' * 200, 'b' * 200); - Directory(path).createSync(recursive: true); + io.Directory(path).createSync(recursive: true); final oldCurrentDirectory = fileSystem.currentDirectory; expect( () => fileSystem.currentDirectory = path, throwsA( - isA() - .having((e) => e.path, 'path', path) + isA() + .having((e) => e.path1, 'path1', path) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', win32.ERROR_FILENAME_EXCED_RANGE, ), ), ); expect(fileSystem.currentDirectory, oldCurrentDirectory); - }, skip: !Platform.isWindows); + }, skip: !io.Platform.isWindows); test('relative path', () { final path = '$tmp/dir'; - Directory(path).createSync(recursive: true); + io.Directory(path).createSync(recursive: true); fileSystem.currentDirectory = 'dir'; diff --git a/pkgs/io_file/test/exceptions_test.dart b/pkgs/io_file/test/exceptions_test.dart index 101b435d..1b6f709c 100644 --- a/pkgs/io_file/test/exceptions_test.dart +++ b/pkgs/io_file/test/exceptions_test.dart @@ -20,14 +20,14 @@ void main() { test('with path1', () { expect( const IOFileException('cannot open file', path1: '/foo/bar').toString(), - 'IOFileException: cannot open file, path1="/foo/bar"', + 'IOFileException: cannot open file: "/foo/bar"', ); }); test('with path2', () { expect( const IOFileException('cannot open file', path2: '/foo/bar').toString(), - 'IOFileException: cannot open file, path2="/foo/bar"', + 'IOFileException: cannot open file -> "/foo/bar"', ); }); @@ -38,19 +38,27 @@ void main() { path1: '/foo/baz', path2: '/foo/bar', ).toString(), - 'IOFileException: cannot rename file, ' - 'path1="/foo/baz", path2="/foo/bar"', + 'IOFileException: cannot rename file: ' + '"/foo/baz" -> "/foo/bar"', ); }); - test('system call', () { + test('systemCall', () { expect( const IOFileException( 'cannot open file', - systemCall: SystemCallError('open', 13, 'permission denied'), + systemCall: 'open', ).toString(), 'IOFileException: cannot open file ' - '(open: permission denied, errorCode=13)', + '[open failed]', + ); + }); + + test('errorCode', () { + expect( + const IOFileException('cannot open file', errorCode: 2).toString(), + 'IOFileException: cannot open file ' + '[errorCode: 2]', ); }); @@ -60,11 +68,12 @@ void main() { 'cannot rename file', path1: '/foo/baz', path2: '/foo/bar', - systemCall: SystemCallError('rename', 13, 'permission denied'), + systemCall: 'renameat', + errorCode: 13, ).toString(), - 'IOFileException: cannot rename file, ' - 'path1="/foo/baz", path2="/foo/bar" ' - '(rename: permission denied, errorCode=13)', + 'IOFileException: cannot rename file: ' + '"/foo/baz" -> "/foo/bar" ' + '[renameat failed with errorCode: 13]', ); }); }); diff --git a/pkgs/io_file/test/metadata_test.dart b/pkgs/io_file/test/metadata_test.dart index 3743e10c..b94f4a75 100644 --- a/pkgs/io_file/test/metadata_test.dart +++ b/pkgs/io_file/test/metadata_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:io_file/io_file.dart'; import 'package:path/path.dart' as p; @@ -32,16 +32,14 @@ void main() { deleteTemp(tmp); }); - //TODO(brianquinlan): test with a very long path. - test('path does not exist', () { expect( () => fileSystem.metadata('$tmp/file1'), throwsA( isA().having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, ), ), ); @@ -49,7 +47,7 @@ void main() { test('absolute path, long name', () { final path = p.join(tmp, 'f' * 255); - File(path).writeAsStringSync('Hello World'); + io.File(path).writeAsStringSync('Hello World'); final data = fileSystem.metadata(path); expect(data.isFile, isTrue); @@ -57,7 +55,7 @@ void main() { test('relative path, long name', () { final path = 'f' * 255; - File(path).writeAsStringSync('Hello World'); + io.File(path).writeAsStringSync('Hello World'); final data = fileSystem.metadata(path); expect(data.isFile, isTrue); @@ -81,16 +79,16 @@ void main() { expect(data.type, FileSystemType.character); }, skip: - !(Platform.isAndroid | - Platform.isIOS | - Platform.isLinux | - Platform.isIOS) + !(io.Platform.isAndroid | + io.Platform.isIOS | + io.Platform.isLinux | + io.Platform.isIOS) ? 'no /dev/tty' : false, ); test('file', () { final path = '$tmp/file1'; - File(path).writeAsStringSync('Hello World'); + io.File(path).writeAsStringSync('Hello World'); final data = fileSystem.metadata(path); expect(data.isDirectory, isFalse); @@ -107,7 +105,7 @@ void main() { expect(data.isLink, isFalse); expect( data.type, - Platform.isWindows ? FileSystemType.unknown : FileSystemType.pipe, + io.Platform.isWindows ? FileSystemType.unknown : FileSystemType.pipe, ); try { @@ -117,9 +115,9 @@ void main() { } catch (_) {} }); test('file link', () { - File('$tmp/file1').writeAsStringSync('Hello World'); + io.File('$tmp/file1').writeAsStringSync('Hello World'); final path = '$tmp/link'; - Link(path).createSync('$tmp/file1'); + io.Link(path).createSync('$tmp/file1'); final data = fileSystem.metadata(path); expect(data.isDirectory, isFalse); @@ -128,9 +126,9 @@ void main() { expect(data.type, FileSystemType.link); }); test('directory link', () { - Directory('$tmp/dir').createSync(); + io.Directory('$tmp/dir').createSync(); final path = '$tmp/link'; - Link(path).createSync('$tmp/dir'); + io.Link(path).createSync('$tmp/dir'); final data = fileSystem.metadata(path); expect(data.isDirectory, isFalse); @@ -147,27 +145,27 @@ void main() { // Tested on Windows at: metadata_windows_test.dart final path = '$tmp/file1'; - File(path).writeAsStringSync('Hello World!'); + io.File(path).writeAsStringSync('Hello World!'); final data = fileSystem.metadata(path); expect(data.isHidden, isNull); }, skip: - (Platform.isIOS || Platform.isMacOS || Platform.isWindows) + (io.Platform.isIOS || io.Platform.isMacOS || io.Platform.isWindows) ? 'does not support hidden file metadata' : false, ); group('size', () { test('empty file', () { final path = '$tmp/file1'; - File(path).writeAsStringSync(''); + io.File(path).writeAsStringSync(''); final data = fileSystem.metadata(path); expect(data.size, 0); }); test('non-empty file', () { final path = '$tmp/file1'; - File(path).writeAsStringSync('Hello World!'); + io.File(path).writeAsStringSync('Hello World!'); final data = fileSystem.metadata(path); expect(data.size, 12); @@ -179,7 +177,7 @@ void main() { () { test('newly created', () { final path = '$tmp/file1'; - File(path).writeAsStringSync(''); + io.File(path).writeAsStringSync(''); final data = fileSystem.metadata(path); expect( @@ -189,7 +187,7 @@ void main() { }); }, skip: - !(Platform.isIOS || Platform.isMacOS || Platform.isWindows) + !(io.Platform.isIOS || io.Platform.isMacOS || io.Platform.isWindows) ? 'creation not supported' : false, ); @@ -197,7 +195,7 @@ void main() { group('modification', () { test('newly created', () { final path = '$tmp/file1'; - File(path).writeAsStringSync('Hello World!'); + io.File(path).writeAsStringSync('Hello World!'); final data = fileSystem.metadata(path); expect( @@ -207,11 +205,11 @@ void main() { }); test('modified after creation', () async { final path = '$tmp/file1'; - File(path).writeAsStringSync('Hello World!'); + io.File(path).writeAsStringSync('Hello World!'); final data1 = fileSystem.metadata(path); await Future.delayed(const Duration(milliseconds: 1000)); - File(path).writeAsStringSync('Hello World!'); + io.File(path).writeAsStringSync('Hello World!'); final data2 = fileSystem.metadata(path); expect( diff --git a/pkgs/io_file/test/read_as_bytes_test.dart b/pkgs/io_file/test/read_as_bytes_test.dart index 94fb0756..f4a5c428 100644 --- a/pkgs/io_file/test/read_as_bytes_test.dart +++ b/pkgs/io_file/test/read_as_bytes_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'dart:typed_data'; import 'package:io_file/io_file.dart'; @@ -41,13 +41,14 @@ void main() { () => fileSystem.readAsBytes('doesnotexist'), throwsA( isA() - .having((e) => e.message, 'message', 'open failed') .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_FILE_NOT_FOUND + : errors.enoent, ) - .having((e) => e.path, 'path', 'doesnotexist'), + .having((e) => e.path1, 'path1', 'doesnotexist'), ), ); }); @@ -56,18 +57,15 @@ void main() { expect( () => fileSystem.readAsBytes(tmp), throwsA( - isA() + isA() .having( - (e) => e.message, - 'message', - Platform.isWindows ? 'open failed' : 'read failed', - ) - .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_ACCESS_DENIED : errors.eisdir, + io.Platform.isWindows + ? win32.ERROR_ACCESS_DENIED + : errors.eisdir, ) - .having((e) => e.path, 'path', tmp), + .having((e) => e.path1, 'path1', tmp), ), ); }); @@ -77,8 +75,8 @@ void main() { final path2 = '$tmp/file2'; final data = randomUint8List(20); - File(path1).writeAsBytesSync(data); - Link(path2).createSync(path1); + io.File(path1).writeAsBytesSync(data); + io.Link(path2).createSync(path1); expect(fileSystem.readAsBytes(path2), data); }); @@ -88,17 +86,16 @@ void main() { final path2 = '$tmp/file2'; final data = randomUint8List(20); - File(path1).writeAsBytesSync(data); - Link(path2).createSync(path1); - File(path1).deleteSync(); + io.File(path1).writeAsBytesSync(data); + io.Link(path2).createSync(path1); + io.File(path1).deleteSync(); expect( () => fileSystem.readAsBytes(path2), throwsA( isA() - .having((e) => e.message, 'message', 'open failed') - .having((e) => e.osError?.errorCode, 'errorCode', errors.enoent) - .having((e) => e.path, 'path', path2), + .having((e) => e.errorCode, 'errorCode', errors.enoent) + .having((e) => e.path1, 'path1', path2), ), ); }); @@ -148,7 +145,7 @@ void main() { test('absolute path, long file name', () { final data = randomUint8List(20); final path = p.join(tmp, 'f' * 255); - File(path).writeAsBytesSync(data); + io.File(path).writeAsBytesSync(data); expect(fileSystem.readAsBytes(path), data); }); @@ -156,7 +153,7 @@ void main() { test('relative path, long file name', () { final data = randomUint8List(20); final path = 'f' * 255; - File(path).writeAsBytesSync(data); + io.File(path).writeAsBytesSync(data); expect(fileSystem.readAsBytes(path), data); }); @@ -166,7 +163,7 @@ void main() { final data = randomUint8List(i); final path = '$tmp/file'; - File(path).writeAsBytesSync(data); + io.File(path).writeAsBytesSync(data); expect(fileSystem.readAsBytes(path), data); }); } @@ -176,7 +173,7 @@ void main() { final data = randomUint8List(i); final path = '$tmp/file1'; - File(path).writeAsBytesSync(data); + io.File(path).writeAsBytesSync(data); expect(fileSystem.readAsBytes(path), data); }); } @@ -187,7 +184,7 @@ void main() { final data = randomUint8List(1 << 31); final path = '$tmp/file'; - File(path).writeAsBytesSync(data); + io.File(path).writeAsBytesSync(data); expect(fileSystem.readAsBytes(path), data); }, skip: 'very slow'); }); diff --git a/pkgs/io_file/test/remove_directory_test.dart b/pkgs/io_file/test/remove_directory_test.dart index a864c468..cbe92f67 100644 --- a/pkgs/io_file/test/remove_directory_test.dart +++ b/pkgs/io_file/test/remove_directory_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:io_file/io_file.dart'; import 'package:path/path.dart' as p; @@ -33,11 +33,14 @@ void main() { test('success', () { final path = '$tmp/dir'; - Directory(path).createSync(); + io.Directory(path).createSync(); fileSystem.removeDirectory(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }); test('absolute path, long directory name', () { @@ -45,12 +48,15 @@ void main() { // When using an API to create a directory, the specified path cannot be // so long that you cannot append an 8.3 file name (that is, the directory // name cannot exceed MAX_PATH minus 12). - final dirname = 'd' * (Platform.isWindows ? win32.MAX_PATH - 12 : 255); + final dirname = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); final path = p.join(tmp, dirname); - Directory(path).createSync(); + io.Directory(path).createSync(); fileSystem.removeDirectory(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }); test('relative path, long directory name', () { @@ -58,30 +64,31 @@ void main() { // When using an API to create a directory, the specified path cannot be // so long that you cannot append an 8.3 file name (that is, the directory // name cannot exceed MAX_PATH minus 12). - final path = 'd' * (Platform.isWindows ? win32.MAX_PATH - 12 : 255); - Directory(path).createSync(); + final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); + io.Directory(path).createSync(); fileSystem.removeDirectory(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }); test('non-empty directory', () { final path = '$tmp/dir'; - Directory(path).createSync(); - File('$tmp/dir/file').writeAsStringSync('Hello World!'); + io.Directory(path).createSync(); + io.File('$tmp/dir/file').writeAsStringSync('Hello World!'); expect( () => fileSystem.removeDirectory(path), throwsA( - isA() - .having((e) => e.message, 'message', 'remove directory failed') - .having( - (e) => e.osError?.errorCode, - 'errorCode', - Platform.isWindows - ? win32.ERROR_DIR_NOT_EMPTY - : errors.enotempty, - ), + isA().having( + (e) => e.errorCode, + 'errorCode', + io.Platform.isWindows + ? win32.ERROR_DIR_NOT_EMPTY + : errors.enotempty, + ), ), ); }); @@ -92,31 +99,27 @@ void main() { expect( () => fileSystem.removeDirectory(path), throwsA( - isA() - .having((e) => e.message, 'message', 'remove directory failed') - .having( - (e) => e.osError?.errorCode, - 'errorCode', - Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, - ), + isA().having( + (e) => e.errorCode, + 'errorCode', + io.Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, + ), ), ); }); test('file', () { final path = '$tmp/file'; - File(path).writeAsStringSync('Hello World!'); + io.File(path).writeAsStringSync('Hello World!'); expect( () => fileSystem.removeDirectory(path), throwsA( - isA() - .having((e) => e.message, 'message', 'remove directory failed') - .having( - (e) => e.osError?.errorCode, - 'errorCode', - Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, - ), + isA().having( + (e) => e.errorCode, + 'errorCode', + io.Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, + ), ), ); }); @@ -124,32 +127,30 @@ void main() { test('link', () { final dirPath = '$tmp/dir'; final linkPath = '$tmp/link'; - Directory(dirPath).createSync(); - Link(linkPath).createSync(dirPath); + io.Directory(dirPath).createSync(); + io.Link(linkPath).createSync(dirPath); - if (Platform.isWindows) { + if (io.Platform.isWindows) { fileSystem.removeDirectory(linkPath); expect( - FileSystemEntity.typeSync(dirPath), - FileSystemEntityType.directory, + io.FileSystemEntity.typeSync(dirPath), + io.FileSystemEntityType.directory, ); expect( - FileSystemEntity.typeSync(linkPath), - FileSystemEntityType.notFound, + io.FileSystemEntity.typeSync(linkPath), + io.FileSystemEntityType.notFound, ); } else { expect( () => fileSystem.removeDirectory(linkPath), throwsA( - isA() - .having((e) => e.message, 'message', 'remove directory failed') - .having( - (e) => e.osError?.errorCode, - 'errorCode', - Platform.isWindows - ? win32.ERROR_PATH_NOT_FOUND - : errors.enotdir, - ), + isA().having( + (e) => e.errorCode, + 'errorCode', + io.Platform.isWindows + ? win32.ERROR_PATH_NOT_FOUND + : errors.enotdir, + ), ), ); } diff --git a/pkgs/io_file/test/remove_directory_tree_test.dart b/pkgs/io_file/test/remove_directory_tree_test.dart index b3dec1ff..5e208ab7 100644 --- a/pkgs/io_file/test/remove_directory_tree_test.dart +++ b/pkgs/io_file/test/remove_directory_tree_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:ffi/ffi.dart'; import 'package:io_file/io_file.dart'; @@ -32,15 +32,16 @@ void main() { deleteTemp(tmp); }); - //TODO(brianquinlan): test with a very long path. - test('empty', () { final path = '$tmp/dir'; - Directory(path).createSync(); + io.Directory(path).createSync(); fileSystem.removeDirectoryTree(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }); test( @@ -50,16 +51,20 @@ void main() { // When using an API to create a directory, the specified path cannot be // so long that you cannot append an 8.3 file name (that is, the // directory name cannot exceed MAX_PATH minus 12). - final dirname = 'd' * (Platform.isWindows ? win32.MAX_PATH - 12 : 255); + final dirname = + 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); final path = p.join(tmp, dirname); - Directory(path).createSync(); - File('$path/file').writeAsStringSync('Hello World!'); + io.Directory(path).createSync(); + io.File('$path/file').writeAsStringSync('Hello World!'); fileSystem.removeDirectoryTree(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }, - skip: Platform.isWindows ? 'TODO(bquinlan): make this pass' : false, + skip: io.Platform.isWindows ? 'TODO(bquinlan): make this pass' : false, ); test( @@ -69,59 +74,74 @@ void main() { // When using an API to create a directory, the specified path cannot be // so long that you cannot append an 8.3 file name (that is, the // directory name cannot exceed MAX_PATH minus 12). - final path = 'd' * (Platform.isWindows ? win32.MAX_PATH - 12 : 255); - Directory(path).createSync(); - File('$path/file').writeAsStringSync('Hello World!'); + final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); + io.Directory(path).createSync(); + io.File('$path/file').writeAsStringSync('Hello World!'); fileSystem.removeDirectoryTree(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }, - skip: Platform.isWindows ? 'TODO(bquinlan) make this pass' : false, + skip: io.Platform.isWindows ? 'TODO(bquinlan) make this pass' : false, ); test('contains single file', () { final path = '$tmp/dir'; - Directory(path).createSync(); - File('$path/file').writeAsStringSync('Hello World!'); + io.Directory(path).createSync(); + io.File('$path/file').writeAsStringSync('Hello World!'); fileSystem.removeDirectoryTree(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }); test('contains single link', () { final path = '$tmp/dir'; - Directory(path).createSync(); - Link('$path/link').createSync(tmp); + io.Directory(path).createSync(); + io.Link('$path/link').createSync(tmp); fileSystem.removeDirectoryTree(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); - expect(FileSystemEntity.typeSync(tmp), FileSystemEntityType.directory); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); + expect( + io.FileSystemEntity.typeSync(tmp), + io.FileSystemEntityType.directory, + ); }); test('contains single empty directory', () { final path = '$tmp/dir'; - Directory(path).createSync(); - Directory('$path/subdir').createSync(); + io.Directory(path).createSync(); + io.Directory('$path/subdir').createSync(); fileSystem.removeDirectoryTree(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }); test('complex tree', () { void createTree(String path, int depth) { - Directory(path).createSync(); + io.Directory(path).createSync(); - File('$path/file1').writeAsStringSync('Hello World'); - Link('$path/filelink1').createSync('$path/file1'); - Link('$path/dirlink1').createSync(path); + io.File('$path/file1').writeAsStringSync('Hello World'); + io.Link('$path/filelink1').createSync('$path/file1'); + io.Link('$path/dirlink1').createSync(path); if (depth > 0) { createTree('$path/dir1', depth - 1); - Link('$path/dirlink2').createSync('$path/dir1'); + io.Link('$path/dirlink2').createSync('$path/dir1'); } } @@ -130,7 +150,10 @@ void main() { fileSystem.removeDirectoryTree(path); - expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound); + expect( + io.FileSystemEntity.typeSync(path), + io.FileSystemEntityType.notFound, + ); }); test( @@ -138,9 +161,9 @@ void main() { () { final path = '$tmp/dir'; final undeletablePath = p.join(path, 'subdir1', 'subdir2', 'file1'); - Directory(path).createSync(); - Directory('$path/subdir1/subdir2').createSync(recursive: true); - File('$path/subdir1/subdir2/file1').writeAsStringSync('Hello World'); + io.Directory(path).createSync(); + io.Directory('$path/subdir1/subdir2').createSync(recursive: true); + io.File('$path/subdir1/subdir2/file1').writeAsStringSync('Hello World'); // r-xr-x--- if (libc.chmod('$path/subdir1/subdir2'.toNativeUtf8().cast(), 360) == -1) { @@ -155,17 +178,13 @@ void main() { () => fileSystem.removeDirectoryTree(path), throwsA( isA() - .having((e) => e.path, 'path', undeletablePath) - .having( - (e) => e.osError?.errorCode, - 'errorCode', - errors.eaccess, - ), + .having((e) => e.path1, 'path1', undeletablePath) + .having((e) => e.errorCode, 'errorCode', errors.eaccess), ), ); }, skip: - Platform.isWindows + io.Platform.isWindows ? 'TODO(brianquinlan): make this work on Windows' : false, ); @@ -175,9 +194,9 @@ void main() { () { final path = '$tmp/dir'; final unreadableDirectory = p.join(path, 'subdir1', 'subdir2'); - Directory(path).createSync(); - Directory('$path/subdir1/subdir2').createSync(recursive: true); - File('$path/subdir1/subdir2/file1').writeAsStringSync('Hello World'); + io.Directory(path).createSync(); + io.Directory('$path/subdir1/subdir2').createSync(recursive: true); + io.File('$path/subdir1/subdir2/file1').writeAsStringSync('Hello World'); // -wx-wx--- if (libc.chmod('$path/subdir1/subdir2'.toNativeUtf8().cast(), 216) == -1) { @@ -192,17 +211,13 @@ void main() { () => fileSystem.removeDirectoryTree(path), throwsA( isA() - .having((e) => e.path, 'path', unreadableDirectory) - .having( - (e) => e.osError?.errorCode, - 'errorCode', - errors.eaccess, - ), + .having((e) => e.path1, 'path1', unreadableDirectory) + .having((e) => e.errorCode, 'errorCode', errors.eaccess), ), ); }, skip: - Platform.isWindows + io.Platform.isWindows ? 'TODO(brianquinlan): make this work on Windows' : false, ); @@ -214,9 +229,9 @@ void main() { () => fileSystem.removeDirectoryTree(path), throwsA( isA().having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, + io.Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : errors.enoent, ), ), ); @@ -224,47 +239,47 @@ void main() { test('file', () { final path = '$tmp/file'; - File(path).writeAsStringSync('Hello World!'); + io.File(path).writeAsStringSync('Hello World!'); expect( () => fileSystem.removeDirectoryTree(path), throwsA( - isA().having( - (e) => e.osError?.errorCode, + isA().having( + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, + io.Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, ), ), ); }); test('file link', () { - File('$tmp/file').writeAsStringSync('Hello World!'); - Link('$tmp/link').createSync('$tmp/file'); + io.File('$tmp/file').writeAsStringSync('Hello World!'); + io.Link('$tmp/link').createSync('$tmp/file'); expect( () => fileSystem.removeDirectoryTree('$tmp/link'), throwsA( - isA().having( - (e) => e.osError?.errorCode, + isA().having( + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, + io.Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, ), ), ); }); test('directory link', () { - File('$tmp/dir').createSync(); - Link('$tmp/link').createSync('$tmp/dir'); + io.File('$tmp/dir').createSync(); + io.Link('$tmp/link').createSync('$tmp/dir'); expect( () => fileSystem.removeDirectoryTree('$tmp/link'), throwsA( - isA().having( - (e) => e.osError?.errorCode, + isA().having( + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, + io.Platform.isWindows ? win32.ERROR_DIRECTORY : errors.enotdir, ), ), ); diff --git a/pkgs/io_file/test/rename_test.dart b/pkgs/io_file/test/rename_test.dart index 114e995e..f15cea91 100644 --- a/pkgs/io_file/test/rename_test.dart +++ b/pkgs/io_file/test/rename_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:io_file/io_file.dart'; import 'package:path/path.dart' as p; @@ -33,51 +33,51 @@ void main() { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path1).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); fileSystem.rename(path1, path2); - expect(File(path1).existsSync(), isFalse); - expect(File(path2).existsSync(), isTrue); + expect(io.File(path1).existsSync(), isFalse); + expect(io.File(path2).existsSync(), isTrue); }); test('move between absolute paths, long file names', () { final path1 = p.join(tmp, '1' * 255); final path2 = p.join(tmp, '2' * 255); - File(path1).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); fileSystem.rename(path1, path2); - expect(File(path1).existsSync(), isFalse); - expect(File(path2).existsSync(), isTrue); + expect(io.File(path1).existsSync(), isFalse); + expect(io.File(path2).existsSync(), isTrue); }); test('move between relative path, long file names', () { final path1 = '1' * 255; final path2 = '2' * 255; - File(path1).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); fileSystem.rename(path1, path2); - expect(File(path1).existsSync(), isFalse); - expect(File(path2).existsSync(), isTrue); + expect(io.File(path1).existsSync(), isFalse); + expect(io.File(path2).existsSync(), isTrue); }); test('move file to existing', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path1).writeAsStringSync('Hello World #1'); - File(path2).writeAsStringSync('Hello World #2'); + io.File(path1).writeAsStringSync('Hello World #1'); + io.File(path2).writeAsStringSync('Hello World #2'); fileSystem.rename(path1, path2); - expect(File(path1).existsSync(), isFalse); - expect(File(path2).readAsStringSync(), 'Hello World #1'); + expect(io.File(path1).existsSync(), isFalse); + expect(io.File(path2).readAsStringSync(), 'Hello World #1'); }); test('move directory absolute path', () { final path1 = '$tmp/dir1'; final path2 = '$tmp/dir2'; - Directory(path1).createSync(recursive: true); + io.Directory(path1).createSync(recursive: true); fileSystem.rename(path1, path2); - expect(Directory(path1).existsSync(), isFalse); - expect(Directory(path2).existsSync(), isTrue); + expect(io.Directory(path1).existsSync(), isFalse); + expect(io.Directory(path2).existsSync(), isTrue); }); test('move non-existant', () { @@ -87,13 +87,11 @@ void main() { expect( () => fileSystem.rename(path1, path2), throwsA( - isA() - .having((e) => e.message, 'message', 'rename failed') - .having( - (e) => e.osError?.errorCode, - 'errorCode', - 2, // ENOENT, ERROR_FILE_NOT_FOUND - ), + isA().having( + (e) => e.errorCode, + 'errorCode', + 2, // ENOENT, ERROR_FILE_NOT_FOUND + ), ), ); }); @@ -102,21 +100,19 @@ void main() { final path1 = '$tmp/file1'; final path2 = '$tmp/dir1'; - File(path1).writeAsStringSync('Hello World'); - Directory(path2).createSync(recursive: true); + io.File(path1).writeAsStringSync('Hello World'); + io.Directory(path2).createSync(recursive: true); expect( () => fileSystem.rename(path1, path2), throwsA( - isA() - .having((e) => e.message, 'message', 'rename failed') - .having( - (e) => e.osError?.errorCode, - 'errorCode', - Platform.isWindows - ? 5 // ERROR_ACCESS_DENIED - : 21, // EISDIR - ), + isA().having( + (e) => e.errorCode, + 'errorCode', + io.Platform.isWindows + ? 5 // ERROR_ACCESS_DENIED + : 21, // EISDIR + ), ), ); }); diff --git a/pkgs/io_file/test/same_test.dart b/pkgs/io_file/test/same_test.dart index 2126bc5d..5571414e 100644 --- a/pkgs/io_file/test/same_test.dart +++ b/pkgs/io_file/test/same_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'package:io_file/io_file.dart'; import 'package:path/path.dart' as p; @@ -35,17 +35,19 @@ void main() { test('path1 does not exist', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path2).writeAsStringSync('Hello World'); + io.File(path2).writeAsStringSync('Hello World'); expect( () => fileSystem.same(path1, path2), throwsA( isA() - .having((e) => e.path, 'path', path1) + .having((e) => e.path1, 'path1', path1) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_FILE_NOT_FOUND + : errors.enoent, ), ), ); @@ -54,17 +56,19 @@ void main() { test('path2 does not exist', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path1).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); expect( () => fileSystem.same(path1, path2), throwsA( isA() - .having((e) => e.path, 'path', path2) + .having((e) => e.path1, 'path1', path2) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_FILE_NOT_FOUND + : errors.enoent, ), ), ); @@ -78,11 +82,13 @@ void main() { () => fileSystem.same(path1, path2), throwsA( isA() - .having((e) => e.path, 'path', path1) + .having((e) => e.path1, 'path1', path1) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_FILE_NOT_FOUND + : errors.enoent, ), ), ); @@ -91,18 +97,20 @@ void main() { test('path1 is a broken symlink', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - Link(path1).createSync('$tmp/file3'); - File(path2).writeAsStringSync('Hello World'); + io.Link(path1).createSync('$tmp/file3'); + io.File(path2).writeAsStringSync('Hello World'); expect( () => fileSystem.same(path1, path2), throwsA( isA() - .having((e) => e.path, 'path', path1) + .having((e) => e.path1, 'path1', path1) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_FILE_NOT_FOUND + : errors.enoent, ), ), ); @@ -111,18 +119,20 @@ void main() { test('path2 is a broken symlink', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path1).writeAsStringSync('Hello World'); - Link(path2).createSync('$tmp/file3'); + io.File(path1).writeAsStringSync('Hello World'); + io.Link(path2).createSync('$tmp/file3'); expect( () => fileSystem.same(path1, path2), throwsA( isA() - .having((e) => e.path, 'path', path2) + .having((e) => e.path1, 'path1', path2) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_FILE_NOT_FOUND + : errors.enoent, ), ), ); @@ -131,17 +141,19 @@ void main() { test('path1 and path2 same, broken symlinks', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file1'; - Link(path1).createSync('$tmp/file3'); + io.Link(path1).createSync('$tmp/file3'); expect( () => fileSystem.same(path1, path2), throwsA( isA() - .having((e) => e.path, 'path', path1) + .having((e) => e.path1, 'path1', path1) .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent, + io.Platform.isWindows + ? win32.ERROR_FILE_NOT_FOUND + : errors.enoent, ), ), ); @@ -150,8 +162,8 @@ void main() { test('different files, same content', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path1).writeAsStringSync('Hello World'); - File(path2).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); + io.File(path2).writeAsStringSync('Hello World'); expect(fileSystem.same(path1, path2), isFalse); }); @@ -160,7 +172,7 @@ void main() { fileSystem.currentDirectory = tmp; final path1 = '$tmp/file1'; const path2 = 'file1'; - File(path1).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); expect(fileSystem.same(path1, path2), isTrue); }); @@ -168,8 +180,8 @@ void main() { test('file path1, symlink path2', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path1).writeAsStringSync('Hello World'); - Link(path2).createSync(path1); + io.File(path1).writeAsStringSync('Hello World'); + io.Link(path2).createSync(path1); expect(fileSystem.same(path1, path2), isTrue); }); @@ -177,19 +189,19 @@ void main() { test('file symlink path1, symlink path2', () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File('$tmp/file3').writeAsStringSync('Hello World'); - Link(path1).createSync('$tmp/file3'); - Link(path2).createSync('$tmp/file3'); + io.File('$tmp/file3').writeAsStringSync('Hello World'); + io.Link(path1).createSync('$tmp/file3'); + io.Link(path2).createSync('$tmp/file3'); expect(fileSystem.same(path1, path2), isTrue); }); test('files through intermediate symlinks', () { - Directory('$tmp/subdir').createSync(); - Link('$tmp/link-to-subdir').createSync('$tmp/subdir'); + io.Directory('$tmp/subdir').createSync(); + io.Link('$tmp/link-to-subdir').createSync('$tmp/subdir'); final path1 = '$tmp/subdir/file1'; final path2 = '$tmp/link-to-subdir/file1'; - File(path1).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); expect(fileSystem.same(path1, path2), isTrue); }); @@ -199,18 +211,18 @@ void main() { () { final path1 = '$tmp/file1'; final path2 = '$tmp/file2'; - File(path1).writeAsStringSync('Hello World'); + io.File(path1).writeAsStringSync('Hello World'); stdlibc.link(path1, path2); expect(fileSystem.same(path1, path2), isTrue); }, - skip: Platform.isWindows ? 'hard links not supported' : false, + skip: io.Platform.isWindows ? 'hard links not supported' : false, ); test('different directories, same content', () { final path1 = '$tmp/dir1'; final path2 = '$tmp/dir2'; - Directory(path1).createSync(); - Directory(path2).createSync(); + io.Directory(path1).createSync(); + io.Directory(path2).createSync(); expect(fileSystem.same(path1, path2), isFalse); }); @@ -218,7 +230,7 @@ void main() { test('same directory, absolute and relative paths', () { final path1 = '$tmp/dir1'; const path2 = 'dir1'; - Directory(path1).createSync(); + io.Directory(path1).createSync(); expect(fileSystem.same(path1, path2), isTrue); }); @@ -226,8 +238,8 @@ void main() { test('directory path1, symlink path2', () { final path1 = '$tmp/dir1'; final path2 = '$tmp/dir2'; - Directory(path1).createSync(); - Link(path2).createSync(path1); + io.Directory(path1).createSync(); + io.Link(path2).createSync(path1); expect(fileSystem.same(path1, path2), isTrue); }); @@ -235,19 +247,19 @@ void main() { test('directory symlink path1, symlink path2', () { final path1 = '$tmp/dir1'; final path2 = '$tmp/dir2'; - Directory('$tmp/dir3').createSync(); - Link(path1).createSync('$tmp/dir3'); - Link(path2).createSync('$tmp/dir3'); + io.Directory('$tmp/dir3').createSync(); + io.Link(path1).createSync('$tmp/dir3'); + io.Link(path2).createSync('$tmp/dir3'); expect(fileSystem.same(path1, path2), isTrue); }); test('directories through intermediate symlinks', () { - Directory('$tmp/subdir').createSync(); - Link('$tmp/link-to-subdir').createSync('$tmp/subdir'); + io.Directory('$tmp/subdir').createSync(); + io.Link('$tmp/link-to-subdir').createSync('$tmp/subdir'); final path1 = '$tmp/subdir/dir1'; final path2 = '$tmp/link-to-subdir/dir1'; - Directory(path1).createSync(); + io.Directory(path1).createSync(); expect(fileSystem.same(path1, path2), isTrue); }); @@ -255,8 +267,8 @@ void main() { test('absolute path, long names', () { final path1 = p.join(tmp, '1' * 255); final path2 = p.join(tmp, '2' * 255); - File(path1).writeAsStringSync('Hello World'); - Link(path2).createSync(path1); + io.File(path1).writeAsStringSync('Hello World'); + io.Link(path2).createSync(path1); expect(fileSystem.same(path1, path2), isTrue); }); @@ -264,8 +276,8 @@ void main() { test('relative path, long names', () { final path1 = '1' * 255; final path2 = '2' * 255; - File(path1).writeAsStringSync('Hello World'); - Link(path2).createSync(path1); + io.File(path1).writeAsStringSync('Hello World'); + io.Link(path2).createSync(path1); expect(fileSystem.same(path1, path2), isTrue); }); diff --git a/pkgs/io_file/test/write_as_bytes_test.dart b/pkgs/io_file/test/write_as_bytes_test.dart index 93851466..6df62f3e 100644 --- a/pkgs/io_file/test/write_as_bytes_test.dart +++ b/pkgs/io_file/test/write_as_bytes_test.dart @@ -5,7 +5,7 @@ @TestOn('vm') library; -import 'dart:io'; +import 'dart:io' as io; import 'dart:typed_data'; import 'package:io_file/io_file.dart'; @@ -40,14 +40,15 @@ void main() { WriteMode.truncateExisting, ), throwsA( - isA() - .having((e) => e.message, 'message', 'open failed') + isA() .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_ACCESS_DENIED : errors.eisdir, + io.Platform.isWindows + ? win32.ERROR_ACCESS_DENIED + : errors.eisdir, ) - .having((e) => e.path, 'path', tmp), + .having((e) => e.path1, 'path1', tmp), ), ); }); @@ -56,8 +57,8 @@ void main() { final filePath = '$tmp/file1'; final symlinkPath = '$tmp/file2'; final data = randomUint8List(20); - File(filePath).writeAsBytesSync(Uint8List(0)); - Link(symlinkPath).createSync(filePath); + io.File(filePath).writeAsBytesSync(Uint8List(0)); + io.Link(symlinkPath).createSync(filePath); fileSystem.writeAsBytes(symlinkPath, data, WriteMode.truncateExisting); @@ -69,11 +70,11 @@ void main() { final filePath = '$tmp/file1'; final symlinkPath = '$tmp/file2'; final data = randomUint8List(20); - File(filePath).writeAsBytesSync(Uint8List(0)); - Link(symlinkPath).createSync(filePath); - File(filePath).deleteSync(); + io.File(filePath).writeAsBytesSync(Uint8List(0)); + io.Link(symlinkPath).createSync(filePath); + io.File(filePath).deleteSync(); - if (Platform.isWindows) { + if (io.Platform.isWindows) { // Windows considers a broken symlink to not be an existing file. fileSystem.writeAsBytes( symlinkPath, @@ -94,15 +95,14 @@ void main() { ), throwsA( isA() - .having((e) => e.message, 'message', 'open failed') .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows + io.Platform.isWindows ? win32.ERROR_FILE_EXISTS : errors.eexist, ) - .having((e) => e.path, 'path', symlinkPath), + .having((e) => e.path1, 'path1', symlinkPath), ), ); } @@ -111,9 +111,9 @@ void main() { final filePath = '$tmp/file1'; final symlinkPath = '$tmp/file2'; final data = randomUint8List(20); - File(filePath).writeAsBytesSync(Uint8List(0)); - Link(symlinkPath).createSync(filePath); - File(filePath).deleteSync(); + io.File(filePath).writeAsBytesSync(Uint8List(0)); + io.Link(symlinkPath).createSync(filePath); + io.File(filePath).deleteSync(); fileSystem.writeAsBytes(symlinkPath, data, WriteMode.truncateExisting); @@ -135,7 +135,7 @@ void main() { WriteMode.appendExisting, ); - expect(File(path).readAsBytesSync(), data); + expect(io.File(path).readAsBytesSync(), data); }); test('failExisting', () { @@ -148,7 +148,7 @@ void main() { WriteMode.failExisting, ); - expect(File(path).readAsBytesSync(), data); + expect(io.File(path).readAsBytesSync(), data); }); test('truncateExisting', () { @@ -161,7 +161,7 @@ void main() { WriteMode.truncateExisting, ); - expect(File(path).readAsBytesSync(), data); + expect(io.File(path).readAsBytesSync(), data); }); }); @@ -169,7 +169,7 @@ void main() { test('appendExisting', () { final data = randomUint8List(20); final path = '$tmp/file'; - File(path).writeAsBytesSync([1, 2, 3]); + io.File(path).writeAsBytesSync([1, 2, 3]); fileSystem.writeAsBytes( path, @@ -177,14 +177,14 @@ void main() { WriteMode.appendExisting, ); - expect(File(path).readAsBytesSync(), [1, 2, 3] + data); + expect(io.File(path).readAsBytesSync(), [1, 2, 3] + data); }); test('null file', () { final data = randomUint8List(20); fileSystem.writeAsBytes( - Platform.isWindows ? r'\\.\NUL' : '/dev/null', + io.Platform.isWindows ? r'\\.\NUL' : '/dev/null', Uint8List.fromList(data), WriteMode.appendExisting, ); @@ -193,19 +193,20 @@ void main() { test('failExisting', () { final data = randomUint8List(20); final path = '$tmp/file'; - File(path).writeAsBytesSync([1, 2, 3]); + io.File(path).writeAsBytesSync([1, 2, 3]); expect( () => fileSystem.writeAsBytes(path, data, WriteMode.failExisting), throwsA( isA() - .having((e) => e.message, 'message', 'open failed') .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_EXISTS : errors.eexist, + io.Platform.isWindows + ? win32.ERROR_FILE_EXISTS + : errors.eexist, ) - .having((e) => e.path, 'path', path), + .having((e) => e.path1, 'path1', path), ), ); }); @@ -213,11 +214,11 @@ void main() { test('truncateExisting', () { final data = randomUint8List(20); final path = '$tmp/file'; - File(path).writeAsBytesSync([1, 2, 3]); + io.File(path).writeAsBytesSync([1, 2, 3]); fileSystem.writeAsBytes(path, data, WriteMode.truncateExisting); - expect(File(path).readAsBytesSync(), data); + expect(io.File(path).readAsBytesSync(), data); }); }); diff --git a/pkgs/io_file/test/write_as_string_test.dart b/pkgs/io_file/test/write_as_string_test.dart index ea7465a3..2151ab75 100644 --- a/pkgs/io_file/test/write_as_string_test.dart +++ b/pkgs/io_file/test/write_as_string_test.dart @@ -6,7 +6,7 @@ library; import 'dart:convert'; -import 'dart:io'; +import 'dart:io' as io; import 'dart:typed_data'; import 'package:io_file/io_file.dart'; @@ -41,14 +41,15 @@ void main() { WriteMode.truncateExisting, ), throwsA( - isA() - .having((e) => e.message, 'message', 'open failed') + isA() .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_ACCESS_DENIED : errors.eisdir, + io.Platform.isWindows + ? win32.ERROR_ACCESS_DENIED + : errors.eisdir, ) - .having((e) => e.path, 'path', tmp), + .having((e) => e.path1, 'path1', tmp), ), ); }); @@ -56,8 +57,8 @@ void main() { test('symlink', () { final filePath = '$tmp/file1'; final symlinkPath = '$tmp/file2'; - File(filePath).writeAsBytesSync(Uint8List(0)); - Link(symlinkPath).createSync(filePath); + io.File(filePath).writeAsBytesSync(Uint8List(0)); + io.Link(symlinkPath).createSync(filePath); fileSystem.writeAsString( symlinkPath, @@ -65,18 +66,18 @@ void main() { WriteMode.truncateExisting, ); - expect(File(symlinkPath).readAsStringSync(), 'Hello World!'); + expect(io.File(symlinkPath).readAsStringSync(), 'Hello World!'); }); group('broken symlink', () { test('failExisting', () { final filePath = '$tmp/file1'; final symlinkPath = '$tmp/file2'; - File(filePath).writeAsBytesSync(Uint8List(0)); - Link(symlinkPath).createSync(filePath); - File(filePath).deleteSync(); + io.File(filePath).writeAsBytesSync(Uint8List(0)); + io.Link(symlinkPath).createSync(filePath); + io.File(filePath).deleteSync(); - if (Platform.isWindows) { + if (io.Platform.isWindows) { // Windows considers a broken symlink to not be an existing file. fileSystem.writeAsString( symlinkPath, @@ -86,8 +87,8 @@ void main() { // Should write at the symlink target, which should also mean that the // symlink is no longer broken. - expect(File(symlinkPath).readAsStringSync(), 'Hello World!'); - expect(File(filePath).readAsStringSync(), 'Hello World!'); + expect(io.File(symlinkPath).readAsStringSync(), 'Hello World!'); + expect(io.File(filePath).readAsStringSync(), 'Hello World!'); } else { expect( () => fileSystem.writeAsString( @@ -97,15 +98,14 @@ void main() { ), throwsA( isA() - .having((e) => e.message, 'message', 'open failed') .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows + io.Platform.isWindows ? win32.ERROR_FILE_EXISTS : errors.eexist, ) - .having((e) => e.path, 'path', symlinkPath), + .having((e) => e.path1, 'path1', symlinkPath), ), ); } @@ -113,9 +113,9 @@ void main() { test('truncateExisting', () { final filePath = '$tmp/file1'; final symlinkPath = '$tmp/file2'; - File(filePath).writeAsBytesSync(Uint8List(0)); - Link(symlinkPath).createSync(filePath); - File(filePath).deleteSync(); + io.File(filePath).writeAsBytesSync(Uint8List(0)); + io.Link(symlinkPath).createSync(filePath); + io.File(filePath).deleteSync(); fileSystem.writeAsString( symlinkPath, @@ -125,8 +125,8 @@ void main() { // Should write at the symlink target, which should also mean that the // symlink is no longer broken. - expect(File(symlinkPath).readAsStringSync(), 'Hello World!'); - expect(File(filePath).readAsStringSync(), 'Hello World!'); + expect(io.File(symlinkPath).readAsStringSync(), 'Hello World!'); + expect(io.File(filePath).readAsStringSync(), 'Hello World!'); }); }); @@ -136,7 +136,7 @@ void main() { fileSystem.writeAsString(path, 'Hello World', WriteMode.appendExisting); - expect(File(path).readAsStringSync(), 'Hello World'); + expect(io.File(path).readAsStringSync(), 'Hello World'); }); test('failExisting', () { @@ -144,7 +144,7 @@ void main() { fileSystem.writeAsString(path, 'Hello World', WriteMode.failExisting); - expect(File(path).readAsStringSync(), 'Hello World'); + expect(io.File(path).readAsStringSync(), 'Hello World'); }); test('truncateExisting', () { @@ -156,14 +156,14 @@ void main() { WriteMode.truncateExisting, ); - expect(File(path).readAsStringSync(), 'Hello World'); + expect(io.File(path).readAsStringSync(), 'Hello World'); }); }); group('existing file', () { test('appendExisting', () { final path = '$tmp/file'; - File(path).writeAsStringSync('message: '); + io.File(path).writeAsStringSync('message: '); fileSystem.writeAsString( path, @@ -171,12 +171,12 @@ void main() { WriteMode.appendExisting, ); - expect(File(path).readAsStringSync(), 'message: Hello World!'); + expect(io.File(path).readAsStringSync(), 'message: Hello World!'); }); test('failExisting', () { final path = '$tmp/file'; - File(path).writeAsBytesSync([1, 2, 3]); + io.File(path).writeAsBytesSync([1, 2, 3]); expect( () => fileSystem.writeAsString( @@ -186,20 +186,21 @@ void main() { ), throwsA( isA() - .having((e) => e.message, 'message', 'open failed') .having( - (e) => e.osError?.errorCode, + (e) => e.errorCode, 'errorCode', - Platform.isWindows ? win32.ERROR_FILE_EXISTS : errors.eexist, + io.Platform.isWindows + ? win32.ERROR_FILE_EXISTS + : errors.eexist, ) - .having((e) => e.path, 'path', path), + .having((e) => e.path1, 'path1', path), ), ); }); test('truncateExisting', () { final path = '$tmp/file'; - File(path).writeAsBytesSync([1, 2, 3]); + io.File(path).writeAsBytesSync([1, 2, 3]); fileSystem.writeAsString( path, @@ -207,7 +208,7 @@ void main() { WriteMode.truncateExisting, ); - expect(File(path).readAsStringSync(), 'Hello World!'); + expect(io.File(path).readAsStringSync(), 'Hello World!'); }); }); @@ -215,14 +216,14 @@ void main() { final path = p.join(tmp, 'f' * 255); fileSystem.writeAsString(path, 'Hello World!'); - expect(File(path).readAsStringSync(), 'Hello World!'); + expect(io.File(path).readAsStringSync(), 'Hello World!'); }); test('relative path, long file name', () { final path = 'f' * 255; fileSystem.writeAsString(path, 'Hello World!'); - expect(File(path).readAsStringSync(), 'Hello World!'); + expect(io.File(path).readAsStringSync(), 'Hello World!'); }); group('encoding', () { @@ -235,7 +236,7 @@ void main() { WriteMode.failExisting, utf8, ); - expect(File(path).readAsStringSync(), 'Γειά σου!'); + expect(io.File(path).readAsStringSync(), 'Γειά σου!'); }); test('unencodable', () { @@ -281,8 +282,8 @@ void main() { ); expect( - File(path).readAsStringSync(), - Platform.isWindows + io.File(path).readAsStringSync(), + io.Platform.isWindows ? 'Greeting:\r\nHello World!\rHi!\r\r\n' : 'Greeting:\nHello World!\rHi!\r\n', ); @@ -300,7 +301,7 @@ void main() { ); expect( - File(path).readAsStringSync(), + io.File(path).readAsStringSync(), 'Greeting:\nHello World!\rHi!\r\n', ); }); @@ -317,7 +318,7 @@ void main() { ); expect( - File(path).readAsStringSync(), + io.File(path).readAsStringSync(), 'Greeting:\r\nHello World!\rHi!\r\r\n', ); });