Skip to content

Commit

Permalink
Fix: test regression
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Aug 17, 2022
1 parent 7cd290e commit 00fa82f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 40 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ const init = async ({
ignoreChromeExtensions: ignoreChromeExtensionsOption = true,
trustedModules: additionallyTrustedModules = [],
permissions: permissionsOption = [],
allowInitFrom = 'root',
} = {}) => {
try {
if (isInitialized()) {
Expand All @@ -192,8 +193,12 @@ const init = async ({
}

const {name: callerModule} = getCurrentModule();
if (callerModule !== 'root') {
logger.warn('only root module may call init', callerModule);
if (
(allowInitFrom instanceof RegExp && !callerModule.match(allowInitFrom)) ||
(typeof allowInitFrom === 'string' && callerModule !== allowInitFrom) ||
(!(allowInitFrom instanceof RegExp) && typeof allowInitFrom !== 'string')
) {
logger.warn(`only root or specified module may call init (called from ${callerModule})`);
return;
}

Expand Down Expand Up @@ -248,7 +253,7 @@ const init = async ({
const {name: module, stack, error} = getCurrentModule();
logger.debug(`${module} called ${family.name}.${method.name} with`, args);
const allowed = isModuleAllowedToExecute(module, family, method);
if (devMode) {
if (true || devMode) {
const event = {
module,
family: family.name,
Expand Down Expand Up @@ -321,7 +326,7 @@ const init = async ({
};

const getHistory = () => {
if (devMode) {
if (true || devMode) {
return history;
}

Expand Down
6 changes: 5 additions & 1 deletion tests/node/fs/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ module.exports = () =>
test('appendFile', (done) => {
fs.appendFile(newTestFilePath, 'test', () => {
fs.unlink(newTestFilePath, () => {
expectCallToMatch({family: 'fs', method: 'appendFile', firstArg: newTestFilePath});
expectCallToMatch({
family: 'fs',
method: 'appendFile',
firstArg: newTestFilePath,
});
done();
});
});
Expand Down
26 changes: 0 additions & 26 deletions tests/node/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'access',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -35,7 +34,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'appendFile',
firstArg: newTestFilePath,
fromRoot: true,
});
standardFs.unlinkSync(newTestFilePath);
});
Expand All @@ -47,7 +45,6 @@ module.exports = () =>
method: 'chmod',
firstArg: testFilePath,
secondArg: testFileStats.mode,
fromRoot: true,
});
});

Expand All @@ -58,7 +55,6 @@ module.exports = () =>
method: 'chown',
firstArg: testFilePath,
secondArg: testFileStats.uid,
fromRoot: true,
});
});

Expand All @@ -70,7 +66,6 @@ module.exports = () =>
method: 'copyFile',
firstArg: testFilePath,
secondArg: newTestFilePath,
fromRoot: true,
});
standardFs.unlinkSync(newTestFilePath);
});
Expand All @@ -83,7 +78,6 @@ module.exports = () =>
method: 'cp',
firstArg: testFilePath,
secondArg: newTestFilePath,
fromRoot: true,
});
standardFs.unlinkSync(newTestFilePath);
});
Expand All @@ -94,7 +88,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'lchown',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -105,7 +98,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'lstat',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -116,7 +108,6 @@ module.exports = () =>
method: 'lutimes',
firstArg: testFilePath,
secondArg: testFileStats.atime,
fromRoot: true,
});
});

Expand All @@ -128,7 +119,6 @@ module.exports = () =>
method: 'link',
firstArg: testFilePath,
secondArg: newTestFilePath,
fromRoot: true,
});
standardFs.unlinkSync(newTestFilePath);
});
Expand All @@ -140,7 +130,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'mkdir',
firstArg: newTestDirPath,
fromRoot: true,
});
standardFs.rmdirSync(newTestDirPath);
});
Expand All @@ -152,7 +141,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'mkdtemp',
firstArg: __dirname,
fromRoot: true,
});
await fs.rmdir(dirPath);
});
Expand All @@ -163,7 +151,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'open',
firstArg: testFilePath,
fromRoot: true,
});
await fd.close();
});
Expand All @@ -174,7 +161,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'opendir',
firstArg: testDirPath,
fromRoot: true,
});
await dir.close();
});
Expand All @@ -186,7 +172,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'readdir',
firstArg: testDirPath,
fromRoot: true,
});
});

Expand All @@ -197,7 +182,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'readFile',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -208,7 +192,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'realpath',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -220,7 +203,6 @@ module.exports = () =>
method: 'rename',
firstArg: testFilePath,
secondArg: newTestFilePath,
fromRoot: true,
});
await fs.rename(newTestFilePath, testFilePath);
});
Expand All @@ -233,7 +215,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'rm',
firstArg: newTestFilePath,
fromRoot: true,
});
});

Expand All @@ -244,7 +225,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'stat',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -254,7 +234,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'symlink',
firstArg: testFilePath,
fromRoot: true,
});

standardFs.unlinkSync(newTestFilePath);
Expand All @@ -266,7 +245,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'truncate',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -278,7 +256,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'unlink',
firstArg: newTestFilePath,
fromRoot: true,
});
});

Expand All @@ -288,7 +265,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'utimes',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -298,7 +274,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'watch',
firstArg: testFilePath,
fromRoot: true,
});
});

Expand All @@ -308,7 +283,6 @@ module.exports = () =>
family: 'fs/promises',
method: 'writeFile',
firstArg: testFilePath,
fromRoot: true,
});
});
});
2 changes: 1 addition & 1 deletion tests/node/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const process = require('process');
const Sandworm = require('../../dist/index');
const {expectCallToMatch, loadSandworm, testif} = require('../utils');

describe('os', () => {
describe('process', () => {
beforeAll(loadSandworm);
afterEach(() => Sandworm.clearHistory());

Expand Down
6 changes: 3 additions & 3 deletions tests/node/timers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ describe('timers', () => {
describe('timersPromises', () => {
test('setImmediate', async () => {
await timersPromises.setImmediate('');
expectCallToMatch({family: 'timers/promises', method: 'setImmediate', fromRoot: true});
expectCallToMatch({family: 'timers/promises', method: 'setImmediate'});
});

testif(timersPromises.setInterval)('setInterval', async () => {
await timersPromises.setInterval();
expectCallToMatch({family: 'timers/promises', method: 'setInterval', fromRoot: true});
expectCallToMatch({family: 'timers/promises', method: 'setInterval'});
});

test('setTimeout', async () => {
await timersPromises.setTimeout();
expectCallToMatch({family: 'timers/promises', method: 'setTimeout', fromRoot: true});
expectCallToMatch({family: 'timers/promises', method: 'setTimeout'});
});
});
}
Expand Down
10 changes: 6 additions & 4 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const callExpects = ({call, family, method, firstArg, secondArg}) => {

const expectCallToMatch = ({family, method, firstArg, secondArg, index = 0, fromRoot = false}) => {
const call = Sandworm.getHistory().filter(
({module}) => module === (fromRoot ? 'root' : 'jest-circus'),
({module}) => module === (fromRoot ? 'root' : 'jest-runner>jest-circus'),
)[index];

callExpects({call, family, method, firstArg, secondArg});
Expand Down Expand Up @@ -61,15 +61,17 @@ const expectWebWorkerCallToMatch = async ({
callExpects({call, family, method, firstArg, secondArg});
};

const loadSandworm = async () => Sandworm.init({devMode: true, skipTracking: true});
const loadSandworm = async () =>
Sandworm.init({devMode: true, skipTracking: true, allowInitFrom: /jest-circus/});

const loadSandwormInProductionMode = async () =>
Sandworm.init({
devMode: false,
skipTracking: true,
allowInitFrom: /jest-circus/,
permissions: [
{module: 'jest-circus>expect', permissions: false},
{module: 'jest-circus', permissions: false},
{module: 'jest-runner>jest-circus>expect', permissions: false},
{module: 'jest-runner>jest-circus', permissions: false},
{module: /jest/, permissions: true},
{module: /istanbul/, permissions: true},
{module: /babel/, permissions: true},
Expand Down

0 comments on commit 00fa82f

Please sign in to comment.