@@ -255,6 +255,113 @@ test("setupCodeQLBundle logs the CodeQL CLI version being used when asked to dow
255255 } ) ;
256256} ) ;
257257
258+ test ( "getCodeQLSource correctly returns latest version from toolcache when tools == toolcache" , async ( t ) => {
259+ const loggedMessages : LoggedMessage [ ] = [ ] ;
260+ const logger = getRecordingLogger ( loggedMessages ) ;
261+
262+ const latestToolcacheVersion = "3.2.1" ;
263+ const latestVersionPath = "/path/to/latest" ;
264+ const testVersions = [ "2.3.1" , latestToolcacheVersion , "1.2.3" ] ;
265+ const findAllVersionsStub = sinon
266+ . stub ( toolcache , "findAllVersions" )
267+ . returns ( testVersions ) ;
268+ const findStub = sinon . stub ( toolcache , "find" ) ;
269+ findStub
270+ . withArgs ( "CodeQL" , latestToolcacheVersion )
271+ . returns ( latestVersionPath ) ;
272+
273+ await withTmpDir ( async ( tmpDir ) => {
274+ setupActionsVars ( tmpDir , tmpDir ) ;
275+ const source = await setupCodeql . getCodeQLSource (
276+ "toolcache" ,
277+ SAMPLE_DEFAULT_CLI_VERSION ,
278+ SAMPLE_DOTCOM_API_DETAILS ,
279+ GitHubVariant . DOTCOM ,
280+ false ,
281+ logger ,
282+ ) ;
283+
284+ // Check that the toolcache functions were called with the expected arguments
285+ t . assert (
286+ findAllVersionsStub . calledOnceWith ( "CodeQL" ) ,
287+ `toolcache.findAllVersions("CodeQL") wasn't called` ,
288+ ) ;
289+ t . assert (
290+ findStub . calledOnceWith ( "CodeQL" , latestToolcacheVersion ) ,
291+ `toolcache.find("CodeQL", ${ latestToolcacheVersion } ) wasn't called` ,
292+ ) ;
293+
294+ // Check that `sourceType` and `toolsVersion` match expectations.
295+ t . is ( source . sourceType , "toolcache" ) ;
296+ t . is ( source . toolsVersion , latestToolcacheVersion ) ;
297+
298+ // Check that key messages we would expect to find in the log are present.
299+ const expectedMessages : string [ ] = [
300+ `Attempting to use the latest CodeQL CLI version in the toolcache, as requested by 'tools: toolcache'.` ,
301+ `CLI version ${ latestToolcacheVersion } is the latest version in the toolcache.` ,
302+ `Using CodeQL CLI version ${ latestToolcacheVersion } from toolcache at ${ latestVersionPath } ` ,
303+ ] ;
304+ for ( const expectedMessage of expectedMessages ) {
305+ t . assert (
306+ loggedMessages . some (
307+ ( msg ) =>
308+ typeof msg . message === "string" &&
309+ msg . message . includes ( expectedMessage ) ,
310+ ) ,
311+ `Expected '${ expectedMessage } ' in the logger output, but didn't find it.` ,
312+ ) ;
313+ }
314+ } ) ;
315+ } ) ;
316+
317+ test ( "getCodeQLSource falls back to downloading the CLI if the toolcache doesn't have a CodeQL CLI when tools == toolcache" , async ( t ) => {
318+ const loggedMessages : LoggedMessage [ ] = [ ] ;
319+ const logger = getRecordingLogger ( loggedMessages ) ;
320+
321+ const testVersions = [ ] ;
322+ const findAllVersionsStub = sinon
323+ . stub ( toolcache , "findAllVersions" )
324+ . returns ( testVersions ) ;
325+
326+ await withTmpDir ( async ( tmpDir ) => {
327+ setupActionsVars ( tmpDir , tmpDir ) ;
328+ const source = await setupCodeql . getCodeQLSource (
329+ "toolcache" ,
330+ SAMPLE_DEFAULT_CLI_VERSION ,
331+ SAMPLE_DOTCOM_API_DETAILS ,
332+ GitHubVariant . DOTCOM ,
333+ false ,
334+ logger ,
335+ ) ;
336+
337+ // Check that the toolcache functions were called with the expected arguments
338+ t . assert (
339+ findAllVersionsStub . calledWith ( "CodeQL" ) ,
340+ `toolcache.findAllVersions("CodeQL") wasn't called` ,
341+ ) ;
342+
343+ // Check that `sourceType` and `toolsVersion` match expectations.
344+ t . is ( source . sourceType , "download" ) ;
345+ t . is ( source . toolsVersion , SAMPLE_DEFAULT_CLI_VERSION . cliVersion ) ;
346+
347+ // Check that key messages we would expect to find in the log are present.
348+ const expectedMessages : string [ ] = [
349+ `Attempting to use the latest CodeQL CLI version in the toolcache, as requested by 'tools: toolcache'.` ,
350+ `Found no CodeQL CLI in the toolcache, ignoring 'tools: toolcache'...` ,
351+ ] ;
352+ for ( const expectedMessage of expectedMessages ) {
353+ t . assert (
354+ loggedMessages . some (
355+ ( msg ) =>
356+ typeof msg . message === "string" &&
357+ msg . message . includes ( expectedMessage ) ,
358+ ) ,
359+ `Expected '${ expectedMessage } ' in the logger output, but didn't find it.` ,
360+ ) ;
361+ }
362+ } ) ;
363+ } ) ;
364+
258365test ( 'tryGetTagNameFromUrl extracts the right tag name for a repo name containing "codeql-bundle"' , ( t ) => {
259366 t . is (
260367 setupCodeql . tryGetTagNameFromUrl (
0 commit comments