⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Open
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
61 changes: 60 additions & 1 deletion test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('node:path');
const test = require('node:test');
const fs = require('node:fs/promises');
const assert = require('node:assert/strict');
const { pathToFileURL } = require('node:url');
const { hostname } = require('node:os');

const stackFramesRegexp = /(?<=\n)(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\[\d+m)?(\n|$)/g;
Expand Down Expand Up @@ -33,8 +34,66 @@ function replaceWindowsPaths(str) {

function transformProjectRoot(replacement = '') {
const projectRoot = path.resolve(__dirname, '../..');
const fsRoot = path.parse(projectRoot).root;

// If projectRoot is fs root, skip stripping to avoid corrupting URL separators.
if (projectRoot === fsRoot) {
return (str) => str.replaceAll('\\\'', "'");
}

// Stack output may use '/' on Windows; handle both separators.
const projectRootPosix = projectRoot.replaceAll(path.win32.sep, path.posix.sep);
const fileUrlPrefix = 'file:///';
const fileUrl = pathToFileURL(projectRoot).href;
const fileUrlRoot = fileUrl.startsWith(fileUrlPrefix) ?
fileUrl.slice(fileUrlPrefix.length) :
null;

const stripPrefixAtBoundary = (str, prefix) => {
if (!prefix) return str;
let out = '';
let index = 0;
while (true) {
const match = str.indexOf(prefix, index);
if (match === -1) return out + str.slice(index);
const after = match + prefix.length;
const nextChar = str[after];
const isBoundary = after === str.length || nextChar === '/' || nextChar === '\\';
out += str.slice(index, match);
out += isBoundary ? replacement : prefix;
index = after;
}
};

// Strip repo prefix from file:// URLs only; keep other schemes intact.
const stripFileUrlRoot = (str) => {
if (!fileUrlRoot) return str;
const needle = `${fileUrlPrefix}${fileUrlRoot}`;
let out = '';
let index = 0;
while (true) {
const match = str.indexOf(needle, index);
if (match === -1) return out + str.slice(index);
const after = match + needle.length;
const nextChar = str[after];
const isBoundary = after === str.length || nextChar === '/';
out += str.slice(index, match);
out += isBoundary ? `${fileUrlPrefix}${replacement}` : needle;

// If the next character is a '/' and the replacement is empty, skip the next character.
if (isBoundary && replacement === '' && nextChar === '/') {
index = after + 1;
} else {
index = after;
}
}
};
return (str) => {
return str.replaceAll('\\\'', "'").replaceAll(projectRoot, replacement);
let out = str.replaceAll('\\\'', "'");
out = stripFileUrlRoot(out);
out = stripPrefixAtBoundary(out, projectRoot);
if (projectRootPosix !== projectRoot) out = stripPrefixAtBoundary(out, projectRootPosix);
return out;
};
}

Expand Down
20 changes: 9 additions & 11 deletions test/es-module/test-esm-import-meta.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import '../common/index.mjs';
import assert from 'assert';
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';

assert.strictEqual(Object.getPrototypeOf(import.meta), null);

Expand All @@ -16,18 +18,14 @@ for (const descriptor of Object.values(descriptors)) {
});
}

const urlReg = /^file:\/\/\/.*\/test\/es-module\/test-esm-import-meta\.mjs$/;
assert.match(import.meta.url, urlReg);
const filePath = fileURLToPath(import.meta.url);
assert.ok(path.isAbsolute(filePath));
assert.strictEqual(import.meta.url, pathToFileURL(filePath).href);

// Match *nix paths: `/some/path/test/es-module`
// Match Windows paths: `d:\\some\\path\\test\\es-module`
const dirReg = /^(\/|\w:\\).*(\/|\\)test(\/|\\)es-module$/;
assert.match(import.meta.dirname, dirReg);

// Match *nix paths: `/some/path/test/es-module/test-esm-import-meta.mjs`
// Match Windows paths: `d:\\some\\path\\test\\es-module\\test-esm-import-meta.js`
const fileReg = /^(\/|\w:\\).*(\/|\\)test(\/|\\)es-module(\/|\\)test-esm-import-meta\.mjs$/;
assert.match(import.meta.filename, fileReg);
const suffix = path.join('test', 'es-module', 'test-esm-import-meta.mjs');
assert.ok(filePath.endsWith(suffix));
assert.strictEqual(import.meta.filename, filePath);
assert.strictEqual(import.meta.dirname, path.dirname(filePath));

// Verify that `data:` imports do not behave like `file:` imports.
import dataDirname from 'data:text/javascript,export default "dirname" in import.meta';
Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-assert-snapshot-transform-project-root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

Check failure on line 1 in test/parallel/test-assert-snapshot-transform-project-root.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded before any other modules

Check failure on line 1 in test/parallel/test-assert-snapshot-transform-project-root.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded

const assert = require('node:assert/strict');
const path = require('node:path');
const { pathToFileURL } = require('node:url');

const snapshot = require('../common/assertSnapshot');

// Coverage: boundary strip, URL tokens, file:// root, % encoding.
{
const stripProjectRoot = snapshot.transformProjectRoot('');
const projectRoot = path.resolve(__dirname, '..', '..');

assert.strictEqual(
stripProjectRoot(`${projectRoot}${path.sep}test${path.sep}fixtures`),
`${path.sep}test${path.sep}fixtures`,
);

const shouldNotStrip = `${projectRoot}_modules`;
assert.strictEqual(stripProjectRoot(shouldNotStrip), shouldNotStrip);

const urlLike = `https://${projectRoot}js.org`;
assert.strictEqual(stripProjectRoot(urlLike), urlLike);

const shouldNotStripSuffix = `${projectRoot}suffix/test`;
assert.strictEqual(stripProjectRoot(shouldNotStripSuffix), shouldNotStripSuffix);

const fileUrl = pathToFileURL(projectRoot).href;
assert.strictEqual(stripProjectRoot(`${fileUrl}/test/fixtures`), 'file:///test/fixtures');

const fileUrlWithSpace = pathToFileURL(path.join(projectRoot, 'spaced dir')).href;
assert.strictEqual(stripProjectRoot(fileUrlWithSpace), 'file:///spaced%20dir');

if (process.platform === 'win32') {
const projectRootPosix = projectRoot.replaceAll(path.win32.sep, path.posix.sep);

assert.strictEqual(
stripProjectRoot(`${projectRootPosix}/test/fixtures`),
'/test/fixtures',
);

const shouldNotStripPosix = `${projectRootPosix}_modules`;
assert.strictEqual(stripProjectRoot(shouldNotStripPosix), shouldNotStripPosix);
}
}
7 changes: 4 additions & 3 deletions test/parallel/test-cli-permission-deny-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ const path = require('path');
}

{
const { root } = path.parse(process.cwd());
const abs = (p) => path.join(root, p);
const firstPath = abs(path.sep + process.cwd().split(path.sep, 2)[1]);
const repoRoot = path.resolve(process.cwd());
const root = path.parse(repoRoot).root;
const [firstDir] = path.relative(root, repoRoot).split(path.sep);
const firstPath = firstDir ? path.join(root, firstDir) : path.join(root, 'test');
if (firstPath.startsWith('/etc')) {
common.skip('/etc as firstPath');
}
Expand Down
9 changes: 6 additions & 3 deletions test/parallel/test-node-output-console.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ function replaceStackTrace(str) {
}

describe('console output', { concurrency: !process.env.TEST_PARALLEL }, () => {
function normalize(str) {
return str.replaceAll(snapshot.replaceWindowsPaths(process.cwd()), '').replaceAll('/', '*').replaceAll(process.version, '*').replaceAll(/\d+/g, '*');
}
const normalize = snapshot.transform(
snapshot.transformProjectRoot(''),
(str) => str.replaceAll('/', '*')
.replaceAll(process.version, '*')
.replaceAll(/\d+/g, '*'),
);
const tests = [
{ name: 'console/2100bytes.js' },
{ name: 'console/console_low_stack_space.js' },
Expand Down
11 changes: 6 additions & 5 deletions test/parallel/test-node-output-errors.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import * as snapshot from '../common/assertSnapshot.js';
import * as os from 'node:os';
import { describe, it } from 'node:test';
import { basename } from 'node:path';
import { pathToFileURL } from 'node:url';

const skipForceColors =
(common.isWindows && (Number(os.release().split('.')[0]) !== 10 || Number(os.release().split('.')[2]) < 14393)); // See https://github.com/nodejs/node/pull/33132


function replaceStackTrace(str) {
return snapshot.replaceStackTrace(str, '$1at *$7\n');
}
Expand All @@ -22,8 +20,7 @@ function replaceForceColorsStackTrace(str) {
describe('errors output', { concurrency: !process.env.TEST_PARALLEL }, () => {
function normalize(str) {
const baseName = basename(process.argv0 || 'node', '.exe');
return str.replaceAll(snapshot.replaceWindowsPaths(process.cwd()), '')
.replaceAll(pathToFileURL(process.cwd()).pathname, '')
return str
.replaceAll('//', '*')
.replaceAll(/\/(\w)/g, '*$1')
.replaceAll('*test*', '*')
Expand All @@ -36,7 +33,11 @@ describe('errors output', { concurrency: !process.env.TEST_PARALLEL }, () => {
return normalize(str).replaceAll(/\d+:\d+/g, '*:*').replaceAll(/:\d+/g, ':*').replaceAll('*fixtures*message*', '*');
}
const common = snapshot
.transform(snapshot.replaceWindowsLineEndings, snapshot.replaceWindowsPaths);
.transform(
snapshot.replaceWindowsLineEndings,
snapshot.replaceWindowsPaths,
snapshot.transformProjectRoot(''),
);
const defaultTransform = snapshot.transform(common, normalize, snapshot.replaceNodeVersion);
const errTransform = snapshot.transform(common, normalizeNoNumbers, snapshot.replaceNodeVersion);
const promiseTransform = snapshot.transform(common, replaceStackTrace,
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-node-output-eval.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { basename } from 'node:path';
import { describe, it } from 'node:test';

describe('eval output', { concurrency: true }, () => {
function normalize(str) {
return str.replaceAll(snapshot.replaceWindowsPaths(process.cwd()), '')
.replaceAll(/\d+:\d+/g, '*:*');
}
const normalize = snapshot.transform(
snapshot.transformProjectRoot(''),
(str) => str.replaceAll(/\d+:\d+/g, '*:*'),
);

const defaultTransform = snapshot.transform(
normalize,
Expand Down
16 changes: 8 additions & 8 deletions test/parallel/test-node-output-v8-warning.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ function replaceExecName(str) {
}

describe('v8 output', { concurrency: !process.env.TEST_PARALLEL }, () => {
function normalize(str) {
return str.replaceAll(snapshot.replaceWindowsPaths(process.cwd()), '')
.replaceAll(/:\d+/g, ':*')
.replaceAll('/', '*')
.replaceAll('*test*', '*')
.replaceAll(/.*?\*fixtures\*v8\*/g, '(node:*) V8: *') // Replace entire path before fixtures/v8
.replaceAll('*fixtures*v8*', '*');
}
const normalize = snapshot.transform(
snapshot.transformProjectRoot(''),
(str) => str.replaceAll(/:\d+/g, ':*')
.replaceAll('/', '*')
.replaceAll('*test*', '*')
.replaceAll(/.*?\*fixtures\*v8\*/g, '(node:*) V8: *') // Replace entire path before fixtures/v8
.replaceAll('*fixtures*v8*', '*'),
);
const common = snapshot
.transform(snapshot.replaceWindowsLineEndings, snapshot.replaceWindowsPaths, replaceNodeVersion, replaceExecName);
const defaultTransform = snapshot.transform(common, normalize);
Expand Down
10 changes: 7 additions & 3 deletions test/parallel/test-node-output-vm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ function replaceNodeVersion(str) {
}

describe('vm output', { concurrency: !process.env.TEST_PARALLEL }, () => {
function normalize(str) {
return str.replaceAll(snapshot.replaceWindowsPaths(process.cwd()), '').replaceAll('//', '*').replaceAll(/\/(\w)/g, '*$1').replaceAll('*test*', '*').replaceAll(/node:vm:\d+:\d+/g, 'node:vm:*');
}
const normalize = snapshot.transform(
snapshot.transformProjectRoot(''),
(str) => str.replaceAll('//', '*')
.replaceAll(/\/(\w)/g, '*$1')
.replaceAll('*test*', '*')
.replaceAll(/node:vm:\d+:\d+/g, 'node:vm:*'),
);

const defaultTransform = snapshot
.transform(snapshot.replaceWindowsLineEndings, snapshot.replaceWindowsPaths, normalize, replaceNodeVersion);
Expand Down
Loading