mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
Add fs.mkstemp(), fs.mkstempSync() and fsPromises.mkstemp(), which create and open a unique temporary file in one operation. They expose uv_fs_mkstemp(), the file counterpart of the function behind fs.mkdtemp(). The callback and sync versions return the path and a file descriptor, the promise version returns the path and a FileHandle. libuv clears the path of the request when mkstemp() fails, so the template is saved in the request and used for the path of the error. The functions are also supported on mounted virtual file systems. Refs: https://github.com/nodejs/node/pull/33549 Refs: https://github.com/nodejs/node/issues/33890 Refs: https://github.com/nodejs/node/issues/5332 Signed-off-by: marcopiraccini <marco.piraccini@gmail.com> PR-URL: https://github.com/nodejs/node/pull/66484 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
79 lines
2.0 KiB
JavaScript
79 lines
2.0 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
|
|
// These tests make sure that the `options` object passed to these functions are
|
|
// never altered.
|
|
//
|
|
// Refer: https://github.com/nodejs/node/issues/7655
|
|
|
|
const fs = require('fs');
|
|
|
|
const options = common.mustNotMutateObjectDeep({});
|
|
const tmpdir = require('../common/tmpdir');
|
|
tmpdir.refresh();
|
|
|
|
fs.readFile(__filename, options, common.mustSucceed());
|
|
fs.readFileSync(__filename, options);
|
|
|
|
fs.readdir(__dirname, options, common.mustSucceed());
|
|
fs.readdirSync(__dirname, options);
|
|
|
|
if (common.canCreateSymLink()) {
|
|
const sourceFile = tmpdir.resolve('test-readlink');
|
|
const linkFile = tmpdir.resolve('test-readlink-link');
|
|
|
|
fs.writeFileSync(sourceFile, '');
|
|
fs.symlinkSync(sourceFile, linkFile);
|
|
|
|
fs.readlink(linkFile, options, common.mustSucceed());
|
|
fs.readlinkSync(linkFile, options);
|
|
}
|
|
|
|
{
|
|
const fileName = tmpdir.resolve('writeFile');
|
|
fs.writeFileSync(fileName, 'ABCD', options);
|
|
fs.writeFile(fileName, 'ABCD', options, common.mustSucceed());
|
|
}
|
|
|
|
{
|
|
const fileName = tmpdir.resolve('appendFile');
|
|
fs.appendFileSync(fileName, 'ABCD', options);
|
|
fs.appendFile(fileName, 'ABCD', options, common.mustSucceed());
|
|
}
|
|
|
|
if (!common.isIBMi) { // IBMi does not support fs.watch()
|
|
const watch = fs.watch(__filename, options, common.mustNotCall());
|
|
watch.close();
|
|
}
|
|
|
|
{
|
|
fs.watchFile(__filename, options, common.mustNotCall());
|
|
fs.unwatchFile(__filename);
|
|
}
|
|
|
|
{
|
|
fs.realpathSync(__filename, options);
|
|
fs.realpath(__filename, options, common.mustSucceed());
|
|
}
|
|
|
|
{
|
|
const tempFileName = tmpdir.resolve('mkdtemp-');
|
|
fs.mkdtempSync(tempFileName, options);
|
|
fs.mkdtemp(tempFileName, options, common.mustSucceed());
|
|
}
|
|
|
|
{
|
|
const tempFileName = tmpdir.resolve('mkstemp-');
|
|
fs.closeSync(fs.mkstempSync(tempFileName, options).fd);
|
|
fs.mkstemp(tempFileName, options, common.mustSucceed(({ fd }) => {
|
|
fs.closeSync(fd);
|
|
}));
|
|
}
|
|
|
|
{
|
|
const fileName = tmpdir.resolve('streams');
|
|
fs.WriteStream(fileName, options).once('open', common.mustCall(() => {
|
|
fs.ReadStream(fileName, options).destroy();
|
|
})).end();
|
|
}
|