@@ -4,10 +4,12 @@ import * as sinon from "sinon";
44
55import * as actionsUtil from "./actions-util" ;
66import { createStubCodeQL , getCodeQLForTesting } from "./codeql" ;
7- import { EnvVar } from "./environment" ;
7+ import { ActionsEnvVars , EnvVar } from "./environment" ;
88import {
99 checkExpectedLogMessages ,
10+ createTestConfig ,
1011 getRecordingLogger ,
12+ getTestEnv ,
1113 LoggedMessage ,
1214 setupTests ,
1315} from "./testing-utils" ;
@@ -1002,3 +1004,124 @@ test.serial(
10021004 t . is ( messages . length , 0 ) ;
10031005 } ,
10041006) ;
1007+
1008+ test ( "getRepositoryRootOrThrow - gets root from config" , async ( t ) => {
1009+ const expectedRoot = "/path/to/root" ;
1010+ const repositoryRoot = workflow . getRepositoryRootOrThrow (
1011+ { } ,
1012+ "testJob" ,
1013+ { } ,
1014+ createTestConfig ( { repositoryRoot : expectedRoot } ) ,
1015+ getTestEnv ( ) ,
1016+ ) ;
1017+
1018+ t . is ( repositoryRoot , expectedRoot ) ;
1019+ } ) ;
1020+
1021+ test ( "getRepositoryRootOrThrow - gets root from workflow" , async ( t ) => {
1022+ const expectedRoot = "/path/to/root" ;
1023+ const repositoryRoot = workflow . getRepositoryRootOrThrow (
1024+ {
1025+ jobs : {
1026+ testJob : {
1027+ steps : [
1028+ {
1029+ uses : "github/codeql-action/analyze" ,
1030+ with : { checkout_path : expectedRoot } ,
1031+ } ,
1032+ ] ,
1033+ } ,
1034+ } ,
1035+ } ,
1036+ "testJob" ,
1037+ { } ,
1038+ createTestConfig ( { } ) ,
1039+ getTestEnv ( ) ,
1040+ ) ;
1041+
1042+ t . is ( repositoryRoot , expectedRoot ) ;
1043+ } ) ;
1044+
1045+ test ( "getRepositoryRootOrThrow - gets root from environment" , async ( t ) => {
1046+ const expectedRoot = "/path/to/root" ;
1047+ const repositoryRoot = workflow . getRepositoryRootOrThrow (
1048+ {
1049+ jobs : { testJob : { steps : [ { uses : "github/codeql-action/analyze" } ] } } ,
1050+ } ,
1051+ "testJob" ,
1052+ { } ,
1053+ createTestConfig ( { } ) ,
1054+ getTestEnv ( { [ ActionsEnvVars . GITHUB_WORKSPACE ] : expectedRoot } ) ,
1055+ ) ;
1056+
1057+ t . is ( repositoryRoot , expectedRoot ) ;
1058+ } ) ;
1059+
1060+ test ( "getRepositoryRootOrThrow - throws if there's no matching job" , async ( t ) => {
1061+ t . throws (
1062+ ( ) =>
1063+ workflow . getRepositoryRootOrThrow (
1064+ {
1065+ jobs : {
1066+ otherJob : {
1067+ steps : [
1068+ {
1069+ uses : "github/codeql-action/analyze" ,
1070+ with : { checkout_path : "/some/path" } ,
1071+ } ,
1072+ ] ,
1073+ } ,
1074+ } ,
1075+ } ,
1076+ "testJob" ,
1077+ { } ,
1078+ createTestConfig ( { } ) ,
1079+ getTestEnv ( ) ,
1080+ ) ,
1081+ { message : / s i n c e t h e w o r k f l o w h a s n o j o b n a m e d t e s t J o b ./ } ,
1082+ ) ;
1083+ } ) ;
1084+
1085+ test ( "getRepositoryRootOrThrow - throws if there's no analyze step" , async ( t ) => {
1086+ t . throws (
1087+ ( ) =>
1088+ workflow . getRepositoryRootOrThrow (
1089+ {
1090+ jobs : {
1091+ testJob : { steps : [ ] } ,
1092+ } ,
1093+ } ,
1094+ "testJob" ,
1095+ { } ,
1096+ createTestConfig ( { } ) ,
1097+ getTestEnv ( ) ,
1098+ ) ,
1099+ { message : / s i n c e t h e t e s t J o b j o b d o e s n o t c a l l / } ,
1100+ ) ;
1101+ } ) ;
1102+
1103+ test ( "getRepositoryRootOrThrow - throws if the env var is not set" , async ( t ) => {
1104+ t . throws (
1105+ ( ) =>
1106+ workflow . getRepositoryRootOrThrow (
1107+ {
1108+ jobs : {
1109+ testJob : {
1110+ steps : [
1111+ {
1112+ uses : "github/codeql-action/analyze" ,
1113+ } ,
1114+ ] ,
1115+ } ,
1116+ } ,
1117+ } ,
1118+ "testJob" ,
1119+ { } ,
1120+ createTestConfig ( { } ) ,
1121+ getTestEnv ( ) ,
1122+ ) ,
1123+ {
1124+ message : `${ ActionsEnvVars . GITHUB_WORKSPACE } environment variable must be set` ,
1125+ } ,
1126+ ) ;
1127+ } ) ;
0 commit comments