2019-01-31 20:11:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const util = require('../lib/test-utils.js');
|
|
|
|
const expect = require('chai').expect;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
describe('fs.readlink', function () {
|
2014-05-23 20:53:50 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
it('should be a function', function () {
|
2019-01-31 20:11:07 +00:00
|
|
|
const fs = util.fs();
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(fs.readlink).to.be.a('function');
|
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
it('should return an error if part of the parent destination path does not exist', function (done) {
|
2019-01-31 20:11:07 +00:00
|
|
|
const fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.readlink('/tmp/mydir', function (error) {
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(error).to.exist;
|
2018-07-15 17:25:35 +00:00
|
|
|
expect(error.code).to.equal('ENOENT');
|
2014-05-23 20:53:50 +00:00
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
it('should return an error if the path is not a symbolic link', function (done) {
|
2019-01-31 20:11:07 +00:00
|
|
|
const fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.readlink('/', function (error) {
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(error).to.exist;
|
2018-07-15 17:25:35 +00:00
|
|
|
expect(error.code).to.equal('ENOENT');
|
2014-05-23 20:53:50 +00:00
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
it('should return an error if the path is not a symbolic link', function (done) {
|
2019-01-31 20:11:07 +00:00
|
|
|
const fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.mkdir('/tmp', function (error) {
|
|
|
|
if (error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.readlink('/tmp', function (error) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('EINVAL');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the contents of a symbolic link', function (done) {
|
|
|
|
const fs = util.fs();
|
|
|
|
|
|
|
|
fs.symlink('/', '/myfile', function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.readlink('/myfile', function (error, result) {
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).to.equal('/');
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-29 21:49:19 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
it('should allow relative paths, but resolve to the dstpath', function (done) {
|
2019-01-31 20:11:07 +00:00
|
|
|
const fs = util.fs();
|
|
|
|
const contents = 'contents';
|
2018-11-29 21:49:19 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.mkdir('/dir', function (error) {
|
|
|
|
if (error) throw error;
|
2018-11-29 21:49:19 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.writeFile('/file', contents, function (error) {
|
|
|
|
if (error) throw error;
|
2018-11-29 21:49:19 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.symlink('../file', '/dir/symlink', function (error) {
|
|
|
|
if (error) throw error;
|
2018-11-29 21:49:19 +00:00
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.readlink('/dir/symlink', function (error, result) {
|
2018-11-29 21:49:19 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).to.equal('../file');
|
|
|
|
|
2019-02-26 16:40:27 +00:00
|
|
|
fs.readFile('/dir/symlink', 'utf8', function (error, data) {
|
2018-11-29 21:49:19 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.equal(contents);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|