From 23ace603c9bbfbf92d02cfa6406026a7d3d82831 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Sun, 15 Dec 2013 21:22:36 -0500 Subject: [PATCH] added/fixed tests and futimes bug --- AUTHORS | 1 + README.md | 8 + src/fs.js | 8 +- tests/spec/fs.utimes.spec.js | 282 ++++++++++++++++++++++------------- 4 files changed, 194 insertions(+), 105 deletions(-) diff --git a/AUTHORS b/AUTHORS index e218b72..eed1c65 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,3 +1,4 @@ Alan K (blog.modeswitch.org) David Humphrey (@humphd) Abir Viqar +Barry Tulchinsky (@btulchinsky) diff --git a/README.md b/README.md index c954638..bc2b2fd 100644 --- a/README.md +++ b/README.md @@ -336,3 +336,11 @@ Asynchronous truncate(2). Callback gets no additional arguments. #### fs.ftruncate(fd, length, callback) Asynchronous ftruncate(2). Callback gets no additional arguments. + +#### fs.utimes(path, atime, mtime, callback) + +Asynchronous utimes(3). Callback gets no additional arguments. + +#### fs.futimes(fd, atime, mtime, callback) + +Asynchronous futimes(3). Callback gets no additional arguments. diff --git a/src/fs.js b/src/fs.js index 276a458..f78b7bd 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1075,14 +1075,14 @@ define(function(require) { } //check if atime and mtime are integers and >= 0 - if (typeof atime != 'number' || typeof mtime == 'number') { + if (typeof atime != 'number' || typeof mtime != 'number') { callback(new EInvalid('Invalid DateTime values')); } else if (atime < 0 || mtime < 0) { callback(new EInvalid('DateTime values cannot be negative')); } else { - context.get(ofd.id, path, update_times); + context.get(ofd.id, update_times); } } @@ -1590,7 +1590,7 @@ define(function(require) { utimes_file(context, path, atime, mtime, check_result) } - function _futimes(context, fd, atime, mtime, callback) { + function _futimes(fs, context, fd, atime, mtime, callback) { function check_result(error) { if (error) { callback(error); @@ -1966,7 +1966,7 @@ define(function(require) { var error = fs.queueOrRun( function () { var context = fs.provider.getReadWriteContext(); - _futimes(context, fd, atime, mtime, callback); + _futimes(fs, context, fd, atime, mtime, callback); } ); }; diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index 038f26f..8fb0b52 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -14,108 +14,108 @@ define(["IDBFS"], function(IDBFS) { delete this.fs; }); - // it('should be a function', function() { - // expect(typeof this.fs.utimes).toEqual('function'); - // }); + it('should be a function', function() { + expect(typeof this.fs.utimes).toEqual('function'); + }); - // it('should error when atime is negative', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function(error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', -1, Date.now(), function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - // it('should error when mtime is negative', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function(error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', Date.now(), -1, function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - // it('should error when atime is as invalid Datetime', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function (error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', 'invalid datetime', Date.now(), function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - // it('should error when mtime is as invalid Datetime', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function (error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', Date.now(), 'invalid datetime', function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - it('should change atime and mtime of a file path)', function () { + it('should error when atime is negative', function () { var complete = false; var _error; var that = this; - var stat; + that.fs.writeFile('/testfile', '', function(error) { + if (error) throw error; + + that.fs.utimes('/testfile', -1, Date.now(), function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should error when mtime is negative', function () { + var complete = false; + var _error; + var that = this; + + that.fs.writeFile('/testfile', '', function(error) { + if (error) throw error; + + that.fs.utimes('/testfile', Date.now(), -1, function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should error when atime is as invalid Datetime', function () { + var complete = false; + var _error; + var that = this; + + that.fs.writeFile('/testfile', '', function (error) { + if (error) throw error; + + that.fs.utimes('/testfile', 'invalid datetime', Date.now(), function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should error when mtime is as invalid Datetime', function () { + var complete = false; + var _error; + var that = this; + + that.fs.writeFile('/testfile', '', function (error) { + if (error) throw error; + + that.fs.utimes('/testfile', Date.now(), 'invalid datetime', function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should change atime and mtime of a file path', function () { + var complete = false; + var _error; + var that = this; + + var _stat; var atime = Date.parse('1 Oct 2000 15:33:22'); var mtime = Date.parse('30 Sep 2000 06:43:54'); @@ -126,10 +126,10 @@ define(["IDBFS"], function(IDBFS) { that.fs.utimes('/testfile', atime, mtime, function (error) { _error = error; - that.fs.stat('/testfile', function (error, rstat) { + that.fs.stat('/testfile', function (error, stat) { if (error) throw error; - stat = rstat; + _stat = stat; complete = true; }); }); @@ -141,8 +141,88 @@ define(["IDBFS"], function(IDBFS) { runs(function() { expect(_error).toEqual(null); - expect(stat.atime).toEqual(atime); - expect(stat.mtime).toEqual(mtime); + expect(_stat.atime).toEqual(atime); + expect(_stat.mtime).toEqual(mtime); + }); + }); + + it ('should change atime and mtime for a valid file descriptor', function (error) { + var complete = false; + var _error; + var that = this; + + var ofd; + var _stat; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + that.fs.open('/testfile', 'w', function (error, result) { + if (error) throw error; + + ofd = result; + + that.fs.futimes(ofd, atime, mtime, function (error) { + _error = error; + + that.fs.fstat(ofd, function (error, stat) { + if (error) throw error; + + _stat = stat; + complete = true; + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toEqual(null); + expect(_stat.atime).toEqual(atime); + expect(_stat.mtime).toEqual(mtime); + }); + }); + + it ('should update atime and mtime of directory path', function (error) { + var complete = false + var _error; + + //Note: required as the filesystem somehow gets removed from the Jasmine object + var fs = this.fs; + + var _stat; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + fs.mkdir('/testdir', function (error) { + if (error) throw error; + + fs.utimes('/testdir', atime, mtime, function (error) { + _error = error; + + fs.stat('/testdir', function (error, stat) { + if (error) { + throw error; + } + + _stat = stat; + complete = true; + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toEqual(null); + expect(_stat.atime).toEqual(atime); + expect(_stat.mtime).toEqual(mtime); + delete fs; }); }); });