/
fs.readlink.spec.js
85 lines (74 loc) · 1.92 KB
/
fs.readlink.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
'use strict';
const helper = require('../helper');
const fs = require('fs');
const mock = require('../../lib/index');
const assert = helper.assert;
const withPromise = helper.withPromise;
describe('fs.readlink(path, callback)', function() {
beforeEach(function() {
mock({
'file.txt': 'content',
link: mock.symlink({path: './file.txt'})
});
});
afterEach(mock.restore);
it('reads a symbolic link', function(done) {
fs.readlink('link', function(err, srcPath) {
if (err) {
return done(err);
}
assert.equal(srcPath, './file.txt');
done();
});
});
it('supports Buffer input', function(done) {
fs.readlink(Buffer.from('link'), function(err, srcPath) {
if (err) {
return done(err);
}
assert.equal(srcPath, './file.txt');
done();
});
});
withPromise.it('promise reads a symbolic link', function(done) {
fs.promises.readlink('link').then(function(srcPath) {
assert.equal(srcPath, './file.txt');
done();
}, done);
});
it('fails for regular files', function(done) {
fs.readlink('file.txt', function(err, srcPath) {
assert.instanceOf(err, Error);
done();
});
});
withPromise.it('promise fails for regular files', function(done) {
fs.promises.readlink('file.txt').then(
function() {
assert.fail('should not succeed.');
done();
},
function(err) {
assert.instanceOf(err, Error);
done();
}
);
});
});
describe('fs.readlinkSync(path)', function() {
beforeEach(function() {
mock({
'file.txt': 'content',
link: mock.symlink({path: './file.txt'})
});
});
afterEach(mock.restore);
it('reads a symbolic link', function() {
assert.equal(fs.readlinkSync('link'), './file.txt');
});
it('fails for regular files', function() {
assert.throws(function() {
fs.readlinkSync('file.txt');
});
});
});