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 Apr 10, 2024
1 parent cd74e46 commit 16c5e25
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/dependencyAnalysis/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Range } from 'vscode-languageserver';

import { IPositionedString, IPositionedContext, IPosition } from '../collector'
import { IPositionedString, IPositionedContext, IPosition } from '../positionTypes';
import { isDefined } from '../utils';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/dependencyAnalysis/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Diagnostic } from 'vscode-languageserver';

import { DependencyMap, IDependencyProvider, getRange } from '../dependencyAnalysis/collector';
import { IPositionedContext } from '../collector';
import { IPositionedContext } from '../positionTypes';
import { executeComponentAnalysis, DependencyData } from './analysis';
import { Vulnerability } from '../vulnerability';
import { connection } from '../server';
Expand Down
4 changes: 3 additions & 1 deletion src/imageAnalysis/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Range } from 'vscode-languageserver';

import { IPositionedString, IPosition } from '../collector'
import { IPositionedString, IPosition } from '../positionTypes';

/**
* Represents a context with a string value and its associated range.
Expand Down Expand Up @@ -67,13 +67,15 @@ export class ImageMap {
*/
public get(key: string): IImage[] {
const images: IImage[] = [];
/* istanbul ignore else */
if (this.mapper.has(key)) {
images.push(...this.mapper.get(key));
}

// Check if the key includes ":latest"
if (key.includes(':latest')) {
const keyWithoutLatest = key.replace(':latest', '');
/* istanbul ignore else */
if (this.mapper.has(keyWithoutLatest)) {
images.push(...this.mapper.get(keyWithoutLatest));
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/vulnerability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Highest severity: ${artifactData.highestVulnerabilitySeverity}`;
private generateRecommendation(artifactData: DependencyData | ImageData): string {
return `${artifactData.sourceId} vulnerability info:
Known security vulnerabilities: ${artifactData.issuesCount}
Recommendation: ${artifactData.recommendationRef || 'No RedHat recommendations'}`;
Recommendation: ${artifactData.recommendationRef || 'No Red Hat recommendations'}`;
}


Expand Down
41 changes: 41 additions & 0 deletions test/codeActionHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,45 @@ describe('Code Action Handler tests', () => {
}
);
});

it('should return a redirect to recommended version code action', async () => {

let globalConfig = {
trackRecommendationAcceptanceCommand: 'mockTrackRecommendationAcceptanceCommand'
};
sinon.stub(config, 'globalConfig').value(globalConfig);

const codeAction: CodeAction = codeActionHandler.generateRedirectToRecommendedVersionAction( 'mockTitle', 'mockImage@mockTag', mockDiagnostic1[0], 'mock/path/Dockerfile');
expect(codeAction).to.deep.equal(
{
"command": {
"command": 'mockTrackRecommendationAcceptanceCommand',
"title": "Track recommendation acceptance",
"arguments": [
"mockImage@mockTag",
"Dockerfile"
]
},
"diagnostics": [
{
"message": "another mock message",
"range": {
"end": {
"character": 654,
"line": 654
},
"start": {
"character": 321,
"line": 321
}
},
"severity": 3,
"source": 'mockSource'
}
],
"kind": "quickfix",
"title": "mockTitle"
}
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { expect } from 'chai';

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

describe('Collector tests', () => {
describe('Dependency Analysis Collector tests', () => {

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

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

const depMap = new DependencyMap([]);

Expand Down
75 changes: 75 additions & 0 deletions test/imageAnalysis/collector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

import { expect } from 'chai';

import { Image, ImageMap, getRange } from '../../src/imageAnalysis/collector';

describe('Image Analysis Collector tests', () => {

// Mock dockerfile image collection
let reqImages: Image[] = [
new Image ({ value: 'alpine', position: { line: 1, column: 0 } }, 'FROM --platform=linux/amd64 alpine as a'),
new Image ({ value: 'alpine:latest', position: { line: 2, column: 0 } }, 'FROM --platform=linux/amd64 alpine:latest AS a'),
new Image ({ value: 'alpine@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b', position: { line: 3, column: 0 } }, 'FROM --platform=linux/amd64 alpine@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b As a'),
new Image ({ value: 'alpine', position: { line: 4, column: 0 } }, 'FROM --platform=linux/amd64 alpine as a'),
];
reqImages.forEach(image => image.platform = 'linux/amd64');

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

const imageMap = new ImageMap(reqImages);

expect(Object.fromEntries(imageMap.mapper)).to.eql({
'alpine': [reqImages[0], reqImages[3]],
'alpine:latest': [reqImages[1]],
'alpine@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b': [reqImages[2]]
});
});

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

const imageMap = new ImageMap([]);

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

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

const imageMap = new ImageMap(reqImages);

// The image names from the response always iclude 'latest' tag if no tag nor digest is provided
expect(JSON.stringify(imageMap.get('alpine:latest'))).to.eq(JSON.stringify([reqImages[1], reqImages[0], reqImages[3]]));
expect(JSON.stringify(imageMap.get('alpine@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b'))).to.eq(JSON.stringify([reqImages[2]]));
});

it('should return image range', async () => {

expect(getRange(reqImages[0])).to.eql(
{
start: { line: 0, character: 0 },
end: { line: 0, character: 39 }
}
);

expect(getRange(reqImages[1])).to.eql(
{
start: { line: 1, character: 0 },
end: { line: 1, character: 46 }
}
);

expect(getRange(reqImages[2])).to.eql(
{
start: { line: 2, character: 0 },
end: { line: 2, character: 111 }
}
);

expect(getRange(reqImages[3])).to.eql(
{
start: { line: 3, character: 0 },
end: { line: 3, character: 39 }
}
);
});
})
33 changes: 33 additions & 0 deletions test/positionTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

import { expect } from 'chai';

import { IPosition, IPositionedString, IPositionedContext } from '../src/positionTypes';

describe('Position Types tests', () => {
describe('IPosition Interface', () => {
it('should have properties line and column', () => {
const position: IPosition = { line: 1, column: 5 };
expect(position).to.have.property('line');
expect(position).to.have.property('column');
});
});

describe('IPositionedString Interface', () => {
it('should have properties value and position', () => {
const positionedString: IPositionedString = { value: 'test', position: { line: 1, column: 5 } };
expect(positionedString).to.have.property('value');
expect(positionedString).to.have.property('position');
});
});

describe('IPositionedContext Interface', () => {
it('should have properties value and range', () => {
// Import Range from 'vscode-languageserver' to mock the range object
const rangeMock = { start: { line: 1, character: 0 }, end: { line: 1, character: 5 } };
const positionedContext: IPositionedContext = { value: 'test', range: rangeMock };
expect(positionedContext).to.have.property('value');
expect(positionedContext).to.have.property('range');
});
});
});
Loading

0 comments on commit 16c5e25

Please sign in to comment.