2014-05-23 20:53:50 +00:00
|
|
|
var util = require('../../lib/test-utils.js');
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
|
|
|
|
function getTimes(fs, path, callback) {
|
|
|
|
fs.stat(path, function(error, stats) {
|
|
|
|
if(error) throw error;
|
2014-03-29 19:21:53 +00:00
|
|
|
callback({mtime: stats.mtimeMs, atime: stats.atimeMs});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
|
|
|
}
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
describe('FileSystemShell.touch', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should be a function', function() {
|
|
|
|
var shell = util.shell();
|
|
|
|
expect(shell.touch).to.be.a('function');
|
|
|
|
});
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should create a new file if path does not exist', function(done) {
|
|
|
|
var fs = util.fs();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.shell();
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
shell.touch('/newfile', function(error) {
|
|
|
|
if(error) throw error;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/newfile', function(error, stats) {
|
|
|
|
expect(error).not.to.exist;
|
2018-11-28 18:54:20 +00:00
|
|
|
expect(stats.isFile()).to.be.true;
|
2014-05-23 20:53:50 +00:00
|
|
|
done();
|
2014-02-15 15:54:54 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should skip creating a new file if options.updateOnly is true', function(done) {
|
|
|
|
var fs = util.fs();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.shell();
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
shell.touch('/newfile', { updateOnly: true }, function(error) {
|
|
|
|
if(error) throw error;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/newfile', function(error, stats) {
|
|
|
|
expect(error).to.exist;
|
2018-07-26 21:41:35 +00:00
|
|
|
expect(stats).not.to.exist;
|
2014-05-23 20:53:50 +00:00
|
|
|
done();
|
2014-02-15 15:54:54 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should update times if path does exist', function(done) {
|
|
|
|
var fs = util.fs();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.shell();
|
2014-05-23 20:53:50 +00:00
|
|
|
var atime = Date.parse('1 Oct 2000 15:33:22');
|
|
|
|
var mtime = Date.parse('30 Sep 2000 06:43:54');
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.open('/newfile', 'w', function (error, fd) {
|
|
|
|
if (error) throw error;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.futimes(fd, atime, mtime, function (error) {
|
|
|
|
if(error) throw error;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.close(fd, function(error) {
|
|
|
|
if(error) throw error;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
getTimes(fs, '/newfile', function(times1) {
|
|
|
|
shell.touch('/newfile', function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
getTimes(fs, '/newfile', function(times2) {
|
|
|
|
expect(times2.mtime).to.be.above(times1.mtime);
|
|
|
|
expect(times2.atime).to.be.above(times1.atime);
|
|
|
|
done();
|
2014-02-15 15:54:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should update times to specified date if path does exist', function(done) {
|
|
|
|
var fs = util.fs();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.shell();
|
2014-05-23 20:53:50 +00:00
|
|
|
var date = Date.parse('1 Oct 2001 15:33:22');
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.open('/newfile', 'w', function (error, fd) {
|
|
|
|
if (error) throw error;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.close(fd, function(error) {
|
|
|
|
if(error) throw error;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
shell.touch('/newfile', { date: date }, function(error) {
|
|
|
|
expect(error).not.to.exist;
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
getTimes(fs, '/newfile', function(times) {
|
|
|
|
expect(times.mtime).to.equal(date);
|
|
|
|
done();
|
2014-02-15 15:54:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|