1
- import { logBridgeExitCodes , run } from '../../src/main'
1
+ import { getBridgeExitCode , logBridgeExitCodes , run } from '../../src/main'
2
2
import * as inputs from '../../src/blackduck-security-action/inputs'
3
3
import { Bridge } from '../../src/blackduck-security-action/bridge-cli'
4
4
import { DownloadFileResponse } from '../../src/blackduck-security-action/download-utility'
@@ -49,7 +49,7 @@ describe('Black Duck Security Action: Handling isBridgeExecuted and Exit Code In
49
49
}
50
50
}
51
51
52
- const setupMocks = ( exitCode : number ) => {
52
+ const setupMocks = ( ) => {
53
53
jest . spyOn ( Bridge . prototype , 'getBridgeVersionFromLatestURL' ) . mockResolvedValueOnce ( '0.1.0' )
54
54
const downloadFileResp : DownloadFileResponse = {
55
55
filePath : 'C://user/temp/download/' ,
@@ -58,7 +58,6 @@ describe('Black Duck Security Action: Handling isBridgeExecuted and Exit Code In
58
58
jest . spyOn ( downloadUtility , 'getRemoteFile' ) . mockResolvedValueOnce ( downloadFileResp )
59
59
jest . spyOn ( downloadUtility , 'extractZipped' ) . mockResolvedValueOnce ( true )
60
60
jest . spyOn ( configVariables , 'getGitHubWorkspaceDir' ) . mockReturnValueOnce ( '/home/bridge' )
61
- jest . spyOn ( Bridge . prototype , 'executeBridgeCommand' ) . mockResolvedValueOnce ( exitCode )
62
61
const uploadResponse : UploadArtifactResponse = { size : 0 , id : 123 }
63
62
jest . spyOn ( diagnostics , 'uploadDiagnostics' ) . mockResolvedValueOnce ( uploadResponse )
64
63
}
@@ -69,7 +68,8 @@ describe('Black Duck Security Action: Handling isBridgeExecuted and Exit Code In
69
68
70
69
it ( 'handles successful execution with exitCode 0' , async ( ) => {
71
70
setupBlackDuckInputs ( )
72
- setupMocks ( 0 )
71
+ setupMocks ( )
72
+ jest . spyOn ( Bridge . prototype , 'executeBridgeCommand' ) . mockResolvedValueOnce ( 0 )
73
73
const response = await run ( )
74
74
75
75
expect ( response ) . toBe ( 0 )
@@ -80,25 +80,30 @@ describe('Black Duck Security Action: Handling isBridgeExecuted and Exit Code In
80
80
81
81
it ( 'handles issues detected but marked as success with exitCode 8' , async ( ) => {
82
82
setupBlackDuckInputs ( { MARK_BUILD_STATUS : 'success' } )
83
- setupMocks ( 8 )
83
+ setupMocks ( )
84
+ jest . spyOn ( Bridge . prototype , 'executeBridgeCommand' ) . mockRejectedValueOnce ( new Error ( 'Bridge CLI execution failed with exit code 8' ) )
84
85
jest . spyOn ( utility , 'checkJobResult' ) . mockReturnValue ( 'success' )
85
86
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' )
87
+ try {
88
+ await run ( )
89
+ } catch ( error : any ) {
90
+ expect ( error . message ) . toContain ( 'Exit Code: 8' )
91
+ expect ( core . info ) . toHaveBeenCalledWith ( 'Marking the build success as configured in the task.' )
92
+ expect ( core . setOutput ) . toHaveBeenCalledWith ( 'status' , 8 )
93
+ expect ( core . debug ) . toHaveBeenCalledWith ( 'Bridge CLI execution completed: true' )
94
+ }
92
95
} )
93
96
94
97
it ( 'handles failure case with exitCode 2' , async ( ) => {
95
98
setupBlackDuckInputs ( )
96
- setupMocks ( 2 )
99
+ setupMocks ( )
100
+ jest . spyOn ( Bridge . prototype , 'executeBridgeCommand' ) . mockRejectedValueOnce ( new Error ( 'Exit Code: 2 Error from adapter end' ) )
97
101
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
+ try {
103
+ await run ( )
104
+ } catch ( error : any ) {
105
+ expect ( error . message ) . toContain ( 'Exit Code: 2 Error from adapter end' )
106
+ }
102
107
} )
103
108
104
109
it ( 'uploads SARIF report for exitCode 8' , async ( ) => {
@@ -107,14 +112,22 @@ describe('Black Duck Security Action: Handling isBridgeExecuted and Exit Code In
107
112
BLACKDUCKSCA_REPORTS_SARIF_FILE_PATH : '/' ,
108
113
MARK_BUILD_STATUS : 'success'
109
114
} )
110
- setupMocks ( 8 )
115
+ setupMocks ( )
116
+ jest . spyOn ( Bridge . prototype , 'executeBridgeCommand' ) . mockRejectedValueOnce ( new Error ( 'Bridge CLI execution failed with exit code 8' ) )
111
117
jest . spyOn ( utility , 'checkJobResult' ) . mockReturnValue ( 'success' )
112
118
jest . spyOn ( utility , 'isPullRequestEvent' ) . mockReturnValue ( false )
113
119
const uploadResponse : UploadArtifactResponse = { size : 0 , id : 123 }
114
120
jest . spyOn ( diagnostics , 'uploadSarifReportAsArtifact' ) . mockResolvedValueOnce ( uploadResponse )
115
121
116
- await run ( )
117
- expect ( diagnostics . uploadSarifReportAsArtifact ) . toHaveBeenCalledWith ( 'Blackduck SCA SARIF Generator' , '/' , 'blackduck_sarif_report' )
122
+ const error = new Error ( 'Error: The process failed with exit code 8' )
123
+ expect ( getBridgeExitCode ( error ) ) . toBe ( true )
124
+
125
+ try {
126
+ await run ( )
127
+ } catch ( error : any ) {
128
+ expect ( error . message ) . toContain ( 'Exit Code: 8' )
129
+ expect ( diagnostics . uploadSarifReportAsArtifact ) . toHaveBeenCalledWith ( 'blackduck-sca-sarif-generator' , '/' , 'blackduck-sca-sarif-artifact' )
130
+ }
118
131
} )
119
132
} )
120
133
0 commit comments