Skip to content

Commit 7103fed

Browse files
committed
- increase code coverage. more tests
- fix parser bug
1 parent 129b43e commit 7103fed

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/SystemdState/Parser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private static function parseExecLine(string $line): ExecCommand
130130

131131
$explode = array_map('trim', explode(';', $matches['command']));
132132
array_walk($explode, function (string $value, int $key, ExecCommand $execCommand) {
133-
$split = explode(' ', $value);
133+
$split = explode('=', $value);
134134
$array = self::parseExecDetail($split);
135135
if (count($array)) {
136136
$execCommand->{$array[0]} = $array[1];
@@ -153,10 +153,10 @@ private static function parseExecDetail(array $values): array
153153
}
154154

155155
if ($index === 'argv[]') {
156-
array_shift($values);
157-
array_shift($values);
156+
$explode = explode(' ', $values[1]);
157+
array_shift($explode);
158158

159-
return ['argv', implode(' ', $values),];
159+
return ['argv', implode(' ', $explode),];
160160
}
161161

162162
if ($index === 'start_time') {

src/SystemdState/SystemdState.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private function handleServiceResponse(string $service): void
138138
*/
139139
private function handleLine(string $line, int $key, TypesInterface $type): void
140140
{
141-
$explode = explode('=', $line);
141+
$explode = explode('=', $line, 2);
142142
$propertyName = $explode[0];
143143

144144
if (empty($propertyName)) {

tests/ParserTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ public function dpParseValueByContent()
1414
$dateTime = \date_create_immutable_from_format('* Y-m-d H:i:s e', time());
1515
$execCommand = new ExecCommand;
1616

17+
$execCommand2 = new ExecCommand;
18+
$execCommand2->path = '/usr/sbin/nginx';
19+
$execCommand2->argv = '-c /etc/nginx/nginx.conf';
20+
$execCommand2->ignoreErrors = false;
21+
$execCommand2->startTime = '[n/a]';
22+
$execCommand2->stopTime = '[n/a]';
23+
$execCommand2->pid = 0;
24+
$execCommand2->code = '(null)';
25+
$execCommand2->status = '0/0';
26+
1727
$testCases = [
1828
[['test'], null],
1929
[['test', 'yes'], true],
@@ -25,7 +35,8 @@ public function dpParseValueByContent()
2535
[['test', '18446744073709551615'], '18446744073709551615'],
2636
[['Timestamp', (string)$dateTime], $dateTime],
2737
[['Environment', 'FOO=bar BAZ=foo'], ['FOO' => 'bar', 'BAZ' => 'foo']],
28-
[['ExecStart', ''], $execCommand]
38+
[['ExecStart', ''], $execCommand],
39+
[['ExecStart', '{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -c /etc/nginx/nginx.conf ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }'], $execCommand2],
2940
];
3041

3142
// To cover codecoverage

0 commit comments

Comments
 (0)