Repository navigation
Expand file tree
/
Copy pathtest-vfs-mount-errors.js
More file actions
141 lines (126 loc) · 3.8 KB
/
Copy pathtest-vfs-mount-errors.js
File metadata and controls
141 lines (126 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Flags: --experimental-vfs --expose-internals
'use strict';
// Error paths in the VFS mount layer:
// - EXDEV when renaming/linking across different VFS instances or VFS<->real
// - last-unmount handler cleanup (vfsState.handlers becomes null again)
// - namespace isolation between layers
require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const os = require('os');
const vfs = require('node:vfs');
const { vfsState } = require('internal/fs/utils');
// EXDEV: rename across two different VFS instances
{
const a = vfs.create();
const b = vfs.create();
a.writeFileSync('/file.txt', 'a');
b.mkdirSync('/x', { recursive: true });
const mountA = a.mount();
const mountB = b.mount();
assert.throws(
() => fs.renameSync(path.join(mountA, 'file.txt'),
path.join(mountB, 'x/file.txt')),
{ code: 'EXDEV' },
);
a.unmount();
b.unmount();
}
// EXDEV: copyFileSync across two different VFS instances
{
const a = vfs.create();
const b = vfs.create();
a.writeFileSync('/file.txt', 'a');
b.mkdirSync('/x', { recursive: true });
const mountA = a.mount();
const mountB = b.mount();
assert.throws(
() => fs.copyFileSync(path.join(mountA, 'file.txt'),
path.join(mountB, 'x/copy.txt')),
{ code: 'EXDEV' },
);
a.unmount();
b.unmount();
}
// EXDEV: linkSync across two different VFS instances
{
const a = vfs.create();
const b = vfs.create();
a.writeFileSync('/file.txt', 'a');
b.mkdirSync('/x', { recursive: true });
const mountA = a.mount();
const mountB = b.mount();
assert.throws(
() => fs.linkSync(path.join(mountA, 'file.txt'),
path.join(mountB, 'x/lk.txt')),
{ code: 'EXDEV' },
);
a.unmount();
b.unmount();
}
// EXDEV: rename from VFS to a real-fs path
{
const a = vfs.create();
a.writeFileSync('/file.txt', 'a');
const mountA = a.mount();
const tmpReal = '/tmp/vfs-mount-real-' + process.pid + '.txt';
assert.throws(
() => fs.renameSync(path.join(mountA, 'file.txt'), tmpReal),
{ code: 'EXDEV' },
);
a.unmount();
}
// Handler cleanup: after last unmount, vfsState.handlers returns to null
{
assert.strictEqual(vfsState.handlers, null);
const x = vfs.create();
x.mount();
assert.notStrictEqual(vfsState.handlers, null);
x.unmount();
assert.strictEqual(vfsState.handlers, null);
// And it re-installs on a subsequent mount
const y = vfs.create();
y.mount();
assert.notStrictEqual(vfsState.handlers, null);
y.unmount();
assert.strictEqual(vfsState.handlers, null);
}
// Two parallel mounts both register, last-out clears handlers
{
const a = vfs.create();
const b = vfs.create();
a.mount();
b.mount();
assert.notStrictEqual(vfsState.handlers, null);
a.unmount();
assert.notStrictEqual(vfsState.handlers, null);
b.unmount();
assert.strictEqual(vfsState.handlers, null);
}
// Namespace isolation: a path in an unmounted (or never-mounted) layer
// namespace is not served by other active layers.
{
const a = vfs.create();
const b = vfs.create();
a.writeFileSync('/f.txt', 'a');
b.writeFileSync('/f.txt', 'b');
const mountA = a.mount();
const mountB = b.mount();
assert.notStrictEqual(mountA, mountB);
b.unmount();
// B's namespace is dead even though A is still mounted.
assert.strictEqual(fs.existsSync(path.join(mountB, 'f.txt')), false);
assert.strictEqual(fs.readFileSync(path.join(mountA, 'f.txt'), 'utf8'), 'a');
// A malformed layer segment under the VFS root is not served either.
const bogus = path.join(os.devNull, 'vfs', 'not-a-number', 'f.txt');
assert.strictEqual(fs.existsSync(bogus), false);
a.unmount();
}
// Double-mount of same instance rejected
{
const a = vfs.create();
a.mount();
assert.throws(() => a.mount(), { code: 'ERR_INVALID_STATE' });
a.unmount();
}