Skip to content

Commit

Permalink
fix: Handle multiline targets with tabs (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 13, 2023
1 parent fb346e2 commit be5a048
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
"ergebnis/composer-normalize": true,
"infection/extension-installer": true,
"bamarni/composer-bin-plugin": true
"infection/extension-installer": true
},
"sort-packages": true
},
Expand Down
7 changes: 7 additions & 0 deletions infection.json5.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
"Fidry\\Makefile\\Test\\BaseMakefileTestCase::getNonDebugMakeFlags"
]
},
"Concat": {
"ignoreSourceCodeByRegex": [
// This transformation is useless here. Also note that it is probably not a "contact" but a string
// permutation.
".+\\$charactersToTrim = \\$multiline \\?.+",
]
},
"FunctionCallRemoval": {
"ignore": [
"Fidry\\Makefile\\Test\\BaseMakefileTestCase::executeCommand",
Expand Down
21 changes: 14 additions & 7 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use function rtrim;
use function Safe\preg_match;
use function str_contains;
use function str_ends_with;
use function str_starts_with;
use function trim;
use const PHP_EOL;
Expand Down Expand Up @@ -89,12 +90,12 @@ private static function parseLine(
$parsedRules = array_values($parsedRules);
$line = rtrim($line);

if (!self::isRule($line)) {
if (!self::isRule($line, $multiline)) {
return $parsedRules;
}

$previousMultiline = $multiline;
$multiline = '\\' === mb_substr($line, -1);
$multiline = str_ends_with($line, '\\');

if (false === $previousMultiline) {
$targetParts = explode(':', $line);
Expand Down Expand Up @@ -123,11 +124,15 @@ private static function parseLine(
return $parsedRules;
}

private static function isRule(string $line): bool
private static function isRule(string $line, bool $previousMultiline): bool
{
return !str_starts_with($line, '#')
&& !str_starts_with($line, "\t")
&& 0 === preg_match('/\S+=.+/', $line);
return (!str_starts_with($line, '#')
&& !str_starts_with($line, "\t")
&& 0 === preg_match('/\S+=.+/', $line)
)
|| (
$previousMultiline && (str_starts_with($line, "\t") || str_starts_with($line, ' '))
);
}

/**
Expand All @@ -153,10 +158,12 @@ private static function parsePrerequisites(
$ignoreNextLinesOfMultiline = true;
}

$charactersToTrim = $multiline ? '\\'."\t" : "\t";

return array_values(
array_filter(
array_map(
static fn (string $dependency) => $multiline ? ltrim($dependency, '\\') : $dependency,
static fn (string $dependency) => ltrim($dependency, $charactersToTrim),
explode(' ', $dependencies),
),
),
Expand Down
39 changes: 39 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function str_replace;

/**
* @internal
Expand Down Expand Up @@ -179,6 +180,44 @@ public static function makefileContentProvider(): iterable
],
];

yield 'PHP-Scoper multiline pre-requisite' => [
<<<'MAKEFILE'
include .makefile/e2e.file
.PHONY: e2e
e2e: ## Runs end-to-end tests
e2e: e2e_004 \
e2e_036 \
e2e_037

MAKEFILE,
[
new Rule('.PHONY', ['e2e']),
new Rule('e2e', ['## Runs end-to-end tests']),
new Rule('e2e', ['e2e_004', 'e2e_036', 'e2e_037']),
],
];

yield 'multiline pre-requisite with tabs' => [
str_replace(
' ',
"\t\t",
<<<'MAKEFILE'
include .makefile/e2e.file
.PHONY: e2e
e2e: ## Runs end-to-end tests
e2e: e2e_004 \
e2e_036 \
e2e_037

MAKEFILE,
),
[
new Rule('.PHONY', ['e2e']),
new Rule('e2e', ['## Runs end-to-end tests']),
new Rule('e2e', ['e2e_004', 'e2e_036', 'e2e_037']),
],
];

yield 'variables' => [
<<<'MAKEFILE'
.DEFAULT_GOAL := help
Expand Down

0 comments on commit be5a048

Please sign in to comment.