Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions lib/util/__tests__/utimes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,51 @@ describe('utimes', () => {
done()
})
})

it('should report close errors', done => {
gracefulFsStub.open = u((pathIgnored, flagsIgnored, modeIgnored, callback) => {
if (typeof modeIgnored === 'function') callback = modeIgnored
process.nextTick(() => callback(null, 42))
})

const closeError = new Error('close error')
gracefulFsStub.close = u((fd, callback) => {
process.nextTick(() => callback(closeError))
})

gracefulFsStub.futimes = u((fd, atimeIgnored, mtimeIgnored, callback) => {
process.nextTick(callback)
})

utimes.utimesMillis('ignored', 'ignored', 'ignored', err => {
assert.strictEqual(err, closeError)
done()
})
})

it('should throw futimes error even if close also errors', done => {
gracefulFsStub.open = u((pathIgnored, flagsIgnored, modeIgnored, callback) => {
if (typeof modeIgnored === 'function') callback = modeIgnored
process.nextTick(() => callback(null, 42))
})

gracefulFsStub.close = u((fd, callback) => {
process.nextTick(() => callback(new Error('close error')))
})

let testError
gracefulFsStub.futimes = u((fd, atimeIgnored, mtimeIgnored, callback) => {
process.nextTick(() => {
testError = new Error('A test error')
callback(testError)
})
})

utimes.utimesMillis('ignored', 'ignored', 'ignored', err => {
assert.strictEqual(err, testError)
done()
})
})
})

describe('utimesMillisSync()', () => {
Expand All @@ -118,5 +163,33 @@ describe('utimes', () => {
assert.throws(() => utimes.utimesMillisSync('ignored', 'ignored', 'ignored'), testError)
assert(closeCalled)
})

it('should report close errors', () => {
gracefulFsStub.openSync = (pathIgnored, flagsIgnored) => 42

const closeError = new Error('close error')
gracefulFsStub.closeSync = (fd) => {
throw closeError
}

gracefulFsStub.futimesSync = (fd, atimeIgnored, mtimeIgnored) => {}

assert.throws(() => utimes.utimesMillisSync('ignored', 'ignored', 'ignored'), closeError)
})

it('should throw futimes error even if close also errors', () => {
gracefulFsStub.openSync = (pathIgnored, flagsIgnored) => 42

gracefulFsStub.closeSync = (fd) => {
throw new Error('close error')
}

const testError = new Error('A test error')
gracefulFsStub.futimesSync = (fd, atimeIgnored, mtimeIgnored) => {
throw testError
}

assert.throws(() => utimes.utimesMillisSync('ignored', 'ignored', 'ignored'), testError)
})
})
})
4 changes: 2 additions & 2 deletions lib/util/utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function utimesMillis (path, atime, mtime) {
try {
await fs.close(fd)
} catch (closeErr) {
error = closeErr
if (!error) error = closeErr
}
}

Expand All @@ -38,7 +38,7 @@ function utimesMillisSync (path, atime, mtime) {
try {
fs.closeSync(fd)
} catch (closeErr) {
error = closeErr
if (!error) error = closeErr
}
}

Expand Down
Loading