运行序列同步任务无法完成
我几乎可以肯定要对这个错误的方式,所以首先我的高层次需求。
我使用的是angular2-seed并想通过Xvfb运行在一个无头模式量角器测试。我不想要一个的Xvfb服务器始终运行(这是一个构建服务器),所以不是我想旋转起来的的Xvfb服务,有量角器做的事情,然后“优雅地”关闭的Xvfb。在隔离这些任务都工作正常,但我已经打了一堵墙,当涉及到将它们添加到一饮而尽构建设置。
下面是在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创建一个承诺,代码没有做它的早期但可能interations去,所以......请注意,调试日志记录应是在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
有人可以把我直/把我在正确的方向,请?
回答如下:由于朱利从angular2种子球队!
这个错误是在不调用从包装类,即
export = runProtractor()
的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();
}
});
}