|
| 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('posix') |
| 6 | +library; |
| 7 | + |
| 8 | +import 'dart:io'; |
| 9 | +import 'dart:typed_data'; |
| 10 | + |
| 11 | +import 'package:io_file/io_file.dart'; |
| 12 | +import 'package:stdlibc/stdlibc.dart' as stdlibc; |
| 13 | +import 'package:test/test.dart'; |
| 14 | + |
| 15 | +import 'test_utils.dart'; |
| 16 | + |
| 17 | +void main() { |
| 18 | + //TODO(brianquinlan): test with a very long path. |
| 19 | + |
| 20 | + group('writeAsBytes', () { |
| 21 | + late String tmp; |
| 22 | + |
| 23 | + setUp(() => tmp = createTemp('writeAsBytes')); |
| 24 | + |
| 25 | + tearDown(() => deleteTemp(tmp)); |
| 26 | + |
| 27 | + test('directory', () { |
| 28 | + expect( |
| 29 | + () => fileSystem.writeAsBytes( |
| 30 | + tmp, |
| 31 | + Uint8List(5), |
| 32 | + WriteMode.truncateExisting, |
| 33 | + ), |
| 34 | + throwsA( |
| 35 | + isA<FileSystemException>() |
| 36 | + .having((e) => e.message, 'message', 'open failed') |
| 37 | + .having((e) => e.osError?.errorCode, 'errorCode', stdlibc.EISDIR) |
| 38 | + .having((e) => e.path, 'path', tmp), |
| 39 | + ), |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + test('symlink', () { |
| 44 | + final filePath = '$tmp/file1'; |
| 45 | + final symlinkPath = '$tmp/file2'; |
| 46 | + final data = randomUint8List(20); |
| 47 | + File(filePath).writeAsBytesSync(Uint8List(0)); |
| 48 | + Link(symlinkPath).createSync(filePath); |
| 49 | + |
| 50 | + fileSystem.writeAsBytes(symlinkPath, data, WriteMode.truncateExisting); |
| 51 | + |
| 52 | + expect(fileSystem.readAsBytes(symlinkPath), data); |
| 53 | + }); |
| 54 | + |
| 55 | + group('broken symlink', () { |
| 56 | + test('failExisting', () { |
| 57 | + final filePath = '$tmp/file1'; |
| 58 | + final symlinkPath = '$tmp/file2'; |
| 59 | + final data = randomUint8List(20); |
| 60 | + File(filePath).writeAsBytesSync(Uint8List(0)); |
| 61 | + Link(symlinkPath).createSync(filePath); |
| 62 | + File(filePath).deleteSync(); |
| 63 | + |
| 64 | + expect( |
| 65 | + () => fileSystem.writeAsBytes( |
| 66 | + symlinkPath, |
| 67 | + Uint8List.fromList(data), |
| 68 | + WriteMode.failExisting, |
| 69 | + ), |
| 70 | + throwsA( |
| 71 | + isA<PathExistsException>() |
| 72 | + .having((e) => e.message, 'message', 'open failed') |
| 73 | + .having( |
| 74 | + (e) => e.osError?.errorCode, |
| 75 | + 'errorCode', |
| 76 | + stdlibc.EEXIST, |
| 77 | + ) |
| 78 | + .having((e) => e.path, 'path', symlinkPath), |
| 79 | + ), |
| 80 | + ); |
| 81 | + }); |
| 82 | + test('truncateExisting', () { |
| 83 | + final filePath = '$tmp/file1'; |
| 84 | + final symlinkPath = '$tmp/file2'; |
| 85 | + final data = randomUint8List(20); |
| 86 | + File(filePath).writeAsBytesSync(Uint8List(0)); |
| 87 | + Link(symlinkPath).createSync(filePath); |
| 88 | + File(filePath).deleteSync(); |
| 89 | + |
| 90 | + fileSystem.writeAsBytes(symlinkPath, data, WriteMode.truncateExisting); |
| 91 | + |
| 92 | + // Should write at the symlink target, which should also mean that the |
| 93 | + // symlink is no longer broken. |
| 94 | + expect(fileSystem.readAsBytes(symlinkPath), data); |
| 95 | + expect(fileSystem.readAsBytes(filePath), data); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + group('new file', () { |
| 100 | + test('appendExisting', () { |
| 101 | + final data = randomUint8List(20); |
| 102 | + final path = '$tmp/file'; |
| 103 | + |
| 104 | + fileSystem.writeAsBytes( |
| 105 | + path, |
| 106 | + Uint8List.fromList(data), |
| 107 | + WriteMode.appendExisting, |
| 108 | + ); |
| 109 | + |
| 110 | + expect(File(path).readAsBytesSync(), data); |
| 111 | + }); |
| 112 | + |
| 113 | + test('failExisting', () { |
| 114 | + final data = randomUint8List(20); |
| 115 | + final path = '$tmp/file'; |
| 116 | + |
| 117 | + fileSystem.writeAsBytes( |
| 118 | + path, |
| 119 | + Uint8List.fromList(data), |
| 120 | + WriteMode.failExisting, |
| 121 | + ); |
| 122 | + |
| 123 | + expect(File(path).readAsBytesSync(), data); |
| 124 | + }); |
| 125 | + |
| 126 | + test('truncateExisting', () { |
| 127 | + final data = randomUint8List(20); |
| 128 | + final path = '$tmp/file'; |
| 129 | + |
| 130 | + fileSystem.writeAsBytes( |
| 131 | + path, |
| 132 | + Uint8List.fromList(data), |
| 133 | + WriteMode.truncateExisting, |
| 134 | + ); |
| 135 | + |
| 136 | + expect(File(path).readAsBytesSync(), data); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + group('existing file', () { |
| 141 | + test('appendExisting', () { |
| 142 | + final data = randomUint8List(20); |
| 143 | + final path = '$tmp/file'; |
| 144 | + File(path).writeAsBytesSync([1, 2, 3]); |
| 145 | + |
| 146 | + fileSystem.writeAsBytes( |
| 147 | + path, |
| 148 | + Uint8List.fromList(data), |
| 149 | + WriteMode.appendExisting, |
| 150 | + ); |
| 151 | + |
| 152 | + expect(File(path).readAsBytesSync(), [1, 2, 3] + data); |
| 153 | + }); |
| 154 | + |
| 155 | + test('failExisting', () { |
| 156 | + final data = randomUint8List(20); |
| 157 | + final path = '$tmp/file'; |
| 158 | + File(path).writeAsBytesSync([1, 2, 3]); |
| 159 | + |
| 160 | + expect( |
| 161 | + () => fileSystem.writeAsBytes(path, data, WriteMode.failExisting), |
| 162 | + throwsA( |
| 163 | + isA<PathExistsException>() |
| 164 | + .having((e) => e.message, 'message', 'open failed') |
| 165 | + .having( |
| 166 | + (e) => e.osError?.errorCode, |
| 167 | + 'errorCode', |
| 168 | + stdlibc.EEXIST, |
| 169 | + ) |
| 170 | + .having((e) => e.path, 'path', path), |
| 171 | + ), |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + test('truncateExisting', () { |
| 176 | + final data = randomUint8List(20); |
| 177 | + final path = '$tmp/file'; |
| 178 | + File(path).writeAsBytesSync([1, 2, 3]); |
| 179 | + |
| 180 | + fileSystem.writeAsBytes(path, data, WriteMode.truncateExisting); |
| 181 | + |
| 182 | + expect(File(path).readAsBytesSync(), data); |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + group('regular files', () { |
| 187 | + for (var i = 0; i <= 1024; ++i) { |
| 188 | + test('Write small file: $i bytes', () { |
| 189 | + final data = randomUint8List(i); |
| 190 | + final path = '$tmp/file'; |
| 191 | + |
| 192 | + fileSystem.writeAsBytes(path, data); |
| 193 | + expect(fileSystem.readAsBytes(path), data); |
| 194 | + }); |
| 195 | + } |
| 196 | + |
| 197 | + for (var i = 1 << 12; i <= 1 << 30; i <<= 4) { |
| 198 | + test('Write large file: $i bytes', () { |
| 199 | + final data = randomUint8List(i); |
| 200 | + final path = '$tmp/file'; |
| 201 | + |
| 202 | + fileSystem.writeAsBytes(path, data); |
| 203 | + expect(fileSystem.readAsBytes(path), data); |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | + test('Write very large file', () { |
| 208 | + // FreeBSD cannot write more than INT_MAX at once. |
| 209 | + final data = randomUint8List(1 << 31 + 1); |
| 210 | + final path = '$tmp/file'; |
| 211 | + |
| 212 | + fileSystem.writeAsBytes(path, data); |
| 213 | + expect(fileSystem.readAsBytes(path), data); |
| 214 | + }, skip: 'very slow'); |
| 215 | + }); |
| 216 | + }); |
| 217 | +} |
0 commit comments