/
fs.rename.spec.js
229 lines (204 loc) · 6.61 KB
/
fs.rename.spec.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use strict';
const helper = require('../helper');
const fs = require('fs');
const mock = require('../../lib/index');
const bufferFrom = require('../../lib/buffer').from;
const assert = helper.assert;
const withPromise = helper.withPromise;
describe('fs.rename(oldPath, newPath, callback)', function() {
beforeEach(function() {
mock({
'path/to/a.bin': bufferFrom([1, 2, 3]),
empty: {},
nested: {
dir: mock.directory({
mtime: new Date(1),
items: {'file.txt': ''}
})
}
});
});
afterEach(mock.restore);
it('allows files to be renamed', function(done) {
fs.rename('path/to/a.bin', 'path/to/b.bin', function(err) {
assert.isTrue(!err);
assert.isFalse(fs.existsSync('path/to/a.bin'));
assert.isTrue(fs.existsSync('path/to/b.bin'));
done();
});
});
it('supports Buffer input', function(done) {
fs.rename(
Buffer.from('path/to/a.bin'),
Buffer.from('path/to/b.bin'),
function(err) {
assert.isTrue(!err);
assert.isFalse(fs.existsSync('path/to/a.bin'));
assert.isTrue(fs.existsSync('path/to/b.bin'));
done();
}
);
});
withPromise.it('promise allows files to be renamed', function(done) {
fs.promises.rename('path/to/a.bin', 'path/to/b.bin').then(function() {
assert.isFalse(fs.existsSync('path/to/a.bin'));
assert.isTrue(fs.existsSync('path/to/b.bin'));
done();
}, done);
});
it('updates mtime of parent directory', function(done) {
const oldTime = fs.statSync('nested/dir').mtime;
fs.rename('nested/dir/file.txt', 'nested/dir/renamed.txt', function(err) {
assert.isTrue(!err);
assert.isFalse(fs.existsSync('nested/dir/file.txt'));
assert.isTrue(fs.existsSync('nested/dir/renamed.txt'));
const newTime = fs.statSync('nested/dir').mtime;
assert.isTrue(newTime > oldTime);
done();
});
});
withPromise.it('promise updates mtime of parent directory', function(done) {
const oldTime = fs.statSync('nested/dir').mtime;
fs.promises
.rename('nested/dir/file.txt', 'nested/dir/renamed.txt')
.then(function() {
assert.isFalse(fs.existsSync('nested/dir/file.txt'));
assert.isTrue(fs.existsSync('nested/dir/renamed.txt'));
const newTime = fs.statSync('nested/dir').mtime;
assert.isTrue(newTime > oldTime);
done();
}, done);
});
it('calls callback with error if old path does not exist', function(done) {
fs.rename('bogus', 'empty', function(err) {
assert.instanceOf(err, Error);
done();
});
});
withPromise.it(
'promise calls callback with error if old path does not exist',
function(done) {
fs.promises.rename('bogus', 'empty').then(
function() {
assert.fail('Should not succeed.');
done();
},
function(err) {
assert.instanceOf(err, Error);
done();
}
);
}
);
it('overwrites existing files', function(done) {
fs.rename('path/to/a.bin', 'nested/dir/file.txt', function(err) {
assert.isTrue(!err);
assert.isFalse(fs.existsSync('path/to/a.bin'));
assert.isTrue(fs.existsSync('nested/dir/file.txt'));
done();
});
});
withPromise.it('promise overwrites existing files', function(done) {
fs.promises.rename('path/to/a.bin', 'nested/dir/file.txt').then(function() {
assert.isFalse(fs.existsSync('path/to/a.bin'));
assert.isTrue(fs.existsSync('nested/dir/file.txt'));
done();
}, done);
});
it('allows directories to be renamed', function(done) {
fs.rename('path/to', 'path/foo', function(err) {
assert.isTrue(!err);
assert.isFalse(fs.existsSync('path/to'));
assert.isTrue(fs.existsSync('path/foo'));
assert.deepEqual(fs.readdirSync('path/foo'), ['a.bin']);
done();
});
});
withPromise.it('promise allows directories to be renamed', function(done) {
fs.promises.rename('path/to', 'path/foo').then(function() {
assert.isFalse(fs.existsSync('path/to'));
assert.isTrue(fs.existsSync('path/foo'));
assert.deepEqual(fs.readdirSync('path/foo'), ['a.bin']);
done();
}, done);
});
it('calls callback with error if new directory not empty', function(done) {
fs.rename('path', 'nested', function(err) {
assert.instanceOf(err, Error);
done();
});
});
withPromise.it(
'promise calls callback with error if new directory not empty',
function(done) {
fs.promises.rename('path', 'nested').then(
function() {
assert.fail('Should not succeed.');
done();
},
function(err) {
assert.instanceOf(err, Error);
done();
}
);
}
);
});
describe('fs.renameSync(oldPath, newPath)', function() {
beforeEach(function() {
mock({
'path/to/a.bin': bufferFrom([1, 2, 3]),
empty: {},
nested: {
dir: {
'file.txt': ''
}
},
link: mock.symlink({path: './path/to/a.bin'})
});
});
afterEach(mock.restore);
it('allows files to be renamed', function() {
fs.renameSync('path/to/a.bin', 'path/to/b.bin');
assert.isFalse(fs.existsSync('path/to/a.bin'));
assert.isTrue(fs.existsSync('path/to/b.bin'));
});
it('overwrites existing files', function() {
fs.renameSync('path/to/a.bin', 'nested/dir/file.txt');
assert.isFalse(fs.existsSync('path/to/a.bin'));
assert.isTrue(fs.existsSync('nested/dir/file.txt'));
});
it('allows directories to be renamed', function() {
fs.renameSync('path/to', 'path/foo');
assert.isFalse(fs.existsSync('path/to'));
assert.isTrue(fs.existsSync('path/foo'));
assert.deepEqual(fs.readdirSync('path/foo'), ['a.bin']);
});
it('replaces existing directories (if empty)', function() {
fs.renameSync('path/to', 'empty');
assert.isFalse(fs.existsSync('path/to'));
assert.isTrue(fs.existsSync('empty'));
assert.deepEqual(fs.readdirSync('empty'), ['a.bin']);
});
it('renames symbolic links', function() {
fs.renameSync('link', 'renamed');
assert.isTrue(fs.existsSync('renamed'));
assert.isFalse(fs.existsSync('link'));
assert.isTrue(fs.existsSync('path/to/a.bin'));
});
it('throws if old path does not exist', function() {
assert.throws(function() {
fs.renameSync('bogus', 'empty');
});
});
it('throws if new path basename is not directory', function() {
assert.throws(function() {
fs.renameSync('path/to/a.bin', 'bogus/a.bin');
});
});
it('throws if new dir is not empty dir', function() {
assert.throws(function() {
fs.renameSync('path/to', 'nested');
});
});
});