import findUp from 'find-up' ; import fs from 'fs-extra' ; import path from 'path' ; import tmp from 'tmp-promise' ; tmp . setGracefulCleanup ( ) ; let fixturesRoot ; let tempRoot ; let tempNumber = 0 ; /**
 * Create a temp directory containing the given fixture name in a git repo.
 * Be sure to call `cleanupFixtures()` after all tests to clean up temp directories.
 */ export function setupFixture ( fixtureName ) { if ( ! fixturesRoot ) { fixturesRoot = findUp . sync ( 'fixtures' , { cwd : __dirname , type : 'directory' , } ) ; } const fixturePath = path . join ( fixturesRoot ! , 'unit' , fixtureName ) ; if ( ! fs . existsSync ( fixturePath ) ) { throw new Error ( `Couldn't find fixture "${ fixtureName }" under "${ path . join ( fixturesRoot ! , 'unit' ) }"` ) ; } if ( ! tempRoot ) { tempRoot = tmp . dirSync ( { unsafeCleanup : true } ) ; } const cwd = path . join ( tempRoot . name , String ( tempNumber ++ ) , fixtureName ) ; fs . mkdirpSync ( cwd ) ; fs . copySync ( fixturePath , cwd ) ; return cwd ; } export function cleanupFixtures ( ) { if ( tempRoot ) { tempRoot . removeCallback ( ) ; tempRoot = undefined ; } } afterAll ( ( ) => { cleanupFixtures ( ) ; } ) ;