0

私はほぼ間違いなくこれを間違った方法で行っているので、最初に私の高レベルの要件を説明します。

angular2-seedを使用しており、Xvfb を使用して分度器テストをヘッドレス モードで実行したいと考えています。Xvfbサーバーを常に実行したくないので(これはビルドサーバーです)、代わりにXvfbサービスを起動し、分度器にそれを実行させてから、Xvfbを「正常に」シャットダウンします。単独では、これらのタスクは正常に機能していますが、それらを gulp ビルド セットアップに追加する際に壁にぶつかりました。

gulpfile のタスクは次のとおりです。

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

タスク自体は、個々の typescript タスク ファイルを通じて読み込まれます。つまり、次のようになります。

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

export = runProtractor

そして、これが私の(最新の)ユーティリティファイルそのものです。

分度器.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);
            }
        });
}

execchild_processから promise を作成するためにおそらく家を回っているように感じますが、以前のコードのインターレーションではそれができなかったので...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

誰かが私をまっすぐに立てたり、正しい方向に押したりできますか??

4

2 に答える 2

0

gulp タスクにコールバック関数を追加し、すべての 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);
    });
});
于 2016-04-13T04:23:25.607 に答える