@@ -2,9 +2,17 @@ import test, { ExecutionContext } from "ava";
22import * as yaml from "js-yaml" ;
33import * as sinon from "sinon" ;
44
5- import { getCodeQLForTesting } from "./codeql" ;
6- import { setupTests } from "./testing-utils" ;
5+ import * as actionsUtil from "./actions-util" ;
6+ import { createStubCodeQL , getCodeQLForTesting } from "./codeql" ;
7+ import { EnvVar } from "./environment" ;
78import {
9+ checkExpectedLogMessages ,
10+ getRecordingLogger ,
11+ LoggedMessage ,
12+ setupTests ,
13+ } from "./testing-utils" ;
14+ import {
15+ checkWorkflow ,
816 CodedError ,
917 formatWorkflowCause ,
1018 formatWorkflowErrors ,
@@ -13,6 +21,7 @@ import {
1321 Workflow ,
1422 WorkflowErrors ,
1523} from "./workflow" ;
24+ import * as workflow from "./workflow" ;
1625
1726function errorCodes (
1827 actual : CodedError [ ] ,
@@ -870,3 +879,78 @@ test("getCategoryInputOrThrow throws error for workflow with multiple calls to a
870879 } ,
871880 ) ;
872881} ) ;
882+
883+ test ( "checkWorkflow - validates workflow if `SKIP_WORKFLOW_VALIDATION` is not set" , async ( t ) => {
884+ const messages : LoggedMessage [ ] = [ ] ;
885+ const codeql = createStubCodeQL ( { } ) ;
886+
887+ sinon . stub ( actionsUtil , "isDynamicWorkflow" ) . returns ( false ) ;
888+ const validateWorkflow = sinon . stub ( workflow . internal , "validateWorkflow" ) ;
889+ validateWorkflow . resolves ( undefined ) ;
890+
891+ await checkWorkflow ( getRecordingLogger ( messages ) , codeql ) ;
892+
893+ t . assert (
894+ validateWorkflow . calledOnce ,
895+ "`checkWorkflow` unexpectedly did not call `validateWorkflow`" ,
896+ ) ;
897+ checkExpectedLogMessages ( t , messages , [
898+ "Detected no issues with the code scanning workflow." ,
899+ ] ) ;
900+ } ) ;
901+
902+ test ( "checkWorkflow - logs problems with workflow validation" , async ( t ) => {
903+ const messages : LoggedMessage [ ] = [ ] ;
904+ const codeql = createStubCodeQL ( { } ) ;
905+
906+ sinon . stub ( actionsUtil , "isDynamicWorkflow" ) . returns ( false ) ;
907+ const validateWorkflow = sinon . stub ( workflow . internal , "validateWorkflow" ) ;
908+ validateWorkflow . resolves ( "problem" ) ;
909+
910+ await checkWorkflow ( getRecordingLogger ( messages ) , codeql ) ;
911+
912+ t . assert (
913+ validateWorkflow . calledOnce ,
914+ "`checkWorkflow` unexpectedly did not call `validateWorkflow`" ,
915+ ) ;
916+ checkExpectedLogMessages ( t , messages , [
917+ "Unable to validate code scanning workflow: problem" ,
918+ ] ) ;
919+ } ) ;
920+
921+ test ( "checkWorkflow - skips validation if `SKIP_WORKFLOW_VALIDATION` is `true`" , async ( t ) => {
922+ process . env [ EnvVar . SKIP_WORKFLOW_VALIDATION ] = "true" ;
923+
924+ const messages : LoggedMessage [ ] = [ ] ;
925+ const codeql = createStubCodeQL ( { } ) ;
926+
927+ sinon . stub ( actionsUtil , "isDynamicWorkflow" ) . returns ( false ) ;
928+ const validateWorkflow = sinon . stub ( workflow . internal , "validateWorkflow" ) ;
929+
930+ await checkWorkflow ( getRecordingLogger ( messages ) , codeql ) ;
931+
932+ t . assert (
933+ validateWorkflow . notCalled ,
934+ "`checkWorkflow` called `validateWorkflow` unexpectedly" ,
935+ ) ;
936+ t . is ( messages . length , 0 ) ;
937+ } ) ;
938+
939+ test ( "checkWorkflow - skips validation for `dynamic` workflows" , async ( t ) => {
940+ const messages : LoggedMessage [ ] = [ ] ;
941+ const codeql = createStubCodeQL ( { } ) ;
942+
943+ const isDynamicWorkflow = sinon
944+ . stub ( actionsUtil , "isDynamicWorkflow" )
945+ . returns ( true ) ;
946+ const validateWorkflow = sinon . stub ( workflow . internal , "validateWorkflow" ) ;
947+
948+ await checkWorkflow ( getRecordingLogger ( messages ) , codeql ) ;
949+
950+ t . assert ( isDynamicWorkflow . calledOnce ) ;
951+ t . assert (
952+ validateWorkflow . notCalled ,
953+ "`checkWorkflow` called `validateWorkflow` unexpectedly" ,
954+ ) ;
955+ t . is ( messages . length , 0 ) ;
956+ } ) ;
0 commit comments