|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const fs = require('fs'); |
| 6 | + |
| 7 | +// Test copyFileSync |
| 8 | +{ |
| 9 | + const myVfs = fs.createVirtual(); |
| 10 | + myVfs.writeFileSync('/source.txt', 'original content'); |
| 11 | + |
| 12 | + myVfs.copyFileSync('/source.txt', '/dest.txt'); |
| 13 | + assert.strictEqual(myVfs.readFileSync('/dest.txt', 'utf8'), 'original content'); |
| 14 | + |
| 15 | + // Source file should still exist |
| 16 | + assert.strictEqual(myVfs.existsSync('/source.txt'), true); |
| 17 | + |
| 18 | + // Test copying to nested path |
| 19 | + myVfs.mkdirSync('/nested/dir', { recursive: true }); |
| 20 | + myVfs.copyFileSync('/source.txt', '/nested/dir/copy.txt'); |
| 21 | + assert.strictEqual(myVfs.readFileSync('/nested/dir/copy.txt', 'utf8'), 'original content'); |
| 22 | + |
| 23 | + // Test copying non-existent file |
| 24 | + assert.throws(() => { |
| 25 | + myVfs.copyFileSync('/nonexistent.txt', '/fail.txt'); |
| 26 | + }, { code: 'ENOENT' }); |
| 27 | +} |
| 28 | + |
| 29 | +// Test async copyFile |
| 30 | +(async () => { |
| 31 | + const myVfs = fs.createVirtual(); |
| 32 | + myVfs.writeFileSync('/async-source.txt', 'async content'); |
| 33 | + |
| 34 | + await myVfs.promises.copyFile('/async-source.txt', '/async-dest.txt'); |
| 35 | + assert.strictEqual(myVfs.readFileSync('/async-dest.txt', 'utf8'), 'async content'); |
| 36 | + |
| 37 | + // Test copying non-existent file |
| 38 | + await assert.rejects( |
| 39 | + myVfs.promises.copyFile('/nonexistent.txt', '/fail.txt'), |
| 40 | + { code: 'ENOENT' } |
| 41 | + ); |
| 42 | +})().then(common.mustCall()); |
| 43 | + |
| 44 | +// Test copyFileSync with mode argument |
| 45 | +{ |
| 46 | + const myVfs = fs.createVirtual(); |
| 47 | + myVfs.writeFileSync('/src-mode.txt', 'mode content'); |
| 48 | + |
| 49 | + // copyFileSync also accepts a mode argument (ignored for VFS but tests the code path) |
| 50 | + myVfs.copyFileSync('/src-mode.txt', '/dest-mode.txt', 0); |
| 51 | + assert.strictEqual(myVfs.readFileSync('/dest-mode.txt', 'utf8'), 'mode content'); |
| 52 | +} |
| 53 | + |
| 54 | +// Test appendFileSync |
| 55 | +{ |
| 56 | + const myVfs = fs.createVirtual(); |
| 57 | + myVfs.writeFileSync('/append.txt', 'hello'); |
| 58 | + |
| 59 | + myVfs.appendFileSync('/append.txt', ' world'); |
| 60 | + assert.strictEqual(myVfs.readFileSync('/append.txt', 'utf8'), 'hello world'); |
| 61 | + |
| 62 | + // Append more |
| 63 | + myVfs.appendFileSync('/append.txt', '!'); |
| 64 | + assert.strictEqual(myVfs.readFileSync('/append.txt', 'utf8'), 'hello world!'); |
| 65 | + |
| 66 | + // Append to non-existent file creates it |
| 67 | + myVfs.appendFileSync('/newfile.txt', 'new content'); |
| 68 | + assert.strictEqual(myVfs.readFileSync('/newfile.txt', 'utf8'), 'new content'); |
| 69 | +} |
| 70 | + |
| 71 | +// Test async appendFile |
| 72 | +(async () => { |
| 73 | + const myVfs = fs.createVirtual(); |
| 74 | + myVfs.writeFileSync('/async-append.txt', 'start'); |
| 75 | + |
| 76 | + await myVfs.promises.appendFile('/async-append.txt', '-end'); |
| 77 | + assert.strictEqual(myVfs.readFileSync('/async-append.txt', 'utf8'), 'start-end'); |
| 78 | +})().then(common.mustCall()); |
| 79 | + |
| 80 | +// Test appendFileSync with Buffer |
| 81 | +{ |
| 82 | + const myVfs = fs.createVirtual(); |
| 83 | + myVfs.writeFileSync('/buffer-append.txt', Buffer.from('start')); |
| 84 | + |
| 85 | + myVfs.appendFileSync('/buffer-append.txt', Buffer.from('-buffer')); |
| 86 | + assert.strictEqual(myVfs.readFileSync('/buffer-append.txt', 'utf8'), 'start-buffer'); |
| 87 | +} |
| 88 | + |
| 89 | +// Test MemoryProvider readonly mode |
| 90 | +{ |
| 91 | + const myVfs = fs.createVirtual(); |
| 92 | + myVfs.writeFileSync('/file.txt', 'content'); |
| 93 | + myVfs.mkdirSync('/dir', { recursive: true }); |
| 94 | + |
| 95 | + // Set to readonly |
| 96 | + myVfs.provider.setReadOnly(); |
| 97 | + assert.strictEqual(myVfs.provider.readonly, true); |
| 98 | + |
| 99 | + // Read operations should still work |
| 100 | + assert.strictEqual(myVfs.readFileSync('/file.txt', 'utf8'), 'content'); |
| 101 | + assert.strictEqual(myVfs.existsSync('/file.txt'), true); |
| 102 | + assert.ok(myVfs.statSync('/file.txt')); |
| 103 | + |
| 104 | + // Write operations should throw EROFS |
| 105 | + assert.throws(() => { |
| 106 | + myVfs.writeFileSync('/file.txt', 'new content'); |
| 107 | + }, { code: 'EROFS' }); |
| 108 | + |
| 109 | + assert.throws(() => { |
| 110 | + myVfs.writeFileSync('/new.txt', 'content'); |
| 111 | + }, { code: 'EROFS' }); |
| 112 | + |
| 113 | + assert.throws(() => { |
| 114 | + myVfs.appendFileSync('/file.txt', 'more'); |
| 115 | + }, { code: 'EROFS' }); |
| 116 | + |
| 117 | + assert.throws(() => { |
| 118 | + myVfs.mkdirSync('/newdir'); |
| 119 | + }, { code: 'EROFS' }); |
| 120 | + |
| 121 | + assert.throws(() => { |
| 122 | + myVfs.unlinkSync('/file.txt'); |
| 123 | + }, { code: 'EROFS' }); |
| 124 | + |
| 125 | + assert.throws(() => { |
| 126 | + myVfs.rmdirSync('/dir'); |
| 127 | + }, { code: 'EROFS' }); |
| 128 | + |
| 129 | + assert.throws(() => { |
| 130 | + myVfs.renameSync('/file.txt', '/renamed.txt'); |
| 131 | + }, { code: 'EROFS' }); |
| 132 | + |
| 133 | + assert.throws(() => { |
| 134 | + myVfs.copyFileSync('/file.txt', '/copy.txt'); |
| 135 | + }, { code: 'EROFS' }); |
| 136 | + |
| 137 | + assert.throws(() => { |
| 138 | + myVfs.symlinkSync('/file.txt', '/link'); |
| 139 | + }, { code: 'EROFS' }); |
| 140 | +} |
| 141 | + |
| 142 | +// Test async operations on readonly VFS |
| 143 | +(async () => { |
| 144 | + const myVfs = fs.createVirtual(); |
| 145 | + myVfs.writeFileSync('/readonly.txt', 'content'); |
| 146 | + myVfs.provider.setReadOnly(); |
| 147 | + |
| 148 | + await assert.rejects( |
| 149 | + myVfs.promises.writeFile('/readonly.txt', 'new'), |
| 150 | + { code: 'EROFS' } |
| 151 | + ); |
| 152 | + |
| 153 | + await assert.rejects( |
| 154 | + myVfs.promises.appendFile('/readonly.txt', 'more'), |
| 155 | + { code: 'EROFS' } |
| 156 | + ); |
| 157 | + |
| 158 | + await assert.rejects( |
| 159 | + myVfs.promises.mkdir('/newdir'), |
| 160 | + { code: 'EROFS' } |
| 161 | + ); |
| 162 | + |
| 163 | + await assert.rejects( |
| 164 | + myVfs.promises.unlink('/readonly.txt'), |
| 165 | + { code: 'EROFS' } |
| 166 | + ); |
| 167 | + |
| 168 | + await assert.rejects( |
| 169 | + myVfs.promises.copyFile('/readonly.txt', '/copy.txt'), |
| 170 | + { code: 'EROFS' } |
| 171 | + ); |
| 172 | +})().then(common.mustCall()); |
| 173 | + |
| 174 | +// Test accessSync |
| 175 | +{ |
| 176 | + const myVfs = fs.createVirtual(); |
| 177 | + myVfs.writeFileSync('/access-test.txt', 'content'); |
| 178 | + |
| 179 | + // Should not throw for existing file |
| 180 | + myVfs.accessSync('/access-test.txt'); |
| 181 | + |
| 182 | + // Should throw for non-existent file |
| 183 | + assert.throws(() => { |
| 184 | + myVfs.accessSync('/nonexistent.txt'); |
| 185 | + }, { code: 'ENOENT' }); |
| 186 | +} |
0 commit comments