2018-11-17 20:31:20 +00:00
|
|
|
var util = require('../lib/test-utils.js');
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
|
|
|
|
describe('fs.access', function () {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
2019-01-03 02:45:53 +00:00
|
|
|
it('should expose access mode flags on fs and fs.constants', function() {
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
// F_OK
|
|
|
|
expect(fs.F_OK).to.equal(0);
|
|
|
|
expect(fs.constants.F_OK).to.equal(0);
|
|
|
|
|
|
|
|
// R_OK
|
|
|
|
expect(fs.R_OK).to.equal(4);
|
|
|
|
expect(fs.constants.R_OK).to.equal(4);
|
|
|
|
|
|
|
|
// W_OK
|
|
|
|
expect(fs.W_OK).to.equal(2);
|
|
|
|
expect(fs.constants.W_OK).to.equal(2);
|
|
|
|
|
|
|
|
// X_OK
|
|
|
|
expect(fs.X_OK).to.equal(1);
|
|
|
|
expect(fs.constants.X_OK).to.equal(1);
|
|
|
|
});
|
|
|
|
|
2018-11-17 20:31:20 +00:00
|
|
|
it('should be a function', function () {
|
|
|
|
var fs = util.fs();
|
|
|
|
expect(typeof fs.access).to.equal('function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an error if file does not exist', function (done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
fs.access('/tmp', fs.constants.F_OK, function (error) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-04 04:08:40 +00:00
|
|
|
it('should return no error if file does exist and mode = F_OK', function (done) {
|
2018-11-17 20:31:20 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.access('/myfile', fs.constants.F_OK, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-04 04:08:40 +00:00
|
|
|
it('should return no error if file does exist and mode = R_OK', function (done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.access('/myfile', fs.constants.R_OK, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return no error if file does exist and mode = W_OK', function (done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.access('/myfile', fs.constants.W_OK, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-22 03:58:55 +00:00
|
|
|
it('should return an error if file is not executable and mode = X_OK', function (done) {
|
2018-12-04 04:08:40 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
2018-12-22 03:58:55 +00:00
|
|
|
fs.chmod('/myfile', '644', function(error){
|
2018-12-04 04:08:40 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.access('/myfile', fs.constants.X_OK, function (error) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('EACCES');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return no error if file does exist and mode = X_OK', function (done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
2018-12-22 03:58:55 +00:00
|
|
|
fs.chmod('/myfile', 0o777, function(error) {
|
2018-12-04 04:08:40 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.access('/myfile', fs.constants.X_OK, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return no error if file does exist and no mode is passed', function (done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.access('/myfile', function (error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return no error if file does exist and mode = R_OK | W_OK', function (done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var contents = 'This is a file.';
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', contents, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
fs.access('/myfile', fs.constants.R_OK | fs.constants.W_OK, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-17 20:31:20 +00:00
|
|
|
});
|