Files
ECC/tests/lib/helpers/load-with-file-system.js
T
affaan-m ef8c2d8b9c fix(opencode): guard legacy cleanup and portable consent fixtures
Preserve contributor history and current-main behavior while resolving the exact reviewed follow-up.

Source-PR: https://github.com/affaan-m/ECC/pull/3008
Source-Parent: d0dc1fcb08
Review-Manifest-SHA256: 8f15febd33dfa9f9ac6e70cb41f75ffee7aca47dd21782e841c33d4874341d6e
2026-09-28 00:54:21 -04:00

45 lines
1.7 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const Module = require('module');
// A private CommonJS graph for filesystem fault fixtures. Dependencies share
// this graph (including opaque lock identities), never the process module cache.
function createFileSystemLoader(fileSystem) {
const sourceRoot = path.resolve(__dirname, '../../../scripts');
const cache = new Map();
function load(filename) {
const absolute = path.resolve(filename);
const relative = path.relative(sourceRoot, absolute);
if (relative.startsWith('..') || path.isAbsolute(relative) || !absolute.endsWith('.js')) {
throw new Error('Fixture loader requires a JavaScript module under scripts/');
}
if (cache.has(absolute)) return cache.get(absolute).exports;
const child = new Module(absolute, module);
child.filename = absolute;
child.paths = Module._nodeModulePaths(path.dirname(absolute));
const nativeRequire = Module.createRequire(absolute);
child.require = request => {
if (request === 'fs' || request === 'node:fs') return fileSystem;
const resolved = nativeRequire.resolve(request);
const dependency = path.relative(sourceRoot, resolved);
if (path.isAbsolute(resolved) && !dependency.startsWith('..')
&& !path.isAbsolute(dependency) && resolved.endsWith('.js')) return load(resolved);
return nativeRequire(request);
};
cache.set(absolute, child);
try {
child._compile(fs.readFileSync(absolute, 'utf8'), absolute);
child.loaded = true;
return child.exports;
} catch (error) {
cache.delete(absolute);
throw error;
}
}
return load;
}
module.exports = { createFileSystemLoader };