|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +@TestOn('vm') |
| 6 | +library; |
| 7 | + |
| 8 | +import 'dart:io' as io; |
| 9 | + |
| 10 | +import 'package:io_file/io_file.dart'; |
| 11 | +import 'package:io_file/src/vm_windows_file_system.dart'; |
| 12 | +import 'package:path/path.dart' as p; |
| 13 | +import 'package:test/test.dart'; |
| 14 | +import 'package:win32/win32.dart' as win32; |
| 15 | + |
| 16 | +import 'errors.dart' as errors; |
| 17 | +import 'file_system_file_utils.dart' hide fileUtils; |
| 18 | +import 'test_utils.dart'; |
| 19 | + |
| 20 | +void tests(FileUtils utils, FileSystem fs) { |
| 21 | + late String tmp; |
| 22 | + late String cwd; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + tmp = utils.createTestDirectory('removeFile'); |
| 26 | + cwd = fs.currentDirectory; |
| 27 | + fs.currentDirectory = tmp; |
| 28 | + }); |
| 29 | + |
| 30 | + tearDown(() { |
| 31 | + fs.currentDirectory = cwd; |
| 32 | + utils.deleteDirectoryTree(tmp); |
| 33 | + }); |
| 34 | + |
| 35 | + test('success', () { |
| 36 | + final path = '$tmp/file'; |
| 37 | + utils.createTextFile(path, 'Hello World!'); |
| 38 | + |
| 39 | + fileSystem.removeFile(path); |
| 40 | + |
| 41 | + expect(utils.exists(path), isFalse, reason: '$path exists'); |
| 42 | + }); |
| 43 | + |
| 44 | + test('absolute path, long directory name', () { |
| 45 | + // On Windows: |
| 46 | + // When using an API to create a directory, the specified path cannot be |
| 47 | + // so long that you cannot append an 8.3 file name (that is, the directory |
| 48 | + // name cannot exceed MAX_PATH minus 12). |
| 49 | + final dirname = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); |
| 50 | + final path = p.join(tmp, dirname); |
| 51 | + utils.createTextFile(path, 'Hello World!'); |
| 52 | + |
| 53 | + fileSystem.removeFile(path); |
| 54 | + |
| 55 | + expect(utils.exists(path), isFalse, reason: '$path exists'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('relative path, long directory name', () { |
| 59 | + // On Windows: |
| 60 | + // When using an API to create a directory, the specified path cannot be |
| 61 | + // so long that you cannot append an 8.3 file name (that is, the directory |
| 62 | + // name cannot exceed MAX_PATH minus 12). |
| 63 | + final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255); |
| 64 | + utils.createTextFile(path, 'Hello World!'); |
| 65 | + |
| 66 | + fileSystem.removeFile(path); |
| 67 | + |
| 68 | + expect(utils.exists(path), isFalse, reason: '$path exists'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('directory', () { |
| 72 | + final path = p.join(tmp, 'dir'); |
| 73 | + utils.createDirectory(path); |
| 74 | + |
| 75 | + expect( |
| 76 | + () => fileSystem.removeFile(path), |
| 77 | + throwsA( |
| 78 | + isA<IOFileException>().having( |
| 79 | + (e) => e.errorCode, |
| 80 | + 'errorCode', |
| 81 | + fileSystem is WindowsFileSystem |
| 82 | + ? win32.ERROR_ACCESS_DENIED |
| 83 | + : anyOf( |
| 84 | + errors.eperm, // POSIX |
| 85 | + errors.eisdir, // Linux |
| 86 | + ), |
| 87 | + ), |
| 88 | + ), |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + test('link to directory', () { |
| 93 | + final dirPath = p.join(tmp, 'dir'); |
| 94 | + final linkPath = p.join(tmp, 'link'); |
| 95 | + utils.createDirectory(dirPath); |
| 96 | + io.Link(linkPath).createSync(dirPath); |
| 97 | + |
| 98 | + if (fs is WindowsFileSystem) { |
| 99 | + expect( |
| 100 | + () => fileSystem.removeFile(linkPath), |
| 101 | + throwsA( |
| 102 | + isA<PathAccessException>().having( |
| 103 | + (e) => e.errorCode, |
| 104 | + 'errorCode', |
| 105 | + win32.ERROR_ACCESS_DENIED, |
| 106 | + ), |
| 107 | + ), |
| 108 | + ); |
| 109 | + expect(utils.exists(dirPath), isTrue, reason: '$dirPath does not exist'); |
| 110 | + expect( |
| 111 | + utils.exists(linkPath), |
| 112 | + isTrue, |
| 113 | + reason: '$linkPath does not exist', |
| 114 | + ); |
| 115 | + } else { |
| 116 | + fileSystem.removeFile(linkPath); |
| 117 | + |
| 118 | + expect(utils.exists(dirPath), isTrue, reason: '$dirPath does not exist'); |
| 119 | + expect(utils.exists(linkPath), isFalse, reason: '$linkPath exists'); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + test('link to file', () { |
| 124 | + final filePath = p.join(tmp, 'file'); |
| 125 | + final linkPath = p.join(tmp, 'link'); |
| 126 | + utils.createTextFile(filePath, 'Hello World!'); |
| 127 | + io.Link(linkPath).createSync(filePath); |
| 128 | + |
| 129 | + fileSystem.removeFile(linkPath); |
| 130 | + |
| 131 | + expect(utils.exists(filePath), isTrue, reason: '$filePath does not exist'); |
| 132 | + expect(utils.exists(linkPath), isFalse, reason: '$linkPath exists'); |
| 133 | + }); |
| 134 | + |
| 135 | + test('path does not exist', () { |
| 136 | + final path = '$tmp/file'; |
| 137 | + |
| 138 | + expect( |
| 139 | + () => fileSystem.removeFile(path), |
| 140 | + throwsA( |
| 141 | + isA<PathNotFoundException>().having( |
| 142 | + (e) => e.errorCode, |
| 143 | + 'errorCode', |
| 144 | + fileSystem is WindowsFileSystem |
| 145 | + ? win32.ERROR_FILE_NOT_FOUND |
| 146 | + : errors.enoent, |
| 147 | + ), |
| 148 | + ), |
| 149 | + ); |
| 150 | + }); |
| 151 | +} |
| 152 | + |
| 153 | +void main() { |
| 154 | + group('removeFile', () { |
| 155 | + group('dart:io verification', () => tests(fileUtils(), fileSystem)); |
| 156 | + group( |
| 157 | + 'self verification', |
| 158 | + () => tests(FileSystemFileUtils(fileSystem), fileSystem), |
| 159 | + ); |
| 160 | + }); |
| 161 | +} |
0 commit comments