1- import { sep } from "path" ;
21import * as fetch from "node-fetch" ;
32import { Range } from "semver" ;
43
54import * as helpers from "../../../src/helpers" ;
65import { extLogger } from "../../../src/common" ;
76import * as fs from "fs-extra" ;
7+ import * as path from "path" ;
88import * as os from "os" ;
9+ import * as tmp from "tmp-promise" ;
910import {
1011 GithubRelease ,
1112 GithubReleaseAsset ,
1213 ReleasesApiConsumer ,
1314 getExecutableFromDirectory ,
1415 DistributionManager ,
1516} from "../../../src/distribution" ;
17+ import { DirectoryResult } from "tmp-promise" ;
18+
19+ jest . mock ( "os" , ( ) => {
20+ const original = jest . requireActual ( "os" ) ;
21+ return {
22+ ...original ,
23+ platform : jest . fn ( ) ,
24+ } ;
25+ } ) ;
26+
27+ const mockedOS = jest . mocked ( os ) ;
1628
1729describe ( "Releases API consumer" , ( ) => {
1830 const owner = "someowner" ;
@@ -192,40 +204,42 @@ describe("Releases API consumer", () => {
192204} ) ;
193205
194206describe ( "Launcher path" , ( ) => {
195- const pathToCmd = `abc${ sep } codeql.cmd` ;
196- const pathToExe = `abc${ sep } codeql.exe` ;
197-
198207 let warnSpy : jest . SpiedFunction < typeof helpers . showAndLogWarningMessage > ;
199208 let errorSpy : jest . SpiedFunction < typeof helpers . showAndLogErrorMessage > ;
200209 let logSpy : jest . SpiedFunction < typeof extLogger . log > ;
201- let pathExistsSpy : jest . SpiedFunction < typeof fs . pathExists > ;
202210
203- let launcherThatExists = "" ;
211+ let directory : DirectoryResult ;
204212
205- beforeEach ( ( ) => {
213+ let pathToCmd : string ;
214+ let pathToExe : string ;
215+
216+ beforeEach ( async ( ) => {
206217 warnSpy = jest
207218 . spyOn ( helpers , "showAndLogWarningMessage" )
208219 . mockResolvedValue ( undefined ) ;
209220 errorSpy = jest
210221 . spyOn ( helpers , "showAndLogErrorMessage" )
211222 . mockResolvedValue ( undefined ) ;
212223 logSpy = jest . spyOn ( extLogger , "log" ) . mockResolvedValue ( undefined ) ;
213- pathExistsSpy = jest
214- . spyOn ( fs , "pathExists" )
215- . mockImplementation ( async ( path : string ) => {
216- return path . endsWith ( launcherThatExists ) ;
217- } ) ;
218224
219- jest . spyOn ( os , "platform" ) . mockReturnValue ( "win32" ) ;
225+ mockedOS . platform . mockReturnValue ( "win32" ) ;
226+
227+ directory = await tmp . dir ( {
228+ unsafeCleanup : true ,
229+ } ) ;
230+
231+ pathToCmd = path . join ( directory . path , "codeql.cmd" ) ;
232+ pathToExe = path . join ( directory . path , "codeql.exe" ) ;
233+ } ) ;
234+
235+ afterEach ( async ( ) => {
236+ await directory . cleanup ( ) ;
220237 } ) ;
221238
222239 it ( "should not warn with proper launcher name" , async ( ) => {
223- launcherThatExists = "codeql.exe" ;
224- const result = await getExecutableFromDirectory ( "abc" ) ;
225- expect ( pathExistsSpy ) . toBeCalledWith ( pathToExe ) ;
240+ await fs . writeFile ( pathToExe , "" ) ;
226241
227- // correct launcher has been found, so alternate one not looked for
228- expect ( pathExistsSpy ) . not . toBeCalledWith ( pathToCmd ) ;
242+ const result = await getExecutableFromDirectory ( directory . path ) ;
229243
230244 // no warning message
231245 expect ( warnSpy ) . not . toHaveBeenCalled ( ) ;
@@ -235,10 +249,9 @@ describe("Launcher path", () => {
235249 } ) ;
236250
237251 it ( "should warn when using a hard-coded deprecated launcher name" , async ( ) => {
238- launcherThatExists = "codeql.cmd" ;
239- const result = await getExecutableFromDirectory ( "abc" ) ;
240- expect ( pathExistsSpy ) . toBeCalledWith ( pathToExe ) ;
241- expect ( pathExistsSpy ) . toBeCalledWith ( pathToCmd ) ;
252+ await fs . writeFile ( pathToCmd , "" ) ;
253+
254+ const result = await getExecutableFromDirectory ( directory . path ) ;
242255
243256 // Should have opened a warning message
244257 expect ( warnSpy ) . toHaveBeenCalled ( ) ;
@@ -248,10 +261,7 @@ describe("Launcher path", () => {
248261 } ) ;
249262
250263 it ( "should avoid warn when no launcher is found" , async ( ) => {
251- launcherThatExists = "xxx" ;
252- const result = await getExecutableFromDirectory ( "abc" , false ) ;
253- expect ( pathExistsSpy ) . toBeCalledWith ( pathToExe ) ;
254- expect ( pathExistsSpy ) . toBeCalledWith ( pathToCmd ) ;
264+ const result = await getExecutableFromDirectory ( directory . path , false ) ;
255265
256266 // no warning message
257267 expect ( warnSpy ) . not . toHaveBeenCalled ( ) ;
@@ -261,10 +271,7 @@ describe("Launcher path", () => {
261271 } ) ;
262272
263273 it ( "should warn when no launcher is found" , async ( ) => {
264- launcherThatExists = "xxx" ;
265274 const result = await getExecutableFromDirectory ( "abc" , true ) ;
266- expect ( pathExistsSpy ) . toBeCalledWith ( pathToExe ) ;
267- expect ( pathExistsSpy ) . toBeCalledWith ( pathToCmd ) ;
268275
269276 // no warning message
270277 expect ( warnSpy ) . not . toHaveBeenCalled ( ) ;
@@ -274,12 +281,13 @@ describe("Launcher path", () => {
274281 } ) ;
275282
276283 it ( "should not warn when deprecated launcher is used, but no new launcher is available" , async function ( ) {
284+ await fs . writeFile ( pathToCmd , "" ) ;
285+
277286 const manager = new DistributionManager (
278287 { customCodeQlPath : pathToCmd } as any ,
279288 { } as any ,
280289 undefined as any ,
281290 ) ;
282- launcherThatExists = "codeql.cmd" ;
283291
284292 const result = await manager . getCodeQlPathWithoutVersionCheck ( ) ;
285293 expect ( result ) . toBe ( pathToCmd ) ;
@@ -290,12 +298,14 @@ describe("Launcher path", () => {
290298 } ) ;
291299
292300 it ( "should warn when deprecated launcher is used, and new launcher is available" , async ( ) => {
301+ await fs . writeFile ( pathToCmd , "" ) ;
302+ await fs . writeFile ( pathToExe , "" ) ;
303+
293304 const manager = new DistributionManager (
294305 { customCodeQlPath : pathToCmd } as any ,
295306 { } as any ,
296307 undefined as any ,
297308 ) ;
298- launcherThatExists = "" ; // pretend both launchers exist
299309
300310 const result = await manager . getCodeQlPathWithoutVersionCheck ( ) ;
301311 expect ( result ) . toBe ( pathToCmd ) ;
@@ -311,7 +321,6 @@ describe("Launcher path", () => {
311321 { } as any ,
312322 undefined as any ,
313323 ) ;
314- launcherThatExists = "xxx" ; // pretend neither launcher exists
315324
316325 const result = await manager . getCodeQlPathWithoutVersionCheck ( ) ;
317326 expect ( result ) . toBeUndefined ( ) ;
0 commit comments