Skip to content

Commit 950a055

Browse files
feat: [SIGINT-2798] Implement Unit Tests for BridgeExecuted and Exit Code Handling Info (#47)
Added unit test cases for handling various exit codes and ensuring proper info messages are logged. Refs: [SIGINT-2798], [INTEGRATE-169]
1 parent 3bec576 commit 950a055

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

test/unit/blackduck-security-action/utility.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {cleanUrl, isBoolean, isPullRequestEvent} from '../../../src/blackduck-security-action/utility'
1+
import {checkJobResult, cleanUrl, isBoolean, isPullRequestEvent} from '../../../src/blackduck-security-action/utility'
22
import * as constants from '../../../src/application-constants'
33
test('cleanUrl() trailing slash', () => {
44
const validUrl = 'https://my-domain.com'
@@ -69,3 +69,22 @@ describe('isPullRequestEvent', () => {
6969
expect(result).toEqual(false)
7070
})
7171
})
72+
73+
describe('checkJobResult', () => {
74+
it('should return the build status if it is valid', () => {
75+
const buildStatus = 'success'
76+
const result = checkJobResult(buildStatus)
77+
expect(result).toBe(buildStatus)
78+
})
79+
80+
it('should return undefined if the build status is invalid', () => {
81+
const buildStatus = 'unstable'
82+
const result = checkJobResult(buildStatus)
83+
expect(result).toBeUndefined()
84+
})
85+
86+
it('should return undefined if the build status is not provided', () => {
87+
const result = checkJobResult()
88+
expect(result).toBeUndefined()
89+
})
90+
})

test/unit/main.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import * as utility from '../../src/blackduck-security-action/utility'
1111
import {GitHubClientServiceFactory} from '../../src/blackduck-security-action/factory/github-client-service-factory'
1212
import {GithubClientServiceCloud} from '../../src/blackduck-security-action/service/impl/cloud/github-client-service-cloud'
1313
import fs from 'fs'
14+
import * as core from '@actions/core'
1415

16+
jest.mock('@actions/core')
1517
jest.mock('@actions/io', () => ({
1618
rmRF: jest.fn()
1719
}))
@@ -32,6 +34,90 @@ afterEach(() => {
3234
jest.restoreAllMocks()
3335
})
3436

37+
describe('Black Duck Security Action: Handling isBridgeExecuted and Exit Code Information Messages', () => {
38+
const setupBlackDuckInputs = (extraInputs: Record<string, any> = {}) => {
39+
Object.defineProperty(inputs, 'BLACKDUCKSCA_URL', {value: 'BLACKDUCKSCA_URL'})
40+
Object.defineProperty(inputs, 'BLACKDUCKSCA_TOKEN', {value: 'BLACKDUCKSCA_TOKEN'})
41+
Object.defineProperty(inputs, 'DETECT_INSTALL_DIRECTORY', {value: 'DETECT_INSTALL_DIRECTORY'})
42+
Object.defineProperty(inputs, 'DETECT_SCAN_FULL', {value: 'TRUE'})
43+
Object.defineProperty(inputs, 'BLACKDUCKSCA_SCAN_FAILURE_SEVERITIES', {value: 'ALL'})
44+
Object.defineProperty(inputs, 'BLACKDUCKSCA_FIXPR_ENABLED', {value: 'false'})
45+
Object.defineProperty(inputs, 'BLACKDUCKSCA_PRCOMMENT_ENABLED', {value: true})
46+
Object.defineProperty(inputs, 'RETURN_STATUS', {value: true})
47+
for (const [key, value] of Object.entries(extraInputs)) {
48+
Object.defineProperty(inputs, key, {value, writable: true})
49+
}
50+
}
51+
52+
const setupMocks = (exitCode: number) => {
53+
jest.spyOn(Bridge.prototype, 'getBridgeVersionFromLatestURL').mockResolvedValueOnce('0.1.0')
54+
const downloadFileResp: DownloadFileResponse = {
55+
filePath: 'C://user/temp/download/',
56+
fileName: 'C://user/temp/download/bridge-win.zip'
57+
}
58+
jest.spyOn(downloadUtility, 'getRemoteFile').mockResolvedValueOnce(downloadFileResp)
59+
jest.spyOn(downloadUtility, 'extractZipped').mockResolvedValueOnce(true)
60+
jest.spyOn(configVariables, 'getGitHubWorkspaceDir').mockReturnValueOnce('/home/bridge')
61+
jest.spyOn(Bridge.prototype, 'executeBridgeCommand').mockResolvedValueOnce(exitCode)
62+
const uploadResponse: UploadArtifactResponse = {size: 0, id: 123}
63+
jest.spyOn(diagnostics, 'uploadDiagnostics').mockResolvedValueOnce(uploadResponse)
64+
}
65+
66+
afterEach(() => {
67+
Object.defineProperty(inputs, 'BLACKDUCKSCA_URL', {value: null})
68+
})
69+
70+
it('handles successful execution with exitCode 0', async () => {
71+
setupBlackDuckInputs()
72+
setupMocks(0)
73+
const response = await run()
74+
75+
expect(response).toBe(0)
76+
expect(core.info).toHaveBeenCalledWith('Black Duck Security Action workflow execution completed successfully.')
77+
expect(core.setOutput).toHaveBeenCalledWith('status', 0)
78+
expect(core.debug).toHaveBeenCalledWith('Bridge CLI execution completed: true')
79+
})
80+
81+
it('handles issues detected but marked as success with exitCode 8', async () => {
82+
setupBlackDuckInputs({MARK_BUILD_STATUS: 'success'})
83+
setupMocks(8)
84+
jest.spyOn(utility, 'checkJobResult').mockReturnValue('success')
85+
86+
const response = await run()
87+
88+
expect(response).toBe(8)
89+
expect(core.info).toHaveBeenCalledWith('Marking the build success as configured in the task.')
90+
expect(core.setOutput).toHaveBeenCalledWith('status', 8)
91+
expect(core.debug).toHaveBeenCalledWith('Bridge CLI execution completed: true')
92+
})
93+
94+
it('handles failure case with exitCode 2', async () => {
95+
setupBlackDuckInputs()
96+
setupMocks(2)
97+
98+
const response = await run()
99+
expect(response).toBe(2)
100+
expect(core.setOutput).toHaveBeenCalledWith('status', 2)
101+
expect(core.debug).toHaveBeenCalledWith('Bridge CLI execution completed: false')
102+
})
103+
104+
it('uploads SARIF report for exitCode 8', async () => {
105+
setupBlackDuckInputs({
106+
BLACKDUCKSCA_REPORTS_SARIF_CREATE: 'true',
107+
BLACKDUCKSCA_REPORTS_SARIF_FILE_PATH: '/',
108+
MARK_BUILD_STATUS: 'success'
109+
})
110+
setupMocks(8)
111+
jest.spyOn(utility, 'checkJobResult').mockReturnValue('success')
112+
jest.spyOn(utility, 'isPullRequestEvent').mockReturnValue(false)
113+
const uploadResponse: UploadArtifactResponse = {size: 0, id: 123}
114+
jest.spyOn(diagnostics, 'uploadSarifReportAsArtifact').mockResolvedValueOnce(uploadResponse)
115+
116+
await run()
117+
expect(diagnostics.uploadSarifReportAsArtifact).toHaveBeenCalledWith('Blackduck SCA SARIF Generator', '/', 'blackduck_sarif_report')
118+
})
119+
})
120+
35121
test('Not supported flow error - run', async () => {
36122
Object.defineProperty(inputs, 'POLARIS_SERVER_URL', {value: null})
37123
Object.defineProperty(inputs, 'BLACKDUCKSCA_URL', {value: null})

0 commit comments

Comments
 (0)