import tmp = require ( 'tmp' ) ; import * as path from 'path' ; import { spawn } from 'nexpect' ; const NODE = process . execPath ; const NEON = path . resolve ( 'bin/cli.js' ) ; export interface Spawnable { cwd : string ; spawn ( args ) : NexpectChain ; spawn ( args , options ) : NexpectChain ; spawn ( args , params , options ) : NexpectChain ; } export interface NexpectOptions { cwd ? : string ; env ? : object ; ignoreCase ? : boolean ; stripColors ? : boolean ; stream ? : 'stdout' | 'stderr' | 'all' ; verbose ? : boolean ; } export interface NexpectChain { expect ( expectation ) : NexpectChain ; wait ( expectation ) : NexpectChain ; sendline ( line ) : NexpectChain ; sendEof ( ) : NexpectChain ; run ( callback ) : void ; } function isSpawnable < T > ( x ) { return ! ! x && typeof x === 'object' && typeof ( x as any ) . cwd === 'string' && typeof ( x as any ) . spawn === 'function' ; } export function spawnable ( obj ) { if ( ! isSpawnable ( obj ) ) { throw new TypeError ( "mocha callback run without running setup()" ) ; } return obj ; } export function setup ( stream = 'all' ) { let tmpobj ; beforeEach ( function ( ) { tmpobj = tmp . dirSync ( { unsafeCleanup : true } ) ; this . cwd = tmpobj . name ; this . spawn = ( args ) => spawn ( NODE , [ NEON ] . concat ( args ) , { cwd : this . cwd , stream , stripColors : true } ) ; } ) ; afterEach ( function ( ) { delete this . cwd ; delete this . spawn ; tmpobj . removeCallback ( ) ; } ) ; } ;