Skip to content

Commit d0aaabd

Browse files
committed
Fixes generated or modified header
1 parent 4b565f6 commit d0aaabd

File tree

4 files changed

+120
-18
lines changed

4 files changed

+120
-18
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
88
## [Unreleased]
99

1010

11+
## [v5.2.1] - 2025-10-17
12+
13+
### Fixed
14+
- The correct file `HEADERS` (generated|modified) are written.
15+
1116
## [v5.2.0] - 2025-09-26
1217

1318
### Added
@@ -430,7 +435,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
430435

431436
- Initial release.
432437

433-
[Unreleased]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.2.0...HEAD
438+
[Unreleased]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.2.1...HEAD
439+
[v5.2.1]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.2.0...v5.2.1
434440
[v5.2.0]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.1.0...v5.2.0
435441
[v5.1.0]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.0.0...v5.1.0
436442
[v5.0.0]: https://github.com/raphaelstolt/lean-package-validator/compare/v4.7.1...v5.0.0

src/GitattributesFileRepository.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,15 @@ public function applyOverwriteHeaderPolicy(string $contentToWrite): string
9595
$gitattributesPath = $this->analyser->getGitattributesFilePath();
9696

9797
if (!\is_file($gitattributesPath)) {
98-
return $contentToWrite;
98+
return self::GENERATED_HEADER . PHP_EOL . PHP_EOL . $contentToWrite;
9999
}
100100

101-
$presentContent = (string) @\file_get_contents($gitattributesPath);
102-
if ($presentContent === '') {
103-
return $contentToWrite;
101+
if (\str_contains($contentToWrite, self::GENERATED_HEADER)) {
102+
return str_replace(self::GENERATED_HEADER, self::MODIFIED_HEADER, $contentToWrite);
104103
}
105104

106-
if (\str_contains($presentContent, self::GENERATED_HEADER)) {
107-
$generatedPrefix = self::GENERATED_HEADER . PHP_EOL;
108-
$modifiedPrefix = self::MODIFIED_HEADER . PHP_EOL;
109-
110-
// If the new content starts with the "generated by" header, replace it.
111-
if (\str_starts_with($contentToWrite, $generatedPrefix)) {
112-
return $modifiedPrefix . \substr($contentToWrite, \strlen($generatedPrefix));
113-
}
114-
115-
// If no header is present at the top, prepend the "partly modified" header.
116-
if (!\str_starts_with($contentToWrite, $modifiedPrefix)) {
117-
return self::MODIFIED_HEADER . PHP_EOL . PHP_EOL . $contentToWrite;
118-
}
105+
if (!str_contains($contentToWrite, self::MODIFIED_HEADER) && !str_contains($contentToWrite, self::GENERATED_HEADER)) {
106+
return self::MODIFIED_HEADER . PHP_EOL . PHP_EOL . $contentToWrite;
119107
}
120108

121109
return $contentToWrite;

tests/Commands/ValidateCommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Stolt\LeanPackage\Commands\ValidateCommand;
2020
use Stolt\LeanPackage\Exceptions\InvalidGlobPattern;
2121
use Stolt\LeanPackage\Exceptions\NoLicenseFilePresent;
22+
use Stolt\LeanPackage\GitattributesFileRepository;
2223
use Stolt\LeanPackage\Helpers\Str as OsHelper;
2324
use Stolt\LeanPackage\Presets\Finder;
2425
use Stolt\LeanPackage\Presets\PhpPreset;
@@ -1617,6 +1618,19 @@ public function incompleteGitattributesFileIsOverwritten(string $option): void
16171618
['specs']
16181619
);
16191620

1621+
$header = GitattributesFileRepository::GENERATED_HEADER . PHP_EOL . PHP_EOL;
1622+
1623+
if ($option === '--overwrite') {
1624+
$header = GitattributesFileRepository::MODIFIED_HEADER . PHP_EOL;
1625+
}
1626+
1627+
if ($option === '--create') {
1628+
$header = GitattributesFileRepository::GENERATED_HEADER . PHP_EOL . PHP_EOL;
1629+
}
1630+
1631+
echo 'OPTION: ' . $option . PHP_EOL;
1632+
echo 'HEADER: ' . $header . PHP_EOL;
1633+
16201634
$command = $this->application->find('validate');
16211635
$commandTester = new CommandTester($command);
16221636
$commandTester->execute([
@@ -1633,6 +1647,7 @@ public function incompleteGitattributesFileIsOverwritten(string $option): void
16331647
The present .gitattributes file is considered invalid.
16341648
16351649
Overwrote it with the shown content:
1650+
$header
16361651
* text=auto eol=lf
16371652
16381653
.gitattributes export-ignore
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Stolt\LeanPackage\Tests;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Stolt\LeanPackage\Analyser;
7+
use Stolt\LeanPackage\GitattributesFileRepository;
8+
use Stolt\LeanPackage\Presets\Finder;
9+
use Stolt\LeanPackage\Presets\PhpPreset;
10+
11+
final class GitattributesFileRepositoryTest extends TestCase
12+
{
13+
public function setUp(): void
14+
{
15+
$this->setUpTemporaryDirectory();
16+
}
17+
18+
/**
19+
* Tear down the test environment.
20+
*
21+
* @return void
22+
*/
23+
protected function tearDown(): void
24+
{
25+
if (\is_dir($this->temporaryDirectory)) {
26+
$this->removeDirectory($this->temporaryDirectory);
27+
}
28+
}
29+
30+
#[Test]
31+
public function addsExpectedFileHeaders(): void
32+
{
33+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
34+
35+
$repository = new GitattributesFileRepository($analyser);
36+
37+
$fakeGitattributesContent = <<<CONTENT
38+
* text=auto eol=lf
39+
40+
.gitattributes export-ignore
41+
phpspec.yml.dist export-ignore
42+
specs/ export-ignore
43+
version-increase-command export-ignore
44+
CONTENT;
45+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
46+
47+
$this->assertStringContainsString(GitattributesFileRepository::GENERATED_HEADER, $contentWithFileHeader);
48+
49+
$generatedHeader = GitattributesFileRepository::GENERATED_HEADER . PHP_EOL . PHP_EOL;
50+
$fakeGitattributesContent = <<<CONTENT
51+
$generatedHeader
52+
* text=auto eol=lf
53+
54+
.gitattributes export-ignore
55+
phpspec.yml.dist export-ignore
56+
specs/ export-ignore
57+
version-increase-command export-ignore
58+
CONTENT;
59+
60+
\touch($this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes');
61+
62+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
63+
64+
$this->assertStringContainsString(GitattributesFileRepository::MODIFIED_HEADER, $contentWithFileHeader);
65+
66+
$modifiedHeader = GitattributesFileRepository::MODIFIED_HEADER . PHP_EOL . PHP_EOL;
67+
$fakeGitattributesContent = <<<CONTENT
68+
$modifiedHeader
69+
* text=auto eol=lf
70+
71+
.gitattributes export-ignore
72+
phpspec.yml.dist export-ignore
73+
specs/ export-ignore
74+
version-increase-command export-ignore
75+
CONTENT;
76+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
77+
78+
$this->assertStringContainsString(GitattributesFileRepository::MODIFIED_HEADER, $contentWithFileHeader);
79+
80+
$fakeGitattributesContent = <<<CONTENT
81+
* text=auto eol=lf
82+
83+
.gitattributes export-ignore
84+
phpspec.yml.dist export-ignore
85+
specs/ export-ignore
86+
version-increase-command export-ignore
87+
CONTENT;
88+
89+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
90+
91+
$this->assertStringContainsString(GitattributesFileRepository::MODIFIED_HEADER, $contentWithFileHeader);
92+
}
93+
}

0 commit comments

Comments
 (0)