|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const test = require('node:test'); |
| 5 | +const assert = require('node:assert'); |
| 6 | +const fs = require('node:fs'); |
| 7 | +const tmpdir = require('../common/tmpdir'); |
| 8 | + |
| 9 | +test('fs.stat should throw AbortError when called with an already aborted AbortSignal', async () => { |
| 10 | +// This test verifies that fs.stat immediately throws an AbortError if the provided AbortSignal |
| 11 | +// has already been canceled. This approach is used because trying to abort an fs.stat call in flight |
| 12 | +// is unreliable given that file system operations tend to complete very quickly on many platforms. |
| 13 | +tmpdir.refresh(); |
| 14 | + |
| 15 | +const filePath = tmpdir.resolve('temp.txt'); |
| 16 | +fs.writeFileSync(filePath, 'Test'); |
| 17 | + |
| 18 | +// Create an already aborted AbortSignal. |
| 19 | +const signal = AbortSignal.abort(); |
| 20 | + |
| 21 | +const { promise, resolve, reject } = Promise.withResolvers(); |
| 22 | +fs.stat(filePath, { signal }, (err, stats) => { |
| 23 | +if (err) { |
| 24 | +return reject(err); |
| 25 | +} |
| 26 | +resolve(stats); |
| 27 | +}); |
| 28 | + |
| 29 | +// Assert that the promise is rejected with an AbortError. |
| 30 | +await assert.rejects(promise, { name: 'AbortError' }); |
| 31 | + |
| 32 | +fs.unlinkSync(filePath); |
| 33 | +tmpdir.refresh(); |
| 34 | +}); |