import { mkdir , rm , exec } from "shelljs" ; import { resolve , dirname , join } from "path" ; import { download , extract } from "gitly" ; import { existsSync } from "fs" ; function makeTestRepo ( name , install = npmInstaller , test = "npm test" ) { return { name , install , test } ; } type TestRepo = ReturnType < typeof makeTestRepo > ; function npmInstaller ( dependencies ) { return dependencies . map ( dep => `npm install --save "${ dep }" --ignore-scripts` ) . join ( " && " ) ; } /** a function that tests a package */ async function testPackage ( testRepo , dependencies , isSilent = false ) { const { name , install , test } = testRepo ; const distFolder = resolve ( join ( __dirname , "fixtures" , "downloaded" , name ) ) ; if ( ! packageExists ( distFolder ) ) { const source = await download ( name ) ; mkdir ( "-p" , distFolder ) ; await extract ( source , distFolder ) ; } if ( packageExists ( distFolder ) ) { exec ( install ( dependencies ) , { fatal : true , cwd : distFolder , silent : isSilent } ) ; exec ( test , { fatal : true , cwd : distFolder , silent : isSilent } ) ; return true ; } return false ; } /** pack a package */ function pack ( packageRoot ) { const packageJson = join ( packageRoot , "package.json" ) ; const pkg = require ( packageJson ) ; const packedPkg = join ( packageRoot , `${ pkg . name }-${ pkg . version }.tgz` ) ; rm ( "-rf" , packedPkg ) ; exec ( "npm pack" , { cwd : packageRoot , fatal : true } ) ; return packedPkg ; } function packageExists ( packageRoot ) { return existsSync ( join ( packageRoot , "package.json" ) ) ; } describe ( "Downloaded tests" , ( ) => { const testRepos = [ makeTestRepo ( "aminya/solid-simple-table" ) ] ; const clean = true ; let packedSolidPkg , packedBabelSolidPkg , dependencies ; beforeAll ( ( ) => { if ( ! existsSync ( resolve ( join ( __dirname , "../../solid/dist" ) ) ) ) { throw new Error ( "Solid is not built. Run `npm run build`" ) ; } if ( clean ) { rm ( "-rf" , resolve ( join ( __dirname , "./fixtures/downloaded" ) ) ) ; } packedSolidPkg = pack ( resolve ( __dirname , "../../solid" ) ) ; packedBabelSolidPkg = pack ( resolve ( __dirname , "../../babel-preset-solid" ) ) ; dependencies = [ packedSolidPkg , packedBabelSolidPkg ] ; } ) ; for ( const testRepo of testRepos ) { test ( testRepo . name , async ( ) => { const pass = await testPackage ( testRepo , dependencies ) ; expect ( pass ) . toBe ( true ) ; } ) ; } afterAll ( ( ) => { rm ( "-rf" , packedSolidPkg ) ; rm ( "-rf" , packedBabelSolidPkg ) ; } ) ; } ) ;