Skip to content

Commit

Permalink
fix(rulesets): require node_module (#1029)
Browse files Browse the repository at this point in the history
* fix(rulesets): require node_module

* test: remove node_modules dir
  • Loading branch information
P0lip authored Mar 24, 2020
1 parent 5dc132d commit 9e2ca55
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/rulesets/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function proxyRequire(source: string): NodeJS.Require {
function req(p: string) {
if (p.startsWith('.')) {
p = join(source, '..', stripRoot(p));
} else {
p = require.resolve(p, { paths: [join(source, '..')] });
}

return actualRequire.call(null, p);
Expand Down
7 changes: 6 additions & 1 deletion test-harness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ describe('cli acceptance tests', () => {
beforeAll(async () => {
await Promise.all(
scenario.assets.map(async ([asset]) => {
const tmpFileHandle = await tmpFile();
const ext = path.extname(asset);
const tmpFileHandle = await tmpFile({
...(ext && { postfix: ext }),
});

tmpFileHandles.set(asset, tmpFileHandle);

const normalizedName = normalize(tmpFileHandle.name);

replacements[asset] = normalizedName;
replacements[`${asset}|no-ext`] = normalizedName.replace(new RegExp(`${path.extname(normalizedName)}$`), '');
replacements[`${asset}|filename|no-ext`] = path.basename(normalizedName, true);
}),
);

Expand Down
36 changes: 36 additions & 0 deletions test-harness/scenarios/require-module.scenario
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
====test====
Custom function is able to require a module
====document====
{}
====asset:fn.js====
const fs = require('fs');
const path = require('path');
const nodeModulesDir = path.join('{asset:fn.js}', '../node_modules');
const packageDir = path.join(nodeModulesDir, 'foo');

fs.mkdirSync(packageDir, { recursive: true });
fs.writeFileSync(path.join(packageDir, 'package.json'), JSON.stringify({ main: './index.js' }));
fs.writeFileSync(path.join(packageDir, 'index.js'), 'module.exports = Function()');

try {
require('foo'); // this will throw if foo cannot be resolved
} finally {
fs.unlinkSync(path.join(packageDir, 'package.json'));
fs.unlinkSync(path.join(packageDir, 'index.js'));
fs.rmdirSync(packageDir);
fs.rmdirSync(nodeModulesDir);
}

module.exports = () => void 0;
====asset:ruleset====
functions: ["{asset:fn.js|filename|no-ext}"]
functionsDir: "."
rules:
has-property:
given: $
then:
function: "{asset:fn.js|filename|no-ext}"
====command====
{bin} lint {document} --ruleset {asset:ruleset} --ignoreUnknownFormat
====status====
0

0 comments on commit 9e2ca55

Please sign in to comment.