Skip to content

Commit

Permalink
fix: refactor afterAll hook
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Oct 3, 2022
1 parent 82c251b commit 19e55bb
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ const {
const appPath = process.env.SANDWORM_APP_PATH || path.join(__dirname, '..', '..', '..');
const config = loadConfig(appPath);

const getPermissions = (activity, devDependencies, prodDependencies) => {
const ignoredModules =
config && Array.isArray(config.ignoredModules) ? config.ignoredModules : [];
const permissions = getPermissionsFromActivity(activity);

const currentPermissions = loadPermissions(appPath);
const newPermissions = getPackagePermissions({
permissions,
devDependencies,
prodDependencies,
ignoredModules,
});

return {currentPermissions, newPermissions};
}

loadSandworm({config, trustedModules: ['mocha']});

module.exports = {
Expand All @@ -31,44 +47,32 @@ module.exports = {
const activity = getRecordedActivity();
logger.log(`Intercepted ${activity.length} events`);
stopRecordingSandwormActivity(() => {
(async () => {
loadDependencies(appPath, ([devDependencies, prodDependencies]) => {
const ignoredModules =
config && Array.isArray(config.ignoredModules) ? config.ignoredModules : [];
const permissions = getPermissionsFromActivity(activity);
loadDependencies(appPath, ([devDependencies, prodDependencies]) => {
const {currentPermissions, newPermissions} = getPermissions(activity, devDependencies, prodDependencies);

const currentPermissions = loadPermissions(appPath);
const newPermissions = getPackagePermissions({
permissions,
devDependencies,
prodDependencies,
ignoredModules,
if (!currentPermissions) {
writePermissions(appPath, newPermissions, () => {
logger.logTestPluginFirstRunMessage();
done();
});
} else {
const {changes, messages} = comparePermissions(currentPermissions, newPermissions);

if (!currentPermissions) {
writePermissions(appPath, newPermissions, () => {
logger.logTestPluginFirstRunMessage();
done();
});
} else {
const {changes, messages} = comparePermissions(currentPermissions, newPermissions);

if (changes.length === 0) {
logger.success('✔ Permission snapshot matches current test run');
}

done(
changes.length > 0
? new Error(
`Sandworm: Permission mismatch:\n${messages.join(
'\n',
)}\nPlease verify and update the \`${SANDWORM_PERMISSION_FILE_NAME}\` file.`,
)
: undefined,
);
if (changes.length === 0) {
logger.success('✔ Permission snapshot matches current test run');
}
});
})();

done(
changes.length > 0
? new Error(
`Sandworm: Permission mismatch:\n${messages.join(
'\n',
)}\nPlease verify and update the \`${SANDWORM_PERMISSION_FILE_NAME}\` file.`,
)
: undefined,
);
}
});
});
},
},
Expand Down

0 comments on commit 19e55bb

Please sign in to comment.