From 359b0705367d54724b1a8b9966d6e878e3921aa9 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Tue, 17 Dec 2013 11:28:22 -0500 Subject: [PATCH] updated/added tests and caching for current time --- src/fs.js | 13 +++-- tests/spec/fs.utimes.spec.js | 100 +++++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/fs.js b/src/fs.js index f78b7bd..c9a7169 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1033,7 +1033,6 @@ define(function(require) { } } - //NOTE: utimes does follow symoblic links (safe to use find_node) function utimes_file(context, path, atime, mtime, callback) { path = normalize(path); @@ -1575,9 +1574,9 @@ define(function(require) { function _utimes(context, path, atime, mtime, callback) { if(!nullCheck(path, callback)) return; - //set atime or mtime to the current time if they are null - atime = (atime) ? atime : Date.now(); - mtime = (mtime) ? mtime : Date.now(); + var currentTime = Date.now(); + atime = (atime) ? atime : currentTime; + mtime = (mtime) ? mtime : currentTime; function check_result(error) { if (error) { @@ -1600,9 +1599,9 @@ define(function(require) { } } - //set atime or mtime to the current time if they are null - atime = (atime) ? atime : Date.now(); - mtime = (mtime) ? mtime : Date.now(); + var currentTime = Date.now() + atime = (atime) ? atime : currentTime; + mtime = (mtime) ? mtime : currentTime; var ofd = fs.openFiles[fd]; diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index 8fb0b52..80be8a9 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -38,6 +38,7 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); }); }); @@ -61,10 +62,11 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); }); }); - it('should error when atime is as invalid Datetime', function () { + it('should error when atime is as invalid number', function () { var complete = false; var _error; var that = this; @@ -84,10 +86,34 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); }); }); - it('should error when mtime is as invalid Datetime', function () { + it ('should error when path does not exist', function () { + var complete = false; + var _error; + var that = this; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + that.fs.utimes('/pathdoesnotexist', atime, mtime, function (error) { + _error = error; + complete = true; + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + expect(_error.name).toEqual('ENoEntry'); + }); + }); + + it('should error when mtime is an invalid number', function () { var complete = false; var _error; var that = this; @@ -107,6 +133,30 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); + }); + }); + + it ('should error when file descriptor is invalid', function () { + var complete = false; + var _error; + var that = this; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + that.fs.futimes(1, atime, mtime, function (error) { + _error = error; + complete = true; + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + expect(_error.name).toEqual('EBadFileDescriptor'); }); }); @@ -204,9 +254,7 @@ define(["IDBFS"], function(IDBFS) { _error = error; fs.stat('/testdir', function (error, stat) { - if (error) { - throw error; - } + if (error) throw error; _stat = stat; complete = true; @@ -225,5 +273,47 @@ define(["IDBFS"], function(IDBFS) { delete fs; }); }); + + it ('should update atime and mtime if they are null', function () { + var complete = false; + var _error; + var that = this; + + var atimeEst; + var mtimeEst; + var now; + + that.fs.writeFile('/myfile', '', function (error) { + if (error) throw error; + + that.fs.utimes('/myfile', null, null, function (error) { + _error = error; + + now = Date.now(); + + that.fs.stat('/myfile', function (error, stat) { + if (error) throw error; + + atime = stat.atime; + mtime = stat.mtime; + atimeEst = now - stat.atime; + mtimeEst = now - stat.mtime; + complete = true; + }); + }); + }); + + waitsFor(function (){ + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toEqual(null); + // Note: testing estimation as time may differ by a couple of milliseconds + // This number should be increased if tests are on slow systems + expect(atimeEst).toBeLessThan(10); + expect(mtimeEst).toBeLessThan(10); + }); + }); }); }); \ No newline at end of file