[英]Spawning interactive CLI command with Expect exits with 0 early, when run as Symfony Process in Behat context
這是一個非常先進的問題,也許沒有必要了解Symfony和Behat來理解這個問題。
因此,為了測試我使用Symfony控制台組件用PHP編寫的交互式CLI應用程序bin/albumgrab
的輸入和輸出,我已經設置了我的Behat特性上下文來構建一個expect
腳本,通過exec
運行。
此exec
命令通過Symfony Process
在PHP中運行。
$expect = <<<BASH
exec expect -c '
set timeout 180
spawn bin/albumgrab
expect "Please enter the name of the directory"
send "/tmp/php-london\n"
expect "Please enter the URL to the first image"
send "https://www.facebook.com/PeeHPLondon/photos/pb.7119218495.-2207520000.1430669248./10153559172718496/?type=3&src=https%3A%2F%2Ffbcdn-sphotos-g-a.akamaihd.net%2Fhphotos-ak-xfp1%2Fv%2Ft1.0-9%2F10986697_10153559172718496_5727444485530442900_n.jpg%3Foh%3Dc47770f4cd15fecc6888bcd504899087%26oe%3D55DA9CB0%26__gda__%3D1439174101_7c78a93bf247dbad6c56681b6db5309c&size=960%2C959&fbid=10153559172718496\\n"
interact
'
BASH;
$process = new Symfony\Component\Process\Process($expect);
$process->mustRun();
但是,當它超過第二個輸入時,它似乎退出但成功。
呼叫:
$process->setTty(true);
使它一直運行,但會直接打印到stdout,我不能再捕獲輸出來進行斷言,即使使用PHP的輸出緩沖也是如此。
我認為PTY無論如何都會更合適:
$process->setPty(true);
因為它是StackOverflow問題的解決方案。 但是,這在任何地方都不受支持,至少在Mac OS X上不支持。
您可以在Github中看到我到目前為止所嘗試的內容: https : //github.com/adamelso/albumgrab/pull/13/files和最后一次嘗試的Travis輸出https://travis-ci.org/adamelso/ albumgrab /職位/ 61137499
所以我的主要問題是它為什么一直提前退出0以及如何防止它?
為了能夠接收問題的答案 - 您肯定需要終端(TTY)或偽終端(PTY)來獲得任何用戶輸入。
這就是為什么 - 沒有$process->setTty(true)
或setPty(true)
- QuestionHelper默默地回退到默認值,命令成功,退出代碼為0。
現在 - 使用示例用戶輸入來測試命令 - 您應該使用symfony的控制台助手組件而不是使用expect。
Symfony\Component\Console\Helper\HelperSet
Symfony\Component\Console\Tester\CommandTester
如何使用這些助手在烹飪書章節測試期望輸入的命令中進行了描述。
根據Expect Script wait命令掛起的答案,您需要等待EOF而不是使用interact
命令:
set timeout 180
spawn ./bin/albumgrab
expect "Please enter the name of the directory"
send "/tmp/php-london\n"
expect "Please enter the URL to the first image"
send "https://www.facebook.com/PeeHPLondon/photos/pb.7119218495.-2207520000.1430669248./10153559172718496/\n"
expect EOF
這是我用來在OS X上測試它的完整腳本:
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\Process\Process;
$expect = <<<BASH
exec expect -c '
set timeout 180
spawn ./bin/albumgrab
expect "Please enter the name of the directory"
send "/tmp/php-london\n"
expect "Please enter the URL to the first image"
send "https://www.facebook.com/PeeHPLondon/photos/pb.7119218495.-2207520000.1430669248./10153559172718496/?type=3&src=https%3A%2F%2Ffbcdn-sphotos-g-a.akamaihd.net%2Fhphotos-ak-xfp1%2Fv%2Ft1.0-9%2F10986697_10153559172718496_5727444485530442900_n.jpg%3Foh%3Dc47770f4cd15fecc6888bcd504899087%26oe%3D55DA9CB0%26__gda__%3D1439174101_7c78a93bf247dbad6c56681b6db5309c&size=960%2C959&fbid=10153559172718496\\n"
expect EOF
'
BASH;
$process = new Process($expect);
$process->setPty(true);
$process->start();
$process->wait(function ($type, $buffer) {
if (Process::ERR === $type) {
echo 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
}
});
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.