Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@W-9729358@ Make CPD engine production ready #522

Merged
merged 1 commit into from Aug 16, 2021
Merged

Conversation

rmohan20
Copy link
Collaborator

No description provided.

export const CpdRuleCategory = 'Copy/Paste Detected';
export const CpdInfoUrl = 'https://pmd.github.io/latest/pmd_userdocs_cpd.html#refactoring-duplicates';
export const CpdViolationSeverity = Severity.LOW;
export const CpdLanguagesSupported: LANGUAGE[] = [...new Set (FileExtToLanguage.values())];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, languages supported will be deduced from the file extensions to languages mapping.

}
this.logger = await Logger.child(this.getName());
this.config = await Controller.getConfig();
this.minimumTokens = EnvVariable.getEnvVariableAsNumber(this.ENGINE_ENUM, EnvVariable.CONFIG_NAME.MINIMUM_TOKENS) || ( await this.config.getMinimumTokens(this.ENGINE_ENUM) );
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment variable would override config value for MinimumTokens.


// Let user know about file paths that could not be matched
if (unmatchedPaths.length > 0) {
uxEvents.emit('info-verbose', eventMessages.getMessage('info.unmatchedPathExtensionCpd', [unmatchedPaths.join(",")]));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified logic in sortPaths to emit information in only one path.

Comment on lines +11 to +13
export enum CONFIG_NAME {
MINIMUM_TOKENS = 'Minimum_Tokens'
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New enum to track config values that can be fetched through environment variables. I've also attempted to standardize the access in this new module.

@@ -0,0 +1,22 @@
public class SomeApex1 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New code fixtures to test CpdEngine

return process.env[envVariableName];
}

export function getEnvVariableAsNumber(engine: ENGINE, configName: CONFIG_NAME): number {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will currently return NaN when the environment variable isn't set or isn't a number.

Do we want it to return NaN if it's set, but not a number, but null if it isn't set?

Whatever we choose, can you add documentation describing the contract when the value isn't set or the value can't be parsed?

Copy link
Collaborator Author

@rmohan20 rmohan20 Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. We don't have a way to control what value the user may set. I see a suggestion on online forums to clean up the string before parsing:

parseInt(text.replace(/\D/g, ""),10);

where \D is a non-digit. Will add documentation for this as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbartolotta-sfdc Let me know what you think about the fix.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmohan20 - this change looks good.

* @returns true if the language was identifed and false if not
*/
private matchPathToLanguage(path: string, languageToPaths: Map<LANGUAGE, string[]>): boolean {
const ext = path.slice(path.lastIndexOf(".") + 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this lowercase ext in case the user has a file named foo.Java or something else unexpected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this. Will fix it.

@rmohan20
Copy link
Collaborator Author

Tarball test went well on both Windows and Linux. Reverting the changes for CPD in smoke-test script.

@ranekere-sfdc ranekere-sfdc linked an issue Aug 12, 2021 that may be closed by this pull request
src/lib/cpd/CpdEngine.ts Show resolved Hide resolved
test/lib/cpd/CpdEngine.test.ts Outdated Show resolved Hide resolved
test/lib/cpd/CpdEngine.test.ts Show resolved Hide resolved
@jfeingold35
Copy link
Collaborator

jfeingold35 commented Aug 12, 2021 via email

@@ -52,22 +25,26 @@ 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 as any).sortPaths(targets)).to.eql(langPathMap);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfeingold35 @jbartolotta-sfdc As per your suggestions, I've restored the as any call for sortPaths and made it a private method again. The remaining as any calls are on public methods and they are already accessible directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CPD Scan
3 participants