Skip to content

Commit

Permalink
Merge branch 'main' into other/increase-coverage-unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Korjen97 authored Jan 22, 2025
2 parents 6151e99 + 1827268 commit 898e26e
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 50 deletions.
140 changes: 140 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@
"mocha": "10.3.0",
"mock-require": "^3.0.3",
"nyc": "^17.1.0",
"sinon": "^19.0.2",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"vsce": "^2.15.0",
Expand Down
6 changes: 2 additions & 4 deletions src/asca/ascaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function clearAscaProblems() {
diagnosticCollection.clear();
}

function updateProblems(scanAscaResult: CxAsca, uri: vscode.Uri) {
export function updateProblems(scanAscaResult: CxAsca, uri: vscode.Uri) {
diagnosticCollection.delete(uri);
const diagnostics: vscode.Diagnostic[] = [];

Expand All @@ -69,12 +69,10 @@ function updateProblems(scanAscaResult: CxAsca, uri: vscode.Uri) {

const problemText = res.problematicLine;
const startIndex = problemText.length - problemText.trimStart().length;
const endIndex = startIndex + problemText.length;


const range = new vscode.Range(
new vscode.Position(res.line - 1, startIndex),
new vscode.Position(res.line - 1, endIndex)
new vscode.Position(res.line - 1, problemText.length)
);
const diagnostic: vscode.Diagnostic = new vscode.Diagnostic(
range,
Expand Down
60 changes: 60 additions & 0 deletions src/unit/ascaService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import './mocks/vscode-mock';
import './mocks/cxWrapper-mock';
import { mockDiagnosticCollection } from './mocks/vscode-mock';
import { updateProblems } from '../asca/ascaService';
import type CxAsca from "@checkmarxdev/ast-cli-javascript-wrapper/dist/main/asca/CxAsca";
import type { Uri } from 'vscode';

describe('ascaService', () => {
let mockUri: Uri;
let sandbox: sinon.SinonSandbox;

beforeEach(() => {
sandbox = sinon.createSandbox();
mockUri = {
fsPath: '/test/path',
scheme: 'file'
} as Uri;
mockDiagnosticCollection.set.reset();

Check failure on line 20 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-latest)

Property 'reset' does not exist on type '() => void'.

Check failure on line 20 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-latest)

Property 'reset' does not exist on type '() => void'.

Check failure on line 20 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / end-to-end-tests (ubuntu-latest)

Property 'reset' does not exist on type '() => void'.
mockDiagnosticCollection.delete.reset();

Check failure on line 21 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-latest)

Property 'reset' does not exist on type '() => void'.

Check failure on line 21 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-latest)

Property 'reset' does not exist on type '() => void'.

Check failure on line 21 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / end-to-end-tests (ubuntu-latest)

Property 'reset' does not exist on type '() => void'.
});

afterEach(() => {
sandbox.restore();
});

describe('updateProblems', () => {
it('should calculate correct range based on leading whitespace', () => {
const mockScanResult: CxAsca = {
scanDetails: [
{
line: 1,
problematicLine: ' const unsafeCode = eval("2+2");', // 2 spaces
ruleName: 'Test Rule',
remediationAdvise: 'Test Advice',
severity: 'LOW'
},
{
line: 2,
problematicLine: ' console.log(secret);', // 4 spaces
ruleName: 'Test Rule 2',
remediationAdvise: 'Test Advice 2',
severity: 'LOW'
}
]
} as CxAsca;

updateProblems(mockScanResult, mockUri);

const diagnostics = mockDiagnosticCollection.set.getCall(0).args[1];

Check failure on line 51 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-latest)

Property 'getCall' does not exist on type '() => void'.

Check failure on line 51 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-latest)

Property 'getCall' does not exist on type '() => void'.

Check failure on line 51 in src/unit/ascaService.test.ts

View workflow job for this annotation

GitHub Actions / end-to-end-tests (ubuntu-latest)

Property 'getCall' does not exist on type '() => void'.

expect(diagnostics[0].range.start.character).to.equal(2);
expect(diagnostics[0].range.end.character).to.equal(33);

expect(diagnostics[1].range.start.character).to.equal(4);
expect(diagnostics[1].range.end.character).to.equal(24);
});
});
});
5 changes: 5 additions & 0 deletions src/unit/getScan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { expect } from "chai";
import "./mocks/vscode-mock";
import "./mocks/cxWrapper-mock";
import { cx } from "../cx";
import { resetMocks } from "./mocks/vscode-mock";

Check failure on line 5 in src/unit/getScan.test.ts

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-latest)

Module '"./mocks/vscode-mock"' has no exported member 'resetMocks'.

Check failure on line 5 in src/unit/getScan.test.ts

View workflow job for this annotation

GitHub Actions / end-to-end-tests (ubuntu-latest)

Module '"./mocks/vscode-mock"' has no exported member 'resetMocks'.

describe("Cx - getScan", () => {
beforeEach(() => {
resetMocks();
});

it("should return scan object when scanId is provided", async () => {
const scanId = "1";
const result = await cx.getScan(scanId);
Expand Down
Loading

0 comments on commit 898e26e

Please sign in to comment.