|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const vfs = require('node:vfs'); |
| 6 | + |
| 7 | +// Test vfs-mount event |
| 8 | +{ |
| 9 | + const mountEvents = []; |
| 10 | + const mountListener = common.mustCall((info) => { |
| 11 | + mountEvents.push(info); |
| 12 | + }); |
| 13 | + |
| 14 | + process.on('vfs-mount', mountListener); |
| 15 | + |
| 16 | + const myVfs = vfs.create(); |
| 17 | + myVfs.writeFileSync('/test.txt', 'hello'); |
| 18 | + myVfs.mount('/virtual'); |
| 19 | + |
| 20 | + assert.strictEqual(mountEvents.length, 1); |
| 21 | + assert.strictEqual(mountEvents[0].mountPoint, '/virtual'); |
| 22 | + assert.strictEqual(mountEvents[0].overlay, false); |
| 23 | + assert.strictEqual(mountEvents[0].readonly, false); |
| 24 | + |
| 25 | + myVfs.unmount(); |
| 26 | + process.removeListener('vfs-mount', mountListener); |
| 27 | +} |
| 28 | + |
| 29 | +// Test vfs-unmount event |
| 30 | +{ |
| 31 | + const unmountEvents = []; |
| 32 | + const unmountListener = common.mustCall((info) => { |
| 33 | + unmountEvents.push(info); |
| 34 | + }); |
| 35 | + |
| 36 | + process.on('vfs-unmount', unmountListener); |
| 37 | + |
| 38 | + const myVfs = vfs.create(); |
| 39 | + myVfs.writeFileSync('/test.txt', 'hello'); |
| 40 | + myVfs.mount('/virtual'); |
| 41 | + myVfs.unmount(); |
| 42 | + |
| 43 | + assert.strictEqual(unmountEvents.length, 1); |
| 44 | + assert.strictEqual(unmountEvents[0].mountPoint, '/virtual'); |
| 45 | + assert.strictEqual(unmountEvents[0].overlay, false); |
| 46 | + assert.strictEqual(unmountEvents[0].readonly, false); |
| 47 | + |
| 48 | + process.removeListener('vfs-unmount', unmountListener); |
| 49 | +} |
| 50 | + |
| 51 | +// Test events with overlay mode |
| 52 | +{ |
| 53 | + const mountEvents = []; |
| 54 | + const unmountEvents = []; |
| 55 | + |
| 56 | + const mountListener = common.mustCall((info) => { |
| 57 | + mountEvents.push(info); |
| 58 | + }); |
| 59 | + const unmountListener = common.mustCall((info) => { |
| 60 | + unmountEvents.push(info); |
| 61 | + }); |
| 62 | + |
| 63 | + process.on('vfs-mount', mountListener); |
| 64 | + process.on('vfs-unmount', unmountListener); |
| 65 | + |
| 66 | + const myVfs = vfs.create({ overlay: true }); |
| 67 | + myVfs.writeFileSync('/test.txt', 'hello'); |
| 68 | + myVfs.mount('/'); |
| 69 | + |
| 70 | + assert.strictEqual(mountEvents.length, 1); |
| 71 | + assert.strictEqual(mountEvents[0].mountPoint, '/'); |
| 72 | + assert.strictEqual(mountEvents[0].overlay, true); |
| 73 | + |
| 74 | + myVfs.unmount(); |
| 75 | + |
| 76 | + assert.strictEqual(unmountEvents.length, 1); |
| 77 | + assert.strictEqual(unmountEvents[0].overlay, true); |
| 78 | + |
| 79 | + process.removeListener('vfs-mount', mountListener); |
| 80 | + process.removeListener('vfs-unmount', unmountListener); |
| 81 | +} |
| 82 | + |
| 83 | +// Test events with readonly mode |
| 84 | +{ |
| 85 | + const mountEvents = []; |
| 86 | + |
| 87 | + const mountListener = common.mustCall((info) => { |
| 88 | + mountEvents.push(info); |
| 89 | + }); |
| 90 | + |
| 91 | + process.on('vfs-mount', mountListener); |
| 92 | + |
| 93 | + const myVfs = vfs.create(); |
| 94 | + myVfs.writeFileSync('/test.txt', 'hello'); |
| 95 | + myVfs.provider.setReadOnly(); |
| 96 | + myVfs.mount('/virtual'); |
| 97 | + |
| 98 | + assert.strictEqual(mountEvents.length, 1); |
| 99 | + assert.strictEqual(mountEvents[0].readonly, true); |
| 100 | + |
| 101 | + myVfs.unmount(); |
| 102 | + process.removeListener('vfs-mount', mountListener); |
| 103 | +} |
| 104 | + |
| 105 | +// Test that unmount on non-mounted VFS does not emit event |
| 106 | +{ |
| 107 | + const unmountEvents = []; |
| 108 | + const unmountListener = (info) => { |
| 109 | + unmountEvents.push(info); |
| 110 | + }; |
| 111 | + |
| 112 | + process.on('vfs-unmount', unmountListener); |
| 113 | + |
| 114 | + const myVfs = vfs.create(); |
| 115 | + myVfs.unmount(); // Unmount without mounting first |
| 116 | + |
| 117 | + assert.strictEqual(unmountEvents.length, 0); |
| 118 | + |
| 119 | + process.removeListener('vfs-unmount', unmountListener); |
| 120 | +} |
| 121 | + |
| 122 | +// Test using declaration triggers unmount event |
| 123 | +{ |
| 124 | + const unmountEvents = []; |
| 125 | + const unmountListener = common.mustCall((info) => { |
| 126 | + unmountEvents.push(info); |
| 127 | + }); |
| 128 | + |
| 129 | + process.on('vfs-unmount', unmountListener); |
| 130 | + |
| 131 | + { |
| 132 | + using myVfs = vfs.create(); |
| 133 | + myVfs.writeFileSync('/test.txt', 'hello'); |
| 134 | + myVfs.mount('/virtual'); |
| 135 | + } // VFS is automatically unmounted here via Symbol.dispose |
| 136 | + |
| 137 | + assert.strictEqual(unmountEvents.length, 1); |
| 138 | + assert.strictEqual(unmountEvents[0].mountPoint, '/virtual'); |
| 139 | + |
| 140 | + process.removeListener('vfs-unmount', unmountListener); |
| 141 | +} |
0 commit comments