@@ -3,7 +3,7 @@ import * as path from "path";
33
44import * as github from "@actions/github" ;
55import { HTTPError } from "@actions/tool-cache" ;
6- import test , { ExecutionContext } from "ava" ;
6+ import test from "ava" ;
77import * as sinon from "sinon" ;
88
99import * as analyses from "./analyses" ;
@@ -873,122 +873,90 @@ function createMockSarif(id?: string, tool?: string) {
873873 } ;
874874}
875875
876- const uploadPayloadMacro = test . macro ( {
877- exec : async (
878- t : ExecutionContext < unknown > ,
879- options : {
880- analysis : analyses . AnalysisConfig ;
881- body : (
882- t : ExecutionContext < unknown > ,
883- upload : ( ) => Promise < string > ,
884- requestStub : sinon . SinonStub ,
885- mockData : {
886- payload : { sarif : string ; commit_sha : string } ;
887- owner : string ;
888- repo : string ;
889- response : {
890- status : number ;
891- data : { id : string } ;
892- headers : any ;
893- url : string ;
894- } ;
895- } ,
896- ) => void | Promise < void > ;
876+ function uploadPayloadFixtures ( analysis : analyses . AnalysisConfig ) {
877+ const mockData = {
878+ payload : { sarif : "base64data" , commit_sha : "abc123" } ,
879+ owner : "test-owner" ,
880+ repo : "test-repo" ,
881+ response : {
882+ status : 200 ,
883+ data : { id : "uploaded-sarif-id" } ,
884+ headers : { } ,
885+ url : analysis . target ,
897886 } ,
898- ) => {
899- const mockData = {
900- payload : { sarif : "base64data" , commit_sha : "abc123" } ,
901- owner : "test-owner" ,
902- repo : "test-repo" ,
903- response : {
904- status : 200 ,
905- data : { id : "uploaded-sarif-id" } ,
906- headers : { } ,
907- url : options . analysis . target ,
887+ } ;
888+ const client = github . getOctokit ( "123" ) ;
889+ sinon . stub ( api , "getApiClient" ) . value ( ( ) => client ) ;
890+ const requestStub = sinon . stub ( client , "request" ) ;
891+
892+ const upload = async ( ) =>
893+ uploadLib . uploadPayload (
894+ mockData . payload ,
895+ {
896+ owner : mockData . owner ,
897+ repo : mockData . repo ,
908898 } ,
909- } ;
910-
911- const client = github . getOctokit ( "123" ) ;
912- sinon . stub ( api , "getApiClient" ) . value ( ( ) => client ) ;
913- const requestStub = sinon . stub ( client , "request" ) ;
914-
915- const upload = async ( ) =>
916- uploadLib . uploadPayload (
917- mockData . payload ,
918- {
919- owner : mockData . owner ,
920- repo : mockData . repo ,
921- } ,
922- getRunnerLogger ( true ) ,
923- options . analysis ,
924- ) ;
899+ getRunnerLogger ( true ) ,
900+ analysis ,
901+ ) ;
925902
926- await options . body ( t , upload , requestStub , mockData ) ;
927- } ,
928- title : ( providedTitle = "" , options : { analysis : analyses . AnalysisConfig } ) =>
929- `uploadPayload - ${ options . analysis . name } - ${ providedTitle } ` ,
930- } ) ;
903+ return {
904+ upload,
905+ requestStub,
906+ mockData,
907+ } ;
908+ }
931909
932910for ( const analysis of [ CodeScanning , CodeQuality ] ) {
933- test ( "uploads successfully" , uploadPayloadMacro , {
934- analysis,
935- body : async ( t , upload , requestStub , mockData ) => {
936- requestStub
937- . withArgs ( analysis . target , {
938- owner : mockData . owner ,
939- repo : mockData . repo ,
940- data : mockData . payload ,
941- } )
942- . onFirstCall ( )
943- . returns ( Promise . resolve ( mockData . response ) ) ;
944- const result = await upload ( ) ;
945- t . is ( result , mockData . response . data . id ) ;
946- t . true ( requestStub . calledOnce ) ;
947- } ,
911+ test ( `uploadPayload on ${ analysis . name } uploads successfully` , async ( t ) => {
912+ const { upload, requestStub, mockData } = uploadPayloadFixtures ( analysis ) ;
913+ requestStub
914+ . withArgs ( analysis . target , {
915+ owner : mockData . owner ,
916+ repo : mockData . repo ,
917+ data : mockData . payload ,
918+ } )
919+ . onFirstCall ( )
920+ . returns ( Promise . resolve ( mockData . response ) ) ;
921+ const result = await upload ( ) ;
922+ t . is ( result , mockData . response . data . id ) ;
923+ t . true ( requestStub . calledOnce ) ;
948924 } ) ;
949925
950926 for ( const envVar of [
951927 "CODEQL_ACTION_SKIP_SARIF_UPLOAD" ,
952928 "CODEQL_ACTION_TEST_MODE" ,
953929 ] ) {
954- test ( `skips upload when ${ envVar } is set` , uploadPayloadMacro , {
955- analysis,
956- body : async ( t , upload , requestStub , mockData ) =>
957- withTmpDir ( async ( tmpDir ) => {
958- process . env . RUNNER_TEMP = tmpDir ;
959- process . env [ envVar ] = "true" ;
960- const result = await upload ( ) ;
961- t . is ( result , "dummy-sarif-id" ) ;
962- t . false ( requestStub . called ) ;
963-
964- const payloadFile = path . join (
965- tmpDir ,
966- `payload-${ analysis . kind } .json` ,
967- ) ;
968- t . true ( fs . existsSync ( payloadFile ) ) ;
969-
970- const savedPayload = JSON . parse ( fs . readFileSync ( payloadFile , "utf8" ) ) ;
971- t . deepEqual ( savedPayload , mockData . payload ) ;
972- } ) ,
930+ test ( `uploadPayload on ${ analysis . name } skips upload when ${ envVar } is set` , async ( t ) => {
931+ const { upload, requestStub, mockData } = uploadPayloadFixtures ( analysis ) ;
932+ await withTmpDir ( async ( tmpDir ) => {
933+ process . env . RUNNER_TEMP = tmpDir ;
934+ process . env [ envVar ] = "true" ;
935+ const result = await upload ( ) ;
936+ t . is ( result , "dummy-sarif-id" ) ;
937+ t . false ( requestStub . called ) ;
938+
939+ const payloadFile = path . join ( tmpDir , `payload-${ analysis . kind } .json` ) ;
940+ t . true ( fs . existsSync ( payloadFile ) ) ;
941+
942+ const savedPayload = JSON . parse ( fs . readFileSync ( payloadFile , "utf8" ) ) ;
943+ t . deepEqual ( savedPayload , mockData . payload ) ;
944+ } ) ;
973945 } ) ;
974946 }
975947
976- test ( "handles error" , uploadPayloadMacro , {
977- analysis,
978- body : async ( t , upload , requestStub ) => {
979- const wrapApiConfigurationErrorStub = sinon . stub (
980- api ,
981- "wrapApiConfigurationError" ,
982- ) ;
983- const originalError = new HTTPError ( 404 ) ;
984- const wrappedError = new Error ( "Wrapped error message" ) ;
985- requestStub . rejects ( originalError ) ;
986- wrapApiConfigurationErrorStub
987- . withArgs ( originalError )
988- . returns ( wrappedError ) ;
989- await t . throwsAsync ( upload , {
990- is : wrappedError ,
991- } ) ;
992- } ,
948+ test ( `uploadPayload on ${ analysis . name } wraps request errors using wrapApiConfigurationError` , async ( t ) => {
949+ const { upload, requestStub } = uploadPayloadFixtures ( analysis ) ;
950+ const wrapApiConfigurationErrorStub = sinon . stub (
951+ api ,
952+ "wrapApiConfigurationError" ,
953+ ) ;
954+ const originalError = new HTTPError ( 404 ) ;
955+ const wrappedError = new Error ( "Wrapped error message" ) ;
956+ requestStub . rejects ( originalError ) ;
957+ wrapApiConfigurationErrorStub . withArgs ( originalError ) . returns ( wrappedError ) ;
958+ await t . throwsAsync ( upload , {
959+ is : wrappedError ,
960+ } ) ;
993961 } ) ;
994962}
0 commit comments