Compare commits

...

1 Commits

Author SHA1 Message Date
Meghan Denny
bb1997b394 node: fix 3 more tests 2025-11-28 17:15:48 -08:00
4 changed files with 157 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ import type { Dirent as DirentType, PathLike, Stats as StatsType } from "fs";
const EventEmitter = require("node:events");
const promises = require("node:fs/promises");
const types = require("node:util/types");
const { validateFunction, validateInteger } = require("internal/validators");
const { validateAbortSignal, validateFunction, validateInteger } = require("internal/validators");
const kEmptyObject = Object.freeze(Object.create(null));
@@ -648,6 +648,33 @@ function getValidatedPath(p: any) {
if (typeof p !== "string") throw $ERR_INVALID_ARG_TYPE("path", "string or URL", p);
return require("node:path").resolve(p);
}
function getOptions(options, defaultOptions = kEmptyObject) {
if (options == null || typeof options === "function") {
return defaultOptions;
}
if (typeof options === "string") {
defaultOptions = { ...defaultOptions };
defaultOptions.encoding = options;
options = defaultOptions;
} else if (typeof options !== "object") {
throw $ERR_INVALID_ARG_TYPE("options", ["string", "object"], options);
}
if (options.encoding !== "buffer") assertEncoding(options.encoding);
if (options.signal !== undefined) {
validateAbortSignal(options.signal, "options.signal");
}
return options;
}
function assertEncoding(encoding) {
if (encoding && !Buffer.isEncoding(encoding)) {
const reason = "is invalid encoding";
throw $ERR_INVALID_ARG_VALUE("encoding", encoding, reason);
}
}
function watchFile(filename, options, listener) {
filename = getValidatedPath(filename);
@@ -1040,9 +1067,10 @@ function _toUnixTimestamp(time: any, name = "time") {
}
function opendirSync(path, options) {
// TODO: validatePath
// validateString(path, "path");
return new Dir(1, path, options);
const handle = 1;
path = getValidatedPath(path);
options = getOptions(options, { encoding: "utf8" });
return new Dir(handle, path, options);
}
class Dir {

View File

@@ -0,0 +1,28 @@
'use strict';
require('../common');
const assert = require('assert');
const fs = require('fs');
const {
O_CREAT = 0,
O_RDONLY = 0,
O_TRUNC = 0,
O_WRONLY = 0,
UV_FS_O_FILEMAP = 0
} = fs.constants;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
// Run this test on all platforms. While UV_FS_O_FILEMAP is only available on
// Windows, it should be silently ignored on other platforms.
const filename = tmpdir.resolve('fmap.txt');
const text = 'Memory File Mapping Test';
const mw = UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY;
const mr = UV_FS_O_FILEMAP | O_RDONLY;
fs.writeFileSync(filename, text, { flag: mw });
const r1 = fs.readFileSync(filename, { encoding: 'utf8', flag: mr });
assert.strictEqual(r1, text);

View File

@@ -0,0 +1,15 @@
'use strict';
// Tests to verify that a correctly formatted invalid encoding error
// is thrown. Originally the internal assertEncoding utility was
// reversing the arguments when constructing the ERR_INVALID_ARG_VALUE
// error.
require('../common');
const { opendirSync } = require('node:fs');
const { throws } = require('node:assert');
throws(() => opendirSync('.', { encoding: 'no' }), {
code: 'ERR_INVALID_ARG_VALUE',
message: 'The argument \'encoding\' is invalid encoding. Received \'no\'',
});

View File

@@ -0,0 +1,82 @@
'use strict';
// Refs: https://github.com/nodejs/node/issues/33940
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('assert');
tmpdir.refresh();
const file = tmpdir.resolve('read_stream_pos_test.txt');
fs.writeFileSync(file, '');
let counter = 0;
const writeInterval = setInterval(() => {
counter = counter + 1;
const line = `hello at ${counter}\n`;
fs.writeFileSync(file, line, { flag: 'a' });
}, 1);
const hwm = 10;
let bufs = [];
let isLow = false;
let cur = 0;
let stream;
const readInterval = setInterval(() => {
if (stream) return;
stream = fs.createReadStream(file, {
highWaterMark: hwm,
start: cur
});
stream.on('data', common.mustCallAtLeast((chunk) => {
cur += chunk.length;
bufs.push(chunk);
if (isLow) {
const brokenLines = Buffer.concat(bufs).toString()
.split('\n')
.filter((line) => {
const s = 'hello at'.slice(0, line.length);
if (line && !line.startsWith(s)) {
return true;
}
return false;
});
assert.strictEqual(brokenLines.length, 0);
exitTest();
return;
}
if (chunk.length !== hwm) {
isLow = true;
}
}));
stream.on('end', () => {
stream = null;
isLow = false;
bufs = [];
});
}, 10);
// Time longer than 90 seconds to exit safely
const endTimer = setTimeout(() => {
exitTest();
}, 90000);
const exitTest = () => {
clearInterval(readInterval);
clearInterval(writeInterval);
clearTimeout(endTimer);
if (stream && !stream.destroyed) {
stream.on('close', () => {
process.exit();
});
stream.destroy();
} else {
process.exit();
}
};