Skip to content

Commit

Permalink
Handling non numeric env variable, file extensions in mixed case
Browse files Browse the repository at this point in the history
  • Loading branch information
rmohan20 committed Aug 12, 2021
1 parent 9e78043 commit c12bdd4
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/lib/cpd/CpdEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class CpdEngine extends AbstractRuleEngine {
private matchPathToLanguage(path: string, languageToPaths: Map<LANGUAGE, string[]>): boolean {
const ext = path.slice(path.lastIndexOf(".") + 1);
if (ext) {
const language = FileExtToLanguage.get(ext);
const language = FileExtToLanguage.get(ext.toLowerCase());
if (language && CpdLanguagesSupported.includes(language)) {
if (languageToPaths.has(language)) {
languageToPaths.get(language).push(path);
Expand Down
7 changes: 6 additions & 1 deletion src/lib/util/EnvironmentVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ export function getEnvVariableAsString(engine: ENGINE, configName: CONFIG_NAME):
}

export function getEnvVariableAsNumber(engine: ENGINE, configName: CONFIG_NAME): number {
return parseInt(getEnvVariableAsString(engine, configName), 10);
const envVariable = getEnvVariableAsString(engine, configName);
if (envVariable) {
// Clean up variable if it has any non-digit values
return parseInt(envVariable.replace(/\D/g, ""), 10);
}
return undefined;
}
161 changes: 118 additions & 43 deletions test/commands/scanner/e2e.cpd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,63 +65,111 @@ describe("End to end tests for CPD engine", () => {

});

describe("Minimum tokens value from env", () => {
before(() => {
process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'] = '50';
});
describe("Processing Minimum Tokens value", () => {

describe("Minimum tokens value from env", () => {
before(() => {
process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'] = '50';
});

after(() => {
delete process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'];
});

after(() => {
delete process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'];
setupCommandTest
.command([
"scanner:run",
"--target", Cpd_Test_Code_Path,
"--engine", ENGINE.CPD,
"--format", "json"
])
.it("Picks up minimum tokens from Environment variable when found", ctx => {
verifyEnvVarIsUsedForMinimumTokens(ctx);
});
});

setupCommandTest
.command([
"scanner:run",
"--target", Cpd_Test_Code_Path,
"--engine", ENGINE.CPD,
"--format", "json"
])
.it("Picks up minimum tokens from Environment variable when found", ctx => {
const Minimum_Tokens_50 = [Apex_File1, Apex_File2, Vf_File1, Vf_File2].sort();
const ruleResults: RuleResult[] = extractRuleResults(ctx);
describe("Minimum tokens value from config", () => {

const actualFileNames = ruleResults.map(ruleResult => ruleResult.fileName).sort();
expect(ruleResults.length).equals(Minimum_Tokens_50.length);
before(() => {
// delete property if it exists
delete process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'];
});

for (let i=0; i<ruleResults.length; i++) {
// Comparing substring since actualFileName contains full path and Minimum_Tokens_50 contains relative paths
expect(actualFileNames[i]).contains(Minimum_Tokens_50[i]);
}
setupCommandTest
.command([
"scanner:run",
"--target", Cpd_Test_Code_Path,
"--engine", ENGINE.CPD,
"--format", "json"
])
.it("Picks up minimum tokens from Config when Environment variable is not found", ctx => {
verifyDefaultConfigIsUsedForMinimumTokens(ctx);
});
});

describe("Invalid String Minimum tokens value from env", () => {
before(() => {
process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'] = 'Some String';
});
});

describe("Minimum tokens value from config", () => {
after(() => {
delete process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'];
});

before(() => {
// delete property if it exists
delete process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'];
setupCommandTest
.command([
"scanner:run",
"--target", Cpd_Test_Code_Path,
"--engine", ENGINE.CPD,
"--format", "json"
])
.it("Uses config value when environment variable is not a number", ctx => {
verifyDefaultConfigIsUsedForMinimumTokens(ctx);
});
});

setupCommandTest
.command([
"scanner:run",
"--target", Cpd_Test_Code_Path,
"--engine", ENGINE.CPD,
"--format", "json"
])
.it("Picks up minimum tokens from Config when Environment variable is not found", ctx => {
const Minimum_Tokens_100 = [Vf_File1, Vf_File2].sort();
describe("Invalid empty Minimum tokens value from env", () => {
before(() => {
process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'] = '';
});

const ruleResults: RuleResult[] = extractRuleResults(ctx);
after(() => {
delete process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'];
});

const actualFileNames = ruleResults.map(ruleResult => ruleResult.fileName).sort();
expect(ruleResults.length).equals(Minimum_Tokens_100.length);
setupCommandTest
.command([
"scanner:run",
"--target", Cpd_Test_Code_Path,
"--engine", ENGINE.CPD,
"--format", "json"
])
.it("Uses config value when environment variable is not empty", ctx => {
verifyDefaultConfigIsUsedForMinimumTokens(ctx);
});
});

for (let i=0; i<ruleResults.length; i++) {
// Comparing substring since actualFileName contains full path and Minimum_Tokens_100 contains relative paths
expect(actualFileNames[i]).contains(Minimum_Tokens_100[i]);
}
describe("String and digit Minimum tokens value from env", () => {
before(() => {
process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'] = 'My5String0';
});

after(() => {
delete process.env['SFDX_SCANNER_CPD_MINIMUM_TOKENS'];
});

setupCommandTest
.command([
"scanner:run",
"--target", Cpd_Test_Code_Path,
"--engine", ENGINE.CPD,
"--format", "json"
])
.it("Uses config value when environment variable is not a number", ctx => {
verifyEnvVarIsUsedForMinimumTokens(ctx);
});
});

});

describe("Violation message content", () => {
Expand Down Expand Up @@ -198,6 +246,33 @@ describe("End to end tests for CPD engine", () => {
});
});
});
function verifyEnvVarIsUsedForMinimumTokens(ctx) {
const Minimum_Tokens_50 = [Apex_File1, Apex_File2, Vf_File1, Vf_File2].sort();
const ruleResults: RuleResult[] = extractRuleResults(ctx);

const actualFileNames = ruleResults.map(ruleResult => ruleResult.fileName).sort();
expect(ruleResults.length).equals(Minimum_Tokens_50.length);

for (let i = 0; i < ruleResults.length; i++) {
// Comparing substring since actualFileName contains full path and Minimum_Tokens_50 contains relative paths
expect(actualFileNames[i]).contains(Minimum_Tokens_50[i]);
}
}

function verifyDefaultConfigIsUsedForMinimumTokens(ctx) {
const Minimum_Tokens_100 = [Vf_File1, Vf_File2].sort();

const ruleResults: RuleResult[] = extractRuleResults(ctx);

const actualFileNames = ruleResults.map(ruleResult => ruleResult.fileName).sort();
expect(ruleResults.length).equals(Minimum_Tokens_100.length);

for (let i = 0; i < ruleResults.length; i++) {
// Comparing substring since actualFileName contains full path and Minimum_Tokens_100 contains relative paths
expect(actualFileNames[i]).contains(Minimum_Tokens_100[i]);
}
}

function extractRuleResults(ctx) {
const stdout = ctx.stdout;
const jsonOutput = stdout.slice(stdout.indexOf('['), stdout.lastIndexOf(']') + 1);
Expand Down
13 changes: 12 additions & 1 deletion test/lib/cpd/CpdEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,24 @@ describe('CpdEngine', () => {
const targets: RuleTarget[] = [{
target: '',
paths: ['file.cls','file.trigger','file.page','file.py','file.component']
}]
}];

let langPathMap: Map<LANGUAGE, string[]> = new Map();
langPathMap.set(LANGUAGE.APEX, ['file.cls','file.trigger']);
langPathMap.set(LANGUAGE.VISUALFORCE, ['file.page','file.component']);
expect(testEngine.sortPaths(targets)).to.eql(langPathMap);
});

it('should compare extensions case-insensitively', () => {
const targets: RuleTarget[] = [{
target: '',
paths: ['file.Cls','file.tRIGGER']
}];

let langPathMap: Map<LANGUAGE, string[]> = new Map();
langPathMap.set(LANGUAGE.APEX, ['file.Cls','file.tRIGGER']);
expect(testEngine.sortPaths(targets)).to.eql(langPathMap);
});
});


Expand Down

0 comments on commit c12bdd4

Please sign in to comment.