diff --git a/lib/util/__tests__/utimes.test.js b/lib/util/__tests__/utimes.test.js index 879816ca..1cda4c07 100644 --- a/lib/util/__tests__/utimes.test.js +++ b/lib/util/__tests__/utimes.test.js @@ -4,7 +4,9 @@ var os = require('os') var fs = require('fs') var fse = require(process.cwd()) var semver = require('semver') -var utimes = require('../utimes') +var proxyquire = require('proxyquire') +var gracefulFsStub +var utimes /* global beforeEach, describe, it */ @@ -17,6 +19,9 @@ describe('utimes', function () { beforeEach(function (done) { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'utimes') fse.emptyDir(TEST_DIR, done) + // reset stubs + gracefulFsStub = {} + utimes = proxyquire('../utimes', {'graceful-fs': gracefulFsStub}) }) describe('hasMillisResSync()', function () { @@ -79,5 +84,39 @@ describe('utimes', function () { done() }) }) + + it('should close open file desciptors after encountering an error', function (done) { + var fakeFd = Math.random() + + gracefulFsStub.open = function (pathIgnored, flagsIgnored, modeIgnored, callback) { + if (typeof modeIgnored === 'function') { + callback = modeIgnored + } + process.nextTick(function () { + callback(null, fakeFd) + }) + } + + var closeCalled = false + gracefulFsStub.close = function (fd, callback) { + assert.equal(fd, fakeFd) + closeCalled = true + process.nextTick(callback) + } + + var testError + gracefulFsStub.futimes = function (fd, atimeIgnored, mtimeIgnored, callback) { + process.nextTick(function () { + testError = new Error('A test error') + callback(testError) + }) + } + + utimes.utimesMillis('ignored', 'ignored', 'ignored', function (err) { + assert.equal(err, testError) + assert(closeCalled) + done() + }) + }) }) }) diff --git a/lib/util/utimes.js b/lib/util/utimes.js index c99b010b..ae5adaf7 100644 --- a/lib/util/utimes.js +++ b/lib/util/utimes.js @@ -55,8 +55,8 @@ function utimesMillis (path, atime, mtime, callback) { fs.open(path, 'r+', function (err, fd) { if (err) return callback(err) fs.futimes(fd, atime, mtime, function (err) { - if (err) return callback(err) fs.close(fd, callback) + if (err) return callback(err) }) }) } diff --git a/package.json b/package.json index 2f08d241..2b4db7f1 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "istanbul": "^0.3.5", "minimist": "^1.1.1", "mocha": "^2.1.0", + "proxyquire": "^1.7.10", "read-dir-files": "^0.1.1", "secure-random": "^1.1.1", "semver": "^4.3.6",