Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle multiline targets with tabs #46

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,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 @@ -88,12 +89,12 @@ private static function parseLine(
): array {
$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 @@ -122,11 +123,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 @@ -152,10 +157,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 @@ -39,6 +39,7 @@
use Fidry\Makefile\Parser;
use Fidry\Makefile\Rule;
use PHPUnit\Framework\TestCase;
use function str_replace;

/**
* @covers \Fidry\Makefile\Parser
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