Skip to content

Commit b7ab7f8

Browse files
authored
Properly handle close errors in utimesMillis*() (#1069)
Fixes #1065 (comment)
1 parent 1c248ed commit b7ab7f8

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

lib/util/__tests__/utimes.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,51 @@ describe('utimes', () => {
9696
done()
9797
})
9898
})
99+
100+
it('should report close errors', done => {
101+
gracefulFsStub.open = u((pathIgnored, flagsIgnored, modeIgnored, callback) => {
102+
if (typeof modeIgnored === 'function') callback = modeIgnored
103+
process.nextTick(() => callback(null, 42))
104+
})
105+
106+
const closeError = new Error('close error')
107+
gracefulFsStub.close = u((fd, callback) => {
108+
process.nextTick(() => callback(closeError))
109+
})
110+
111+
gracefulFsStub.futimes = u((fd, atimeIgnored, mtimeIgnored, callback) => {
112+
process.nextTick(callback)
113+
})
114+
115+
utimes.utimesMillis('ignored', 'ignored', 'ignored', err => {
116+
assert.strictEqual(err, closeError)
117+
done()
118+
})
119+
})
120+
121+
it('should throw futimes error even if close also errors', done => {
122+
gracefulFsStub.open = u((pathIgnored, flagsIgnored, modeIgnored, callback) => {
123+
if (typeof modeIgnored === 'function') callback = modeIgnored
124+
process.nextTick(() => callback(null, 42))
125+
})
126+
127+
gracefulFsStub.close = u((fd, callback) => {
128+
process.nextTick(() => callback(new Error('close error')))
129+
})
130+
131+
let testError
132+
gracefulFsStub.futimes = u((fd, atimeIgnored, mtimeIgnored, callback) => {
133+
process.nextTick(() => {
134+
testError = new Error('A test error')
135+
callback(testError)
136+
})
137+
})
138+
139+
utimes.utimesMillis('ignored', 'ignored', 'ignored', err => {
140+
assert.strictEqual(err, testError)
141+
done()
142+
})
143+
})
99144
})
100145

101146
describe('utimesMillisSync()', () => {
@@ -118,5 +163,33 @@ describe('utimes', () => {
118163
assert.throws(() => utimes.utimesMillisSync('ignored', 'ignored', 'ignored'), testError)
119164
assert(closeCalled)
120165
})
166+
167+
it('should report close errors', () => {
168+
gracefulFsStub.openSync = (pathIgnored, flagsIgnored) => 42
169+
170+
const closeError = new Error('close error')
171+
gracefulFsStub.closeSync = (fd) => {
172+
throw closeError
173+
}
174+
175+
gracefulFsStub.futimesSync = (fd, atimeIgnored, mtimeIgnored) => {}
176+
177+
assert.throws(() => utimes.utimesMillisSync('ignored', 'ignored', 'ignored'), closeError)
178+
})
179+
180+
it('should throw futimes error even if close also errors', () => {
181+
gracefulFsStub.openSync = (pathIgnored, flagsIgnored) => 42
182+
183+
gracefulFsStub.closeSync = (fd) => {
184+
throw new Error('close error')
185+
}
186+
187+
const testError = new Error('A test error')
188+
gracefulFsStub.futimesSync = (fd, atimeIgnored, mtimeIgnored) => {
189+
throw testError
190+
}
191+
192+
assert.throws(() => utimes.utimesMillisSync('ignored', 'ignored', 'ignored'), testError)
193+
})
121194
})
122195
})

lib/util/utimes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async function utimesMillis (path, atime, mtime) {
1616
try {
1717
await fs.close(fd)
1818
} catch (closeErr) {
19-
error = closeErr
19+
if (!error) error = closeErr
2020
}
2121
}
2222

@@ -38,7 +38,7 @@ function utimesMillisSync (path, atime, mtime) {
3838
try {
3939
fs.closeSync(fd)
4040
} catch (closeErr) {
41-
error = closeErr
41+
if (!error) error = closeErr
4242
}
4343
}
4444

0 commit comments

Comments
 (0)