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 May 2, 2024
1 parent db64378 commit 38c8447
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 40 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dist"
],
"dependencies": {
"@RHEcosystemAppEng/exhort-javascript-api": "^0.1.1-ea.31",
"@RHEcosystemAppEng/exhort-javascript-api": "^0.1.1-ea.34",
"@xml-tools/ast": "^5.0.5",
"@xml-tools/parser": "^1.0.11",
"json-to-ast": "^2.1.0",
Expand Down
18 changes: 18 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ export const RHDA_DIAGNOSTIC_SOURCE = 'Red Hat Dependency Analytics Plugin';
* Placeholder used as a version for dependency templates.
*/
export const VERSION_PLACEHOLDER: string = '__VERSION__';
/**
* Represents provider ecosystem names.
*/
export const GRADLE = 'gradle';
export const MAVEN = 'maven';
export const GOLANG = 'golang';
export const NPM = 'npm';
export const PYPI = 'pypi';
/**
* An object mapping ecosystem names to their true ecosystems.
*/
export const ecosystemNameMappings: { [key: string]: string } = {
[GRADLE]: MAVEN,
[MAVEN]: MAVEN,
[GOLANG]: GOLANG,
[NPM]: NPM,
[PYPI]: PYPI,
};
24 changes: 21 additions & 3 deletions src/dependencyAnalysis/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Range } from 'vscode-languageserver';

import { IPositionedString, IPositionedContext, IPosition } from '../positionTypes';
import { isDefined } from '../utils';
import { ecosystemNameMappings, GRADLE } from '../constants';

/**
* Represents a dependency specification.
Expand Down Expand Up @@ -35,8 +36,11 @@ export class Dependency implements IDependency {
*/
export class DependencyMap {
mapper: Map<string, IDependency>;
constructor(deps: IDependency[]) {
this.mapper = new Map(deps.map(d => [d.name.value, d]));
constructor(deps: IDependency[], ecosystem: string) {
this.mapper = new Map(deps.map(d => {
const key = ecosystem === GRADLE && d.version ? `${d.name.value}@${d.version.value}` : d.name.value;
return [key, d];
}));
}

/**
Expand Down Expand Up @@ -67,6 +71,12 @@ export interface IDependencyProvider {
* @returns The resolved name of the dependency.
*/
resolveDependencyFromReference(ref: string): string;

/**
* Gets the name of the providers ecosystem.
* @returns The name of the providers ecosystem.
*/
getEcosystem(): string;
}

/**
Expand All @@ -85,7 +95,15 @@ export class EcosystemDependencyResolver {
* @returns The resolved name of the dependency.
*/
resolveDependencyFromReference(ref: string): string {
return ref.replace(`pkg:${this.ecosystem}/`, '');
return ref.replace(`pkg:${ecosystemNameMappings[this.ecosystem]}/`, '');
}

/**
* Gets the name of the ecosystem this provider is configured for.
* @returns The name of the ecosystem.
*/
getEcosystem(): string {
return this.ecosystem;
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/dependencyAnalysis/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IPositionedContext } from '../positionTypes';
import { executeComponentAnalysis, DependencyData } from './analysis';
import { Vulnerability } from '../vulnerability';
import { connection } from '../server';
import { VERSION_PLACEHOLDER } from '../constants';
import { VERSION_PLACEHOLDER, GRADLE } from '../constants';
import { clearCodeActionsMap, registerCodeAction, generateSwitchToRecommendedVersionAction } from '../codeActionHandler';
import { decodeUriPath } from '../utils';
import { AbstractDiagnosticsPipeline } from '../diagnosticsPipeline';
Expand All @@ -37,10 +37,11 @@ class DiagnosticsPipeline extends AbstractDiagnosticsPipeline<DependencyData> {
/**
* Runs diagnostics on dependencies.
* @param dependencies - A map containing dependency data by reference string.
* @param ecosystem - The name of the ecosystem in which dependencies are being analyzed.
*/
runDiagnostics(dependencies: Map<string, DependencyData[]>) {
runDiagnostics(dependencies: Map<string, DependencyData[]>, ecosystem: string) {
Object.entries(dependencies).map(([ref, dependencyData]) => {
const dependencyRef = ref.split('@')[0];
const dependencyRef = ecosystem === GRADLE ? ref : ref.split('@')[0];
const dependency = this.dependencyMap.get(dependencyRef);

if (dependency) {
Expand Down Expand Up @@ -101,7 +102,8 @@ class DiagnosticsPipeline extends AbstractDiagnosticsPipeline<DependencyData> {
async function performDiagnostics(diagnosticFilePath: string, contents: string, provider: IDependencyProvider) {
try {
const dependencies = await provider.collect(contents);
const dependencyMap = new DependencyMap(dependencies);
const ecosystem = provider.getEcosystem();
const dependencyMap = new DependencyMap(dependencies, ecosystem);

const diagnosticsPipeline = new DiagnosticsPipeline(dependencyMap, diagnosticFilePath);
diagnosticsPipeline.clearDiagnostics();
Expand All @@ -110,7 +112,7 @@ async function performDiagnostics(diagnosticFilePath: string, contents: string,

clearCodeActionsMap(diagnosticFilePath);

diagnosticsPipeline.runDiagnostics(response.dependencies);
diagnosticsPipeline.runDiagnostics(response.dependencies, ecosystem);

diagnosticsPipeline.reportDiagnostics();
} catch (error) {
Expand Down
5 changes: 3 additions & 2 deletions src/diagnosticsPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ interface IDiagnosticsPipeline<T> {
/**
* Runs diagnostics on dependencies.
* @param artifact - A map containing artifact data.
* @param ecosystem - The name of the ecosystem to analyze dependencies in.
*/
runDiagnostics(artifact: Map<string, T[]>);
runDiagnostics(artifact: Map<string, T[]>, ecosystem: string);
}

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ abstract class AbstractDiagnosticsPipeline<T> implements IDiagnosticsPipeline<T>
});
}

abstract runDiagnostics(artifact: Map<string, T[]>): void;
abstract runDiagnostics(artifact: Map<string, T[]>, ecosystem: string): void;
}

export { AbstractDiagnosticsPipeline };
31 changes: 16 additions & 15 deletions src/providers/build.gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* ------------------------------------------------------------------------------------------ */
'use strict';

import { VERSION_PLACEHOLDER } from '../constants';
import { VERSION_PLACEHOLDER, GRADLE } from '../constants';
import { IDependencyProvider, EcosystemDependencyResolver, IDependency, Dependency } from '../dependencyAnalysis/collector';

/**
Expand All @@ -20,14 +20,19 @@ export class DependencyProvider extends EcosystemDependencyResolver implements I
COMMENT_REGEX: RegExp = /\/\*[\s\S]*?\*\//g;

/**
* Regular expression for locating key value pairs in a string.
* Regular expression for locating key-value pairs in a string with colons as separators.
*/
FIND_KEY_VALUE_PAIRS_REGEX: RegExp = /\b(\w+)\s*:\s*(['"])(.*?)\2/g;
FIND_KEY_VALUE_PAIRS_WITH_COLON_REGEX: RegExp = /\b(\w+)\s*:\s*(['"])(.*?)\2/g;

/**
* Regular expression for locating key-value pairs in a string with equals signs as separators.
*/
FIND_KEY_VALUE_PAIRS_WITH_EQUALS_REGEX: RegExp = /\b(\w+)\s*=\s*(['"])(.*?)\2/;

/**
* Regular expression for matching key value pairs.
*/
SPLIT_KEY_VALUE_PAIRS_REGEX: RegExp = /\s*:\s*/;
SPLIT_KEY_VALUE_PAIRS_WITH_COLON_REGEX: RegExp = /\s*:\s*/;

/**
* Regular expression for matching strings enclosed in double or single quotes.
Expand Down Expand Up @@ -55,7 +60,7 @@ export class DependencyProvider extends EcosystemDependencyResolver implements I
ARGS_SCOPE: string = 'ext';

constructor() {
super('maven'); // set gradle ecosystem to 'maven'
super(GRADLE); // set ecosystem to 'gradle'
}

/**
Expand Down Expand Up @@ -92,11 +97,11 @@ export class DependencyProvider extends EcosystemDependencyResolver implements I
let depData: string;
let quoteUsed: string;

const keyValuePairs = cleanLine.match(this.FIND_KEY_VALUE_PAIRS_REGEX);
const keyValuePairs = cleanLine.match(this.FIND_KEY_VALUE_PAIRS_WITH_COLON_REGEX);
if (keyValuePairs) {
// extract data from dependency in Map format
keyValuePairs.forEach(pair => {
const [key, value] = pair.split(this.SPLIT_KEY_VALUE_PAIRS_REGEX);
const [key, value] = pair.split(this.SPLIT_KEY_VALUE_PAIRS_WITH_COLON_REGEX);
const match = value.match(this.BETWEEN_QUOTES_REGEX);
quoteUsed = match[1];
const valueData = match[2];
Expand Down Expand Up @@ -222,7 +227,7 @@ export class DependencyProvider extends EcosystemDependencyResolver implements I
}

if (isSingleArgument) {
if (parsedLine.includes('{')) {
if (parsedLine.startsWith('{')) {
isArgumentBlock = true;
}
isSingleArgument = false;
Expand All @@ -240,14 +245,10 @@ export class DependencyProvider extends EcosystemDependencyResolver implements I
if (parsedLine.includes('}')) {
isArgumentBlock = false;
}

if (!this.BETWEEN_QUOTES_REGEX.test(parsedLine)) {
return dependencies;
}

if (parsedLine.includes('=')) {
const argData = parsedLine.split('=');
this.args.set(argData[0].trim(), argData[1].trim().replace(this.BETWEEN_QUOTES_REGEX, '$2'));
const argDataMatch = parsedLine.match(this.FIND_KEY_VALUE_PAIRS_WITH_EQUALS_REGEX);
if (argDataMatch) {
this.args.set(argDataMatch[1].trim(), argDataMatch[3].trim());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/providers/go.mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

import { IDependencyProvider, EcosystemDependencyResolver, IDependency, Dependency } from '../dependencyAnalysis/collector';
import { GOLANG } from '../constants';

/* Please note :: There is an issue with the usage of semverRegex Node.js package in this code.
* Often times it fails to recognize versions that contain an added suffix, usually including extra details such as a timestamp and a commit hash.
Expand All @@ -27,7 +28,7 @@ export class DependencyProvider extends EcosystemDependencyResolver implements I
replacementMap: Map<string, IDependency> = new Map<string, IDependency>();

constructor() {
super('golang'); // set ecosystem to 'golang'
super(GOLANG); // set ecosystem to 'golang'
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/providers/package.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import jsonAst from 'json-to-ast';
import { IDependencyProvider, EcosystemDependencyResolver, IDependency, Dependency } from '../dependencyAnalysis/collector';
import { NPM } from '../constants';

/**
* Process entries found in the package.json file.
Expand All @@ -14,7 +15,7 @@ export class DependencyProvider extends EcosystemDependencyResolver implements I
classes: string[] = ['dependencies'];

constructor() {
super('npm'); // set ecosystem to 'npm'
super(NPM); // set ecosystem to 'npm'
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/providers/pom.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import { IDependencyProvider, EcosystemDependencyResolver, IDependency, Dependency } from '../dependencyAnalysis/collector';
import { parse, DocumentCstNode } from '@xml-tools/parser';
import { buildAst, accept, XMLElement, XMLDocument } from '@xml-tools/ast';
import { VERSION_PLACEHOLDER } from '../constants';
import { VERSION_PLACEHOLDER, MAVEN } from '../constants';

/**
* Process entries found in the pom.xml file.
*/
export class DependencyProvider extends EcosystemDependencyResolver implements IDependencyProvider {

constructor() {
super('maven'); // set ecosystem to 'maven'
super(MAVEN); // set ecosystem to 'maven'
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/providers/requirements.txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* ------------------------------------------------------------------------------------------ */
'use strict';

import { PYPI } from '../constants';
import { IDependencyProvider, EcosystemDependencyResolver, IDependency, Dependency } from '../dependencyAnalysis/collector';

/**
Expand All @@ -12,7 +13,7 @@ import { IDependencyProvider, EcosystemDependencyResolver, IDependency, Dependen
export class DependencyProvider extends EcosystemDependencyResolver implements IDependencyProvider {

constructor() {
super('pypi'); // set ecosystem to 'pypi'
super(PYPI); // set ecosystem to 'pypi'
}

/**
Expand Down
Loading

0 comments on commit 38c8447

Please sign in to comment.