Initiating an interactive CLI command with Expect exit from 0 before when executed as Symfony Process in Behat context
This is a pretty advanced question, maybe you won't need to know Symfony and Behat to understand the problem.
So, to test the input and output of an interactive CLI application bin/albumgrab
that I wrote in PHP using the Symfony Console component, I set up my Behat function context to create a expect
script, execute through exec
.
This command is exec
run in PHP via Symfony 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"
interact
'
BASH;
$process = new Symfony\Component\Process\Process($expect);
$process->mustRun();
However, when it goes through the second entrance, it appears to exit, but successfully.
Vocation:
$process->setTty(true);
Runs it completely, but will print directly to stdout and I can no longer capture the output to make an assertion, even with PHP output buffered.
I figured PTY would be more appropriate:
$process->setPty(true);
As it was in the solution on StackOverflow question . However, this is not supported everywhere, at least not for Mac OS X.
You can see what I have tried so far on Github: https://github.com/adamelso/albumgrab/pull/13/files and Travis output for last try https://travis-ci.org/adamelso/albumgrab/ jobs / 61137499
So my main question is why does it keep staying at 0 before and how to prevent it?
source to share
As per the answer Wait Script wait Command Hangs, you need to wait for EOF instead of command 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
Here's the complete Script I used for testing on 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());
}
source to share
To get the answer to the question - you definitely need a terminal (TTY) or pseudo-terminal (PTY) to receive any user input.
This is why - without $process->setTty(true)
or setPty(true)
- the QuestionHelper silently reverts to its default, and the command succeeds with an exit code of 0.
Now - to test your command with sample user input - you should be using the symfony console helper components, not expect.
Symfony\Component\Console\Helper\HelperSet
Symfony\Component\Console\Tester\CommandTester
How to use these helpers is described in the cookbook chapter Testing a Command That Expects Input .
source to share