Skip to content

Commit

Permalink
test: add and update unit tests (#226)
Browse files Browse the repository at this point in the history
Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov authored Nov 27, 2023
1 parent 8deb87a commit c7fc8c9
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 53 deletions.
23 changes: 13 additions & 10 deletions src/componentAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* ------------------------------------------------------------------------------------------ */
'use strict';

import * as path from 'path';
import exhort from '@RHEcosystemAppEng/exhort-javascript-api';

import { connection } from './server';
import { globalConfig } from './config';
import { isDefined } from './utils';

import exhort from '@RHEcosystemAppEng/exhort-javascript-api';

/**
* Represents a source object with an ID and dependencies array.
*/
Expand Down Expand Up @@ -61,7 +62,7 @@ interface IAnalysisResponse {
class AnalysisResponse implements IAnalysisResponse {
dependencies: Map<string, DependencyData[]> = new Map<string, DependencyData[]>();

constructor(resData: exhort.AnalysisReport) {
constructor(resData: exhort.AnalysisReport, diagnosticFilePath: string) {

const failedProviders: string[] = [];
const sources: Source[] = [];
Expand All @@ -79,8 +80,11 @@ class AnalysisResponse implements IAnalysisResponse {

if (failedProviders.length !== 0) {
const errMsg = `The component analysis couldn't fetch data from the following providers: [${failedProviders.join(', ')}]`;
connection.console.warn(errMsg);
connection.sendNotification('caSimpleWarning', errMsg);
connection.console.warn(`Component Analysis Error: ${errMsg}`);
connection.sendNotification('caError', {
error: errMsg,
uri: diagnosticFilePath,
});
}

sources.forEach(source => {
Expand All @@ -104,13 +108,12 @@ class AnalysisResponse implements IAnalysisResponse {
* @param contents - The contents of the manifest file to analyze.
* @returns A Promise resolving to an AnalysisResponse object.
*/
async function componentAnalysisService (fileType: string, contents: string): Promise<AnalysisResponse> {
async function executeComponentAnalysis (diagnosticFilePath: string, contents: string): Promise<AnalysisResponse> {

// Define configuration options for the component analysis request
const options = {
'RHDA_TOKEN': globalConfig.telemetryId,
'RHDA_SOURCE': globalConfig.utmSource,
'EXHORT_DEV_MODE': globalConfig.exhortDevMode,
'MATCH_MANIFEST_VERSIONS': globalConfig.matchManifestVersions,
'EXHORT_MVN_PATH': globalConfig.exhortMvnPath,
'EXHORT_NPM_PATH': globalConfig.exhortNpmPath,
Expand All @@ -128,9 +131,9 @@ async function componentAnalysisService (fileType: string, contents: string): Pr
options['EXHORT_OSS_INDEX_TOKEN'] = globalConfig.exhortOSSIndexToken;
}

const componentAnalysisJson = await exhort.componentAnalysis(fileType, contents, options); // Execute component analysis
const componentAnalysisJson = await exhort.componentAnalysis(path.basename(diagnosticFilePath), contents, options); // Execute component analysis

return new AnalysisResponse(componentAnalysisJson);
return new AnalysisResponse(componentAnalysisJson, diagnosticFilePath);
}

export { componentAnalysisService, DependencyData };
export { executeComponentAnalysis, DependencyData };
32 changes: 15 additions & 17 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
/**
* Represents the global configuration settings.
*/
export class Config
class Config
{
triggerFullStackAnalysis: string;
telemetryId: string;
utmSource: string;
exhortDevMode: string;
exhortSnykToken: string;
exhortOSSIndexUser: string;
exhortOSSIndexToken: string;
matchManifestVersions: string;
exhortMvnPath: string;
exhortNpmPath: string;
exhortGoPath: string;
exhortPython3Path: string;
exhortPip3Path: string;
exhortPythonPath: string;
exhortPipPath: string;
telemetryId: string;
utmSource: string;
exhortSnykToken: string;
exhortOSSIndexUser: string;
exhortOSSIndexToken: string;
matchManifestVersions: string;
exhortMvnPath: string;
exhortNpmPath: string;
exhortGoPath: string;
exhortPython3Path: string;
exhortPip3Path: string;
exhortPythonPath: string;
exhortPipPath: string;

/**
* Initializes a new instance of the Config class with default values from the parent process environment variable data.
Expand All @@ -33,7 +32,6 @@ export class Config
this.triggerFullStackAnalysis = process.env.VSCEXT_TRIGGER_FULL_STACK_ANALYSIS || '';
this.telemetryId = process.env.VSCEXT_TELEMETRY_ID || '';
this.utmSource = process.env.VSCEXT_UTM_SOURCE || '';
this.exhortDevMode = process.env.VSCEXT_EXHORT_DEV_MODE || 'false';
this.exhortSnykToken = process.env.VSCEXT_EXHORT_SNYK_TOKEN || '';
this.exhortOSSIndexUser = process.env.VSCEXT_EXHORT_OSS_INDEX_USER || '';
this.exhortOSSIndexToken = process.env.VSCEXT_EXHORT_OSS_INDEX_TOKEN || '';
Expand Down Expand Up @@ -71,4 +69,4 @@ export class Config
*/
const globalConfig = new Config();

export { globalConfig };
export { Config, globalConfig };
20 changes: 10 additions & 10 deletions src/diagnosticsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
'use strict';

import { Diagnostic } from 'vscode-languageserver';
import { DependencyMap, IDependencyProvider } from './collector';
import { componentAnalysisService, DependencyData } from './componentAnalysis';
import { DependencyMap, IDependencyProvider, getRange } from './collector';
import { executeComponentAnalysis, DependencyData } from './componentAnalysis';
import { Vulnerability } from './vulnerability';
import { getRange } from './collector';
import { connection } from './server';
import * as path from 'path';

/**
* Diagnostics Pipeline specification.
Expand Down Expand Up @@ -101,9 +99,9 @@ async function performDiagnostics(diagnosticFilePath: string, contents: string,
let dependencies = null;
dependencies = await provider.collect(contents)
.catch(error => {
connection.console.warn(`Error: ${error}`);
connection.console.warn(`Component Analysis Error: ${error}`);
connection.sendNotification('caError', {
data: error,
error: error,
uri: diagnosticFilePath,
});
return;
Expand All @@ -114,14 +112,16 @@ async function performDiagnostics(diagnosticFilePath: string, contents: string,

diagnosticsPipeline.clearDiagnostics();

const analysis = componentAnalysisService(path.basename(diagnosticFilePath), contents)
const analysis = executeComponentAnalysis(diagnosticFilePath, contents)
.then(response => {
diagnosticsPipeline.runDiagnostics(response.dependencies);
})
.catch(error => {
const errMsg = `Component Analysis error. ${error}`;
connection.console.warn(errMsg);
connection.sendNotification('caSimpleWarning', errMsg);
connection.console.warn(`Component Analysis Error: ${error}`);
connection.sendNotification('caError', {
error: error,
uri: diagnosticFilePath,
});
return;
});

Expand Down
3 changes: 2 additions & 1 deletion test/codeActionHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import { Range } from 'vscode-languageserver';
import { CodeAction, CodeActionKind, Diagnostic } from 'vscode-languageserver/node';

import * as config from '../src/config';
import { RHDA_DIAGNOSTIC_SOURCE } from '../src/constants';
import { getDiagnosticsCodeActions } from '../src/codeActionHandler';

describe('Code Action Handler test', () => {
describe('Code Action Handler tests', () => {

const mockRange: Range = {
start: {
Expand Down
10 changes: 9 additions & 1 deletion test/collector.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

import { expect } from 'chai';

import { Dependency, DependencyMap, getRange } from '../src/collector';
import { DependencyProvider } from '../src/providers/pom.xml';

describe('Collector util test', () => {
describe('Collector tests', () => {

// Mock manifest dependency collection
const reqDeps: Dependency[] = [
Expand All @@ -22,6 +23,13 @@ describe('Collector util test', () => {
});
});

it('should create empty map', async () => {

const depMap = new DependencyMap([]);

expect(Object.keys(depMap.mapper).length).to.eql(0);
});

it('should get dependency from dependency map', async () => {

const depMap = new DependencyMap(reqDeps);
Expand Down
74 changes: 74 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

import { expect } from 'chai';

import { Config } from '../src/config';

describe('Config tests', () => {

const mockConfig = new Config();

const data = {
redHatDependencyAnalytics: {
exhortSnykToken: 'mockToken',
exhortOSSIndexUser: 'mockUser',
exhortOSSIndexToken: 'mockToken',
matchManifestVersions: false
},
mvn: {
executable: { path: 'mockPath' }
},
npm: {
executable: { path: 'mockPath' }
},
go: {
executable: { path: 'mockPath' }
},
python3: {
executable: { path: 'mockPath' }
},
pip3: {
executable: { path: 'mockPath' }
},
python: {
executable: { path: 'mockPath' }
},
pip: {
executable: { path: 'mockPath' }
},
};

it('should initialize with default values when environment variables are not set', () => {
expect(mockConfig.triggerFullStackAnalysis).to.eq('');
expect(mockConfig.telemetryId).to.eq('');
expect(mockConfig.utmSource).to.eq('');
expect(mockConfig.exhortSnykToken).to.eq('');
expect(mockConfig.exhortOSSIndexUser).to.eq('');
expect(mockConfig.exhortOSSIndexToken).to.eq('');
expect(mockConfig.matchManifestVersions).to.eq('true');
expect(mockConfig.exhortMvnPath).to.eq('mvn');
expect(mockConfig.exhortNpmPath).to.eq('npm');
expect(mockConfig.exhortGoPath).to.eq('go');
expect(mockConfig.exhortPython3Path).to.eq('python3');
expect(mockConfig.exhortPip3Path).to.eq('pip3');
expect(mockConfig.exhortPythonPath).to.eq('python');
expect(mockConfig.exhortPipPath).to.eq('pip');
});

it('should update configuration based on provided data', () => {

mockConfig.updateConfig(data);

expect(mockConfig.exhortSnykToken).to.eq('mockToken');
expect(mockConfig.exhortOSSIndexUser).to.eq('mockUser');
expect(mockConfig.exhortOSSIndexToken).to.eq('mockToken');
expect(mockConfig.matchManifestVersions).to.eq('false');
expect(mockConfig.exhortMvnPath).to.eq('mockPath');
expect(mockConfig.exhortNpmPath).to.eq('mockPath');
expect(mockConfig.exhortGoPath).to.eq('mockPath');
expect(mockConfig.exhortPython3Path).to.eq('mockPath');
expect(mockConfig.exhortPip3Path).to.eq('mockPath');
expect(mockConfig.exhortPythonPath).to.eq('mockPath');
expect(mockConfig.exhortPipPath).to.eq('mockPath');
});
});
13 changes: 7 additions & 6 deletions test/providers/go.mod.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

import { expect } from 'chai';

import { DependencyProvider } from '../../src/providers/go.mod';

describe('Golang Gomudules go.mod parser test', () => {
describe('Golang Gomodules go.mod parser tests', () => {
let dependencyProvider: DependencyProvider;

beforeEach(() => {
Expand All @@ -15,7 +16,7 @@ describe('Golang Gomudules go.mod parser test', () => {
expect(deps).is.eql([]);
});

it('tests require statement in go.mod', async () => {
it('tests require statements in go.mod', async () => {
const deps = await dependencyProvider.collect(`
module github.com/alecthomas/kingpin
Expand Down Expand Up @@ -91,7 +92,7 @@ describe('Golang Gomudules go.mod parser test', () => {
]);
});

it('tests empty lines in go.mod', async () => {
it('tests go.mod with empty lines', async () => {
const deps = await dependencyProvider.collect(`
module github.com/alecthomas/kingpin
Expand Down Expand Up @@ -119,7 +120,7 @@ describe('Golang Gomudules go.mod parser test', () => {
]);
});

it('tests deps with spaces before and after dep name and version', async () => {
it('tests go.mod with dependencies which have spaces before and after package name and version', async () => {
const deps = await dependencyProvider.collect(`
module github.com/alecthomas/kingpin
require (
Expand Down Expand Up @@ -150,7 +151,7 @@ describe('Golang Gomudules go.mod parser test', () => {
]);
});

it('tests deps with alpha, beta and extra for version', async () => {
it('tests go.mod with dependencies which have extra sufixes in version', async () => {
const deps = await dependencyProvider.collect(`
module github.com/alecthomas/kingpin
Expand Down Expand Up @@ -273,7 +274,7 @@ describe('Golang Gomudules go.mod parser test', () => {
]);
});

it('tests multiple module points to same replace module in go.mod', async () => {
it('tests go.mod with multiple packages replaced by the same package in go.mod', async () => {
const deps = await dependencyProvider.collect(`
module github.com/alecthomas/kingpin
go 1.13
Expand Down
9 changes: 5 additions & 4 deletions test/providers/package.json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import { expect } from 'chai';
import * as sinon from 'sinon';

import { DependencyProvider } from '../../src/providers/package.json';

describe('Javascript NPM package.json parser test', () => {
describe('Javascript NPM package.json parser tests', () => {
let dependencyProvider: DependencyProvider;

beforeEach(() => {
Expand All @@ -21,7 +22,7 @@ describe('Javascript NPM package.json parser test', () => {
expect(deps.length).equal(0);
});

it('tests invalid token package.json', async () => {
it('tests invalid token', async () => {
const deps = await dependencyProvider.collect(`
{
<<<<<
Expand Down Expand Up @@ -54,7 +55,7 @@ describe('Javascript NPM package.json parser test', () => {
expect(deps).eql([]);
});

it('tests single dependency ', async () => {
it('tests single dependency', async () => {
const deps = await dependencyProvider.collect(`
{
"hello":{},
Expand Down Expand Up @@ -134,7 +135,7 @@ describe('Javascript NPM package.json parser test', () => {
]);
});

it('tests 3 dependencies with spaces', async () => {
it('tests dependencies with spaces', async () => {
const deps = await dependencyProvider.collect(`
{
"hello":{},
Expand Down
3 changes: 2 additions & 1 deletion test/providers/pom.xml.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

import { expect } from 'chai';

import { DependencyProvider } from '../../src/providers/pom.xml';

describe('Java Maven pom.xml parser test', () => {
describe('Java Maven pom.xml parser tests', () => {
let dependencyProvider: DependencyProvider;

beforeEach(() => {
Expand Down
Loading

0 comments on commit c7fc8c9

Please sign in to comment.