-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathCommandTest.php
298 lines (283 loc) · 11.4 KB
/
CommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
use mikehaertl\shellcommand\Command;
class CommandTest extends \PHPUnit\Framework\TestCase
{
public function setUp()
{
// Default in some installations
setlocale(LC_CTYPE, 'C');
}
// Create command from command string
public function testCanPassCommandStringToConstructor()
{
$command = new Command('/bin/ls -l');
$this->assertEquals('/bin/ls -l', $command->getExecCommand());
}
public function testCanPassCommandOptionToConstructor()
{
$command = new Command(array(
'command' => '/bin/ls -l',
));
$this->assertEquals('/bin/ls -l', $command->getExecCommand());
}
public function testCanSetCommand()
{
$command = new Command();
$command->setCommand('/bin/ls -l');
$this->assertEquals('/bin/ls -l', $command->getExecCommand());
}
// Options
public function testCanSetOptions()
{
$command = new Command;
$command->setOptions(array(
'command' => 'echo',
'escapeArgs' => false,
'procEnv' => array('TESTVAR' => 'test'),
'args' => '-n $TESTVAR',
));
$this->assertEquals('echo -n $TESTVAR', $command->getExecCommand());
$this->assertFalse($command->escapeArgs);
$this->assertFalse($command->getExecuted());
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals('test', $command->getOutput());
}
public function testCanPassOptionsToConstructor()
{
$command = new Command(array(
'command' => 'echo',
'args' => '-n $TESTVAR',
'escapeArgs' => false,
'procEnv' => array('TESTVAR' => 'test'),
));
$this->assertEquals('echo -n $TESTVAR', $command->getExecCommand());
$this->assertFalse($command->escapeArgs);
$this->assertFalse($command->getExecuted());
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals('test', $command->getOutput());
}
// Arguments
public function testCanAddArguments()
{
$command = new Command(array(
'locale' => 'en_US.UTF-8',
));
$command->setCommand('test');
$command->setArgs('--arg1=x');
$command->addArg('--a');
$command->addArg('--a', '中文字äüp');
$command->addArg('--a', array("v'1",'v2','v3'));
$command->addArg('-b=','v', false);
$command->addArg('-b=', array('v4','v5','v6'));
$command->addArg('-c', '');
$command->addArg('some name', null, true);
$this->assertEquals("--arg1=x --a --a '中文字äüp' --a 'v'\''1' 'v2' 'v3' -b=v -b='v4' 'v5' 'v6' -c '' 'some name'", $command->getArgs());
$this->assertEquals("test --arg1=x --a --a '中文字äüp' --a 'v'\''1' 'v2' 'v3' -b=v -b='v4' 'v5' 'v6' -c '' 'some name'", $command->getExecCommand());
}
public function testCanResetArguments()
{
$command = new Command();
$command->addArg('--demo');
$command->addArg('-name=test');
$command->setArgs('--arg1=x');
$this->assertEquals("--arg1=x", $command->getArgs());
}
public function testCanDisableEscaping()
{
$command = new Command();
$command->escapeArgs = false;
$command->addArg('--a');
$command->addArg('--a', 'v');
$command->addArg('--a', array("v1",'v2','v3'));
$command->addArg('-b=','v', true);
$command->addArg('-b=', array('v4','v5','v6'));
$command->addArg('some name', null, true);
$this->assertEquals("--a --a v --a v1 v2 v3 -b='v' -b=v4 v5 v6 'some name'", $command->getArgs());
}
public function testCanRunCommandWithArguments()
{
$command = new Command('ls');
$command->addArg('-l');
$command->addArg('-n');
$this->assertEquals("ls -l -n", $command->getExecCommand());
$this->assertFalse($command->getExecuted());
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
}
// Output / error / exit code
public function testCanRunValidCommand()
{
$dir = __DIR__;
$command = new Command("/bin/ls $dir/Command*");
$this->assertFalse($command->getExecuted());
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals("$dir/CommandTest.php", $command->getOutput());
$this->assertEquals("$dir/CommandTest.php\n", $command->getOutput(false));
$this->assertEmpty($command->getError());
$this->assertEmpty($command->getStdErr());
$this->assertEquals(0, $command->getExitCode());
}
public function testCanNotRunEmptyCommand()
{
$command = new Command('');
$this->assertFalse($command->execute());
$this->assertEquals('Could not locate any executable command', $command->getError());
}
public function testCanNotRunNotExistantCommand()
{
$command = new Command('/does/not/exist');
$this->assertFalse($command->getExecuted());
$this->assertFalse($command->execute());
$this->assertFalse($command->getExecuted());
$this->assertNotEmpty($command->getError());
$this->assertNotEmpty($command->getStdErr());
$this->assertEmpty($command->getOutput());
$this->assertEquals(127, $command->getExitCode());
}
public function testCanNotRunInvalidCommand()
{
$command = new Command('ls --this-does-not-exist');
$this->assertFalse($command->getExecuted());
$this->assertFalse($command->execute());
$this->assertFalse($command->getExecuted());
$this->assertNotEmpty($command->getError());
$this->assertNotEmpty($command->getStdErr());
$this->assertEmpty($command->getOutput());
$this->assertEquals(2, $command->getExitCode());
}
public function testCanCastToString()
{
$command = new Command('ls');
$command->addArg('-l');
$command->addArg('-n');
$this->assertEquals("ls -l -n", (string)$command);
}
// Exec
public function testCanRunValidCommandWithExec()
{
$dir = __DIR__;
$command = new Command("/bin/ls $dir/Command*");
$command->useExec = true;
$this->assertFalse($command->getExecuted());
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals("$dir/CommandTest.php", $command->getOutput());
$this->assertEmpty($command->getError());
$this->assertEmpty($command->getStdErr());
$this->assertEquals(0, $command->getExitCode());
}
public function testCanNotRunNotExistantCommandWithExec()
{
$command = new Command('/does/not/exist');
$command->useExec = true;
$this->assertFalse($command->getExecuted());
$this->assertFalse($command->execute());
$this->assertFalse($command->getExecuted());
$this->assertNotEmpty($command->getError());
$this->assertNotEmpty($command->getStdErr());
$this->assertNotEmpty($command->getOutput());
$this->assertEquals(127, $command->getExitCode());
}
public function testCanNotRunInvalidCommandWithExec()
{
$command = new Command('ls --this-does-not-exist');
$command->useExec = true;
$this->assertFalse($command->getExecuted());
$this->assertFalse($command->execute());
$this->assertFalse($command->getExecuted());
$this->assertNotEmpty($command->getError());
$this->assertNotEmpty($command->getStdErr());
$this->assertNotEmpty($command->getOutput());
$this->assertEquals(2, $command->getExitCode());
}
// Proc
public function testCanProvideProcEnvVars()
{
$command = new Command('echo $TESTVAR');
$command->procEnv = array('TESTVAR' => 'testvalue');
$this->assertTrue($command->execute());
$this->assertEquals("testvalue", $command->getOutput());
}
public function testCanProvideProcDir()
{
$tmpDir = sys_get_temp_dir();
$command = new Command('pwd');
$command->procCwd = $tmpDir;
$this->assertFalse($command->getExecuted());
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals($tmpDir, $command->getOutput());
}
public function testCanRunCommandWithStandardInput()
{
$command = new Command('/bin/cat');
$command->addArg('-T');
$command->setStdIn("\t");
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals("^I", $command->getOutput());
}
public function testCanRunCommandWithStandardInputStream()
{
$string = str_repeat('01234567890abcdef', 16 * 1024); // 16 * 16 * 1024 = 256KB
$tmpfile = tmpfile();
fwrite($tmpfile, $string);
fseek($tmpfile, 0);
$command = new Command('/bin/cat');
$command->setStdIn($tmpfile);
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals(strlen($string), strlen($command->getOutput()));
fclose($tmpfile);
}
public function testCanRunCommandWithBigInputAndOutput()
{
$string = str_repeat('01234567890abcdef', 16 * 1024); // 16 * 16 * 1024 = 256KB
$command = new Command('/bin/cat');
$command->setStdIn($string);
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals(strlen($string), strlen($command->getOutput()));
}
public function testCanRunLongRunningCommandWithBigInputAndOutput()
{
$string = str_repeat('01234567890abcdef', 16 * 1024); // 16 * 16 * 1024 = 256KB
$command = new Command('/bin/cat; echo "start" ; sleep 2 ; echo "done"');
$command->setStdIn($string);
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$expected = $string . "start\ndone";
$this->assertEquals(strlen($expected), strlen($command->getOutput()));
}
public function testCanRunLongRunningCommandWithStandardInputStream()
{
$string = str_repeat('01234567890abcdef', 16 * 1024); // 16 * 16 * 1024 = 256KB
$tmpfile = tmpfile();
fwrite($tmpfile, $string);
fseek($tmpfile, 0);
$command = new Command('/bin/cat; echo "start" ; sleep 2 ; echo "done"');
$command->setStdIn($tmpfile);
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$expected = $string . "start\ndone";
$this->assertEquals(strlen($expected), strlen($command->getOutput()));
fclose($tmpfile);
}
public function testCanTerminateLongRunningCommandWithTimeout()
{
$command = new Command('sleep 5');
$command->timeout = 2;
$startTime = time();
$this->assertFalse($command->execute());
$stopTime = time();
$this->assertFalse($command->getExecuted());
$this->assertNotEquals(0, $command->getExitCode());
$this->assertStringStartsWith('Command terminated by signal', $command->getError());
$this->assertStringStartsWith('Command terminated by signal', $command->getStdErr());
$this->assertEmpty($command->getOutput());
$this->assertEquals(2, $stopTime - $startTime);
}
}