2016-04-12 79 views
0

我几乎肯定会以错误的方式解决这个问题,所以首先提出我的高级需求。运行序列同步任务永远不会完成

我正在使用angular2-seed并希望使用Xvfb在无头模式下运行量角器测试。我不希望任何时候都运行Xvfb服务器(这是一个构建服务器),所以我想旋转一个Xvfb服务,使用量角器做它的事情,然后“优雅地”关闭Xvfb。孤立地说,这些任务工作正常,但是我将它们添加到gulp构建设置中时遇到了困难。

这里是在gulpfile任务:

gulp.task('e2e.headless', (done: any) => 
    runSequence('start.xvfb', 
       'protractor', 
       'stop.xvfb', 
       done)); 

本身是通过个人的打字稿任务文件装入任务,即:

import {runProtractor} from '../../utils'; 

export = runProtractor 

这里是我的(最新)实用程序文件本身。

protractor.ts

import * as util from 'gulp-util'; 
import {normalize, join} from 'path'; 
import {ChildProcess} from 'child_process'; 

function reportError(message: string) { 
    console.error(require('chalk').white.bgRed.bold(message)); 
    process.exit(1); 
} 

function promiseFromChildProcess(child: ChildProcess) { 
    return new Promise(function (resolve:() => void, reject:() => void) { 
    child.on('close', (code: any) => { 
     util.log('Exited with code: ', code); 
     resolve(); 
    }); 
    child.stdout.on('data', (data: any) => { 
     util.log(`stdout: ${data}`); 
    }); 

    child.stderr.on('data', (data: any) => { 
     util.log(`stderr: ${data}`); 
     reject(); 
    }); 
    }); 
} 

export function runProtractor(): (done:() => void) => void { 
    return done => { 
    const root = normalize(join(__dirname, '..', '..', '..')); 
    const exec = require('child_process').exec; 

    // Our Xvfb instance is running on :99 
    // TODO: Pass this in instead of hard-coding 
    process.env.DISPLAY=':99'; 
    util.log('cwd:', root); 

    let child = exec('protractor', { cwd: root, env: process.env}, 
     function (error: Error, stdout: NodeBuffer, stderr: NodeBuffer) { 
     if (error !== null) { 
      reportError('Protractor error: ' + error + stderr); 
     } 
     }); 
    promiseFromChildProcess(child).then(() => done()); 
    }; 
} 

xvfb_tools.ts

import * as util from 'gulp-util'; 

const exec = require('child_process').exec; 

function reportError(message: string) { 
    console.error(require('chalk').white.bgRed.bold(message)); 
    process.exit(1); 
} 

export function stopXvfb() { 
    return exec('pkill -c -n Xvfb', 
     function (error: NodeJS.ErrnoException, stdout: NodeBuffer, stderr: NodeBuffer) { 
      if (error !== null) { 
       reportError('Failed to kill Xvfb. Not really sure why...'); 
      } else if (stdout.toString() === '0') { 
       reportError('No known Xvfb instance. Is it running?'); 
      } else { 
       util.log('Xvfb terminated'); 
      } 
     }); 
} 

export function startXvfb() { 
    return exec('Xvfb :99 -ac -screen 0 1600x1200x24', 
     function (error: NodeJS.ErrnoException, stdout: NodeBuffer, stderr: NodeBuffer) { 
      if (error !== null && error.code !== null) { 
       reportError('Xvfb failed to start. Err: ' + error.code + ', ' + error + ', ' + stderr); 
      } 
     }); 
} 

我感觉好像我在房前屋后创建从一个承诺可能将我的exec child_process,但代码的早期交互没有它,所以... 请注意,显示根目录的runProtractor()应输出的调试日志记录永远不会被调用,所以我很确定这里有一个异步问题。这是从任务的输出:

[00:47:49] Starting 'e2e.headless'... 
[00:47:49] Starting 'start.xvfb'... 
[00:47:49] Finished 'start.xvfb' after 12 ms 
[00:47:49] Starting 'protractor'... 
[00:47:49] Finished 'protractor' after 5.74 ms 
[00:47:49] Starting 'stop.xvfb'... 
[00:47:49] Finished 'stop.xvfb' after 11 ms 
[00:47:49] Finished 'e2e.headless' after 38 ms 
[00:47:49] Xvfb terminated 

有人可以让我直/推我在正确的方向吗?

回答

0

感谢来自角2种子团队的Ludovic!

错误在于未从包装类中调用runProtractor函数,即 export = runProtractor()。一旦发现这一点,我就可以去除不必要的包装函数以及promiseFromChildProcess,这些都是分心的。

最后的任务只是一个匿名函数,它吞气回调“完成”退出时被称为:

function reportError(message: string) { 
    console.error(require('chalk').white.bgRed.bold(message)); 
    process.exit(1); 
} 

export = (done: any) => { 
    const root = normalize(join(__dirname, '..', '..', '..')); 
    const exec = require('child_process').exec; 

    process.env.DISPLAY=':99'; 
    util.log('cwd:', root); 

    exec('protractor', { cwd: root, env: process.env}, 
     function (error: Error, stdout: NodeBuffer, stderr: NodeBuffer) { 
     if (error !== null) { 
      reportError('Protractor error: ' + error + stderr); 
     } else { 
      done(); 
     } 
     }); 
} 
0

您需要为您的吞咽任务添加回调函数,并在所有runSequence任务完成后调用cb函数。

gulp.task('e2e.headless', (cb) => 
runSequence('start.xvfb', 
    'protractor', 
    'stop.xvfb', 
    (err) => 
     if (err) { 
      console.log(err.message); 
     } else { 
      console.log("Build finished successfully"); 
     } 
     cb(err); 
    }); 
}); 
+0

不是什么'done'是在我目前的任务吗? – GMeister

+0

是的,完成执行? – Vish

+0

这不是,请参阅我的答案为什么 - 谢谢 – GMeister

相关问题