fix(providence-analytics): better allowlist-mode detection

This commit is contained in:
Thijs Louisse 2022-03-30 19:02:43 +02:00 committed by Thijs Louisse
parent 64bdddf6b7
commit 17dadabfbf
3 changed files with 47 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'providence-analytics': patch
---
providence-analytics: enhanced allowlistMode detection

View file

@ -423,6 +423,16 @@ class InputDataService {
return { globPattern };
}
/**
* Gets allowlist mode that determines which files to analyze
* @param {PathFromSystemRoot} startPath - local filesystem path
* @returns {'git'|'npm'}
*/
static _determineAllowListMode(startPath) {
const isNodeModule = /^.*\/(node_modules\/@.*|node_modules)\/.*$/.test(startPath);
return isNodeModule ? 'npm' : 'git';
}
/**
* Gets an array of files for given extension
* @param {PathFromSystemRoot} startPath - local filesystem path
@ -453,9 +463,7 @@ class InputDataService {
let gitIgnorePaths = [];
/** @type {string[]} */
let npmPackagePaths = [];
const hasGitIgnore = getGitignoreFile(startPath);
const allowlistMode = cfg.allowlistMode || (hasGitIgnore ? 'git' : 'npm');
const allowlistMode = cfg.allowlistMode || this._determineAllowListMode(startPath);
if (allowlistMode === 'git') {
gitIgnorePaths = getGitIgnorePaths(startPath);

View file

@ -349,9 +349,7 @@ build/
'./package.json': JSON.stringify({
files: ['dist'],
}),
'.gitignore': `
/dist
`,
'.gitignore': '/dist',
});
const globOutput = InputDataService.gatherFilesFromDir('/fictional/project');
expect(globOutput).to.eql([
@ -372,6 +370,36 @@ build/
// This means allowlistMode is 'npm'
'/fictional/project/dist/bundle.js',
]);
mockProject(
{ './dist/bundle.js': '', '.gitignore': '/dist' },
{
projectName: 'detect-as-npm',
projectPath: '/inside/proj/with/node_modules/detect-as-npm',
},
);
const globOutput3 = InputDataService.gatherFilesFromDir(
'/inside/proj/with/node_modules/detect-as-npm',
);
expect(globOutput3).to.eql([
// This means allowlistMode is 'npm' (even though we found .gitignore)
'/inside/proj/with/node_modules/detect-as-npm/dist/bundle.js',
]);
mockProject(
{ './dist/bundle.js': '', '.gitignore': '/dist' },
{
projectName: '@scoped/detect-as-npm',
projectPath: '/inside/proj/with/node_modules/@scoped/detect-as-npm',
},
);
const globOutput4 = InputDataService.gatherFilesFromDir(
'/inside/proj/with/node_modules/@scoped/detect-as-npm',
);
expect(globOutput4).to.eql([
// This means allowlistMode is 'npm' (even though we found .gitignore)
'/inside/proj/with/node_modules/@scoped/detect-as-npm/dist/bundle.js',
]);
});
it('custom "allowlist" will take precedence over "allowlistMode"', async () => {