Skip to content

Commit e35f51b

Browse files
committed
Avoids duplicated headers. Closes #54.
1 parent d5ffe92 commit e35f51b

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

src/Commands/ValidateCommand.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ final class ValidateCommand extends Command
3636

3737
protected string $defaultPreset = 'Php';
3838

39+
protected string $generatedHeader = '# This file was generated by the lean package validator (http://git.io/lean-package-validator).';
40+
41+
protected string $modifiedHeader = '# This file was partly modified by the lean package validator (http://git.io/lean-package-validator).';
42+
3943
/**
4044
* Package analyser.
4145
*
@@ -446,8 +450,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
446450
if ($createGitattributesFile || $overwriteGitattributesFile) {
447451
try {
448452
if ($omitHeader === false) {
449-
$headerContent = '# This file was generated by the lean package validator (http://git.io/lean-package-validator).' . PHP_EOL;
450-
$expectedGitattributesFileContent = $headerContent . PHP_EOL . $expectedGitattributesFileContent;
453+
$expectedGitattributesFileContent = $this->generatedHeader . PHP_EOL . PHP_EOL . $expectedGitattributesFileContent;
451454
}
452455

453456
$outputContent .= $this->createGitattributesFile(
@@ -541,10 +544,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
541544
}
542545
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
543546

544-
545547
if ($omitHeader === false) {
546-
$headerContent = '# This file was partly modified by the lean package validator (http://git.io/lean-package-validator).' . PHP_EOL;
547-
$expectedGitattributesFileContent = $headerContent . PHP_EOL . $expectedGitattributesFileContent;
548+
if (\str_contains($expectedGitattributesFileContent, $this->generatedHeader)) {
549+
$expectedGitattributesFileContent = \str_replace(
550+
$this->generatedHeader . PHP_EOL . PHP_EOL,
551+
'',
552+
$expectedGitattributesFileContent
553+
);
554+
}
555+
$expectedGitattributesFileContent = $this->modifiedHeader . PHP_EOL . PHP_EOL . $expectedGitattributesFileContent;
548556
}
549557

550558
$outputContent .= $this->overwriteGitattributesFile(

tests/Commands/ValidateCommandTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,67 @@ public function staleExportIgnoresAreConsideredAsInvalid(): void
15231523
$this->assertTrue($commandTester->getStatusCode() > Command::SUCCESS);
15241524
}
15251525

1526+
#[Test]
1527+
#[Ticket('https://github.com/raphaelstolt/lean-package-validator/issues/54')]
1528+
public function createdHeaderIsReplacedByModifiedHeader(): void
1529+
{
1530+
$gitattributesContent = <<<CONTENT
1531+
# This file was generated by the lean package validator (http://git.io/lean-package-validator).
1532+
1533+
# These files are always considered text and should use LF.
1534+
# See core.whitespace @ http://git-scm.com/docs/git-config for whitespace flags.
1535+
1536+
*.php text eol=lf
1537+
1538+
# Ignore all non production artifacts with an "export-ignore".
1539+
.gitattributes export-ignore
1540+
.github/ export-ignore
1541+
.gitignore export-ignore
1542+
phpunit.xml.dist export-ignore
1543+
1544+
CONTENT;
1545+
1546+
$this->createTemporaryGitattributesFile($gitattributesContent);
1547+
1548+
$artifactFilenames = ['.gitignore', '.gitattributes', 'phpunit.xml.dist'];
1549+
1550+
$this->createTemporaryFiles(
1551+
$artifactFilenames,
1552+
['.github', 'tests']
1553+
);
1554+
1555+
$command = $this->application->find('validate');
1556+
$commandTester = new CommandTester($command);
1557+
$commandTester->execute([
1558+
'command' => $command->getName(),
1559+
'directory' => WORKING_DIRECTORY,
1560+
'--overwrite' => true
1561+
]);
1562+
1563+
1564+
$expectedGitattributesContent = <<<CONTENT
1565+
# This file was partly modified by the lean package validator (http://git.io/lean-package-validator).
1566+
1567+
# These files are always considered text and should use LF.
1568+
# See core.whitespace @ http://git-scm.com/docs/git-config for whitespace flags.
1569+
1570+
*.php text eol=lf
1571+
1572+
# Ignore all non production artifacts with an "export-ignore".
1573+
.gitattributes export-ignore
1574+
.github/ export-ignore
1575+
.gitignore export-ignore
1576+
phpunit.xml.dist export-ignore
1577+
tests/ export-ignore
1578+
1579+
CONTENT;
1580+
1581+
$this->assertStringEqualsFile(
1582+
WORKING_DIRECTORY . DIRECTORY_SEPARATOR . '.gitattributes',
1583+
$expectedGitattributesContent
1584+
);
1585+
}
1586+
15261587
#[Test]
15271588
#[Ticket('https://github.com/raphaelstolt/lean-package-validator/issues/8')]
15281589
#[DataProvider('optionProvider')]

0 commit comments

Comments
 (0)