Skip to content

Commit

Permalink
DT-208 Adds unit test suite to policies
Browse files Browse the repository at this point in the history
  • Loading branch information
francostramana authored Mar 19, 2024
1 parent d79018e commit 077639f
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Continuous Integration

on:
workflow_dispatch:
pull_request:
push:
branches:
Expand Down
32 changes: 32 additions & 0 deletions __tests__/copyleft-policy-check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { CopyleftPolicyCheck } from '../src/policies/copyleft-policy-check';
import { CONCLUSION, PolicyCheck } from '../src/policies/policy-check';
import { ScannerResults } from '../src/services/result.interfaces';
import * as github from '@actions/github';
import { resultsMock } from './results.mock';

describe('CopyleftPolicyCheck', () => {
let scannerResults: ScannerResults;
let policyCheck: CopyleftPolicyCheck;

beforeEach(() => {
jest.clearAllMocks();

jest.spyOn(github, 'getOctokit').mockImplementation();
jest.spyOn(PolicyCheck.prototype, 'run').mockImplementation();
jest.spyOn(PolicyCheck.prototype, 'finish').mockImplementation();

policyCheck = new CopyleftPolicyCheck();
});

it('should pass the policy check when no copyleft components are found', async () => {
scannerResults = JSON.parse(resultsMock[0].content);
await policyCheck.run(scannerResults);
expect(policyCheck.conclusion).toEqual(CONCLUSION.Success);
});

it('should fail the policy check when copyleft components are found', async () => {
scannerResults = JSON.parse(resultsMock[2].content);
await policyCheck.run(scannerResults);
expect(policyCheck.conclusion).toEqual(CONCLUSION.Neutral);
});
});
2 changes: 1 addition & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('action', () => {
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation();
});

it('sets the time output', async () => {
it('SCANOSS Scan Action started', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
Expand Down
21 changes: 18 additions & 3 deletions __tests__/report-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as github from '@actions/github';
import * as core from '@actions/core';

import { generatePRSummary } from '../src/services/report.service';
import { generateJobSummary, generatePRSummary } from '../src/services/report.service';

const tableTest = [
{
Expand Down Expand Up @@ -654,7 +655,7 @@ const tableTest = [
}
];

describe('Test report service', () => {
describe('Test report service: generatePRSummary', () => {
beforeEach(() => {
jest.spyOn(github.context, 'repo', 'get').mockReturnValue({ owner: 'x', repo: 'y' });
github.context.runId = 0;
Expand All @@ -663,8 +664,22 @@ describe('Test report service', () => {
for (const t of tableTest) {
it(`${t.name}`, () => {
const report = generatePRSummary(JSON.parse(t.scannerResults), []);
console.log(report);
expect(report).toEqual(t.output);
});
}
});

describe('Test report service: generateJobSummary', () => {
beforeEach(() => {
jest.spyOn(github.context, 'repo', 'get').mockReturnValue({ owner: 'x', repo: 'y' });
jest.spyOn(core.summary, 'write').mockImplementation();

github.context.runId = 0;
});

for (const t of tableTest) {
it(`${t.name}`, async () => {
await expect(generateJobSummary(JSON.parse(t.scannerResults), [])).resolves.toEqual(undefined);
});
}
});
20 changes: 20 additions & 0 deletions __tests__/sbom.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Sbom } from '../src/utils/sbom.utils';

export const sbomMock: Sbom[] = [
{
components: [] // empty sbom
},
{
components: [
{ purl: 'pkg:github/scanoss/engine' },
{ purl: 'pkg:github/scanoss/engine' },
{ purl: 'pkg:github/scanoss/engine' },
{ purl: 'pkg:pypi/requests' },
{ purl: 'pkg:pypi/crc32c' },
{ purl: 'pkg:pypi/binaryornot' },
{ purl: 'pkg:pypi/pytest' },
{ purl: 'pkg:pypi/pytest-cov' },
{ purl: 'pkg:pypi/beautifulsoup4' }
]
}
];
39 changes: 39 additions & 0 deletions __tests__/undeclared-policy-check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CONCLUSION, PolicyCheck } from '../src/policies/policy-check';
import { ScannerResults } from '../src/services/result.interfaces';
import * as github from '@actions/github';
import { resultsMock } from './results.mock';
import { UndeclaredPolicyCheck } from '../src/policies/undeclared-policy-check';
import * as sbom from '../src/utils/sbom.utils';
import { sbomMock } from './sbom.mock';

describe('UndeclaredPolicyCheck', () => {
let scannerResults: ScannerResults;
let undeclaredPolicyCheck: UndeclaredPolicyCheck;

beforeEach(() => {
jest.clearAllMocks();

jest.spyOn(github, 'getOctokit').mockImplementation();

jest.spyOn(PolicyCheck.prototype, 'run').mockImplementation();
jest.spyOn(PolicyCheck.prototype, 'finish').mockImplementation();

scannerResults = JSON.parse(resultsMock[3].content);

undeclaredPolicyCheck = new UndeclaredPolicyCheck();
});

it('should pass the policy check when undeclared components are not found', async () => {
jest.spyOn(sbom, 'parseSbom').mockImplementation(async _ => Promise.resolve(sbomMock[1]));

Check warning on line 27 in __tests__/undeclared-policy-check.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'_' is defined but never used

Check warning on line 27 in __tests__/undeclared-policy-check.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'_' is defined but never used

await undeclaredPolicyCheck.run(scannerResults);
expect(undeclaredPolicyCheck.conclusion).toEqual(CONCLUSION.Success);
});

it('should fail the policy check when undeclared components are found', async () => {
jest.spyOn(sbom, 'parseSbom').mockImplementation(async _ => Promise.resolve(sbomMock[0]));

Check warning on line 34 in __tests__/undeclared-policy-check.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'_' is defined but never used

Check warning on line 34 in __tests__/undeclared-policy-check.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'_' is defined but never used

await undeclaredPolicyCheck.run(scannerResults);
expect(undeclaredPolicyCheck.conclusion).toEqual(CONCLUSION.Neutral);
});
});
2 changes: 1 addition & 1 deletion src/policies/policy-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export abstract class PolicyCheck {
return await this.finish(summary, text);
}

protected async finish(summary: string, text?: string): Promise<void> {
async finish(summary: string, text?: string): Promise<void> {
core.debug(`Finish policy check: ${this.checkName}. (conclusion=${this._conclusion})`);
this._status = STATUS.FINISHED;

Expand Down

0 comments on commit 077639f

Please sign in to comment.