Skip to content

Commit

Permalink
test: update unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov committed Dec 17, 2023
1 parent 76b2a70 commit ea8a8e1
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 62 deletions.
36 changes: 20 additions & 16 deletions src/codeActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { RHDA_DIAGNOSTIC_SOURCE } from './constants';

let codeActionsMap: Map<string, CodeAction[]> = new Map<string, CodeAction[]>();

/**
* Gets the code actions map.
*/
function getCodeActionsMap(): Map<string, CodeAction[]> {
return codeActionsMap;
}

/**
* Clears the code actions map.
*/
Expand All @@ -35,26 +42,15 @@ function registerCodeAction(key: string, codeAction: CodeAction) {
* @param uri - The URI of the file being analyzed.
* @returns An array of CodeAction objects to be made available to the user.
*/
function getDiagnosticsCodeActions(diagnostics: Diagnostic[], uri: string): CodeAction[] {
function getDiagnosticsCodeActions(diagnostics: Diagnostic[]): CodeAction[] {
let hasRhdaDiagonostic: boolean = false;
const codeActions: CodeAction[] = [];

for (const diagnostic of diagnostics) {
const diagnosticCodeActions = codeActionsMap[diagnostic.range.start.line + '|' + diagnostic.range.start.character];
if (diagnosticCodeActions && diagnosticCodeActions.length !== 0) {

if (path.basename(uri) === 'pom.xml') {
diagnosticCodeActions.forEach(codeAction => {
codeAction.command = {
title: 'RedHat repository recommendation',
command: globalConfig.triggerRHRepositoryRecommendationNotification,
};
});
}

codeActions.push(...diagnosticCodeActions);

}
const key = `${diagnostic.range.start.line}|${diagnostic.range.start.character}`;
const diagnosticCodeActions = codeActionsMap[key] || [];
codeActions.push(...diagnosticCodeActions);

hasRhdaDiagonostic ||= diagnostic.source === RHDA_DIAGNOSTIC_SOURCE;
}
Expand Down Expand Up @@ -104,7 +100,15 @@ function generateSwitchToRecommendedVersionAction(title: string, versionReplacem
range: diagnostic.range,
newText: versionReplacementString
}];

if (diagnostic.severity !== 1 && path.basename(uri) === 'pom.xml') {
codeAction.command = {
title: 'RedHat repository recommendation',
command: globalConfig.triggerRHRepositoryRecommendationNotification,
};
}

return codeAction;
}

export { clearCodeActionsMap, registerCodeAction , generateSwitchToRecommendedVersionAction, getDiagnosticsCodeActions };
export { getCodeActionsMap, clearCodeActionsMap, registerCodeAction , generateSwitchToRecommendedVersionAction, getDiagnosticsCodeActions };
2 changes: 1 addition & 1 deletion src/componentAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class AnalysisResponse implements IAnalysisResponse {
* @private
*/
private getRemediation(issue: any): string {
return isDefined(issue, 'remediation', 'trustedContent', 'package') ? issue.remediation.trustedContent.package : '';
return isDefined(issue, 'remediation', 'trustedContent', 'ref') ? issue.remediation.trustedContent.ref.split('?')[0] : '';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/diagnosticsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Vulnerability } from './vulnerability';
import { connection } from './server';
import { VERSION_PLACEHOLDER } from './constants';
import { clearCodeActionsMap, registerCodeAction, generateSwitchToRecommendedVersionAction } from './codeActionHandler';
import { IPositionedContext } from './collector'
import { IPositionedContext } from './collector';

/**
* Diagnostics Pipeline specification.
Expand Down Expand Up @@ -108,7 +108,7 @@ class DiagnosticsPipeline implements IDiagnosticsPipeline {
* @private
*/
private createCodeAction(loc: string, ref: string, context: IPositionedContext, sourceId: string, vulnerabilityDiagnostic: Diagnostic) {
const switchToVersion = this.provider.resolveDependencyFromReference(ref).split('@')[1]
const switchToVersion = this.provider.resolveDependencyFromReference(ref).split('@')[1];
const versionReplacementString = context ? context.value.replace(VERSION_PLACEHOLDER, switchToVersion) : switchToVersion;
const title = `Switch to version ${switchToVersion} for ${sourceId}`;
const codeAction = generateSwitchToRecommendedVersionAction(title, versionReplacementString, vulnerabilityDiagnostic, this.diagnosticFilePath);
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ connection.onDidChangeConfiguration(() => {
* Handles code action requests from client.
*/
connection.onCodeAction((params): CodeAction[] => {
return getDiagnosticsCodeActions(params.context.diagnostics, params.textDocument.uri);
return getDiagnosticsCodeActions(params.context.diagnostics);
});

connection.listen();
Expand Down
Loading

0 comments on commit ea8a8e1

Please sign in to comment.