Skip to content

Commit c7b85ac

Browse files
committed
Reformats negated export-ignore directives
1 parent 92f67f8 commit c7b85ac

3 files changed

Lines changed: 109 additions & 4 deletions

File tree

CHANGELOG.md

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

1212
### Added
1313
- Expanded the `validation` to also validate __negated__ export-ignore directives. Closes [#70](https://github.com/raphaelstolt/lean-package-validator/issues/70).
14-
- New `--flavour` option for the `create` command which influences the `.gitattributes` generation. Closes [#71](https://github.com/raphaelstolt/lean-package-validator/issues/71).
14+
- New `--flavour` option for the `create` command which influences the `.gitattributes` file generation. Closes [#71](https://github.com/raphaelstolt/lean-package-validator/issues/71).
15+
- Expanded the `reformatting` to also reformat __negated__ export-ignore directives. Closes [#72](https://github.com/raphaelstolt/lean-package-validator/issues/72).
1516

1617
## [v5.9.1] - 2026-05-17
1718

src/Analyser.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,13 @@ public function getReformattedGitattributesContent(): string
970970
continue;
971971
}
972972

973+
973974
[$pattern] = \explode('export-ignore', $line, 2);
974975

976+
if ($this->isNegatedExportIgnoreLine($line)) {
977+
[$pattern] = \explode('-export-ignore', $line, 2);
978+
}
979+
975980
$exportIgnorePatterns[] = \rtrim($pattern);
976981
}
977982

@@ -985,8 +990,14 @@ public function getReformattedGitattributesContent(): string
985990
if ($this->isAlignableExportIgnoreLine($line) === false) {
986991
return $line;
987992
}
993+
$exportIgnorePattern = 'export-ignore';
994+
995+
if ($this->isNegatedExportIgnoreLine($line)) {
996+
$exportIgnorePattern = '-export-ignore';
997+
}
998+
999+
[$pattern, $suffix] = \explode($exportIgnorePattern, $line, 2);
9881000

989-
[$pattern, $suffix] = \explode('export-ignore', $line, 2);
9901001
$pattern = \trim($pattern);
9911002

9921003
if (\str_starts_with($pattern, '/')) {
@@ -1001,7 +1012,7 @@ public function getReformattedGitattributesContent(): string
10011012
$longestPattern = \strlen($pattern);
10021013
}
10031014

1004-
return $pattern . \str_repeat(' ', $longestPattern - \strlen($pattern) + 1) . 'export-ignore' . $suffix;
1015+
return $pattern . \str_repeat(' ', $longestPattern - \strlen($pattern) + 1) . $exportIgnorePattern . $suffix;
10051016
}, $gitattributesLines);
10061017

10071018
if ($this->sortAlphabetically === true) {
@@ -1131,10 +1142,16 @@ private function collapseAndTrimBlankLines(array $lines): array
11311142

11321143
private function isAlignableExportIgnoreLine(string $line): bool
11331144
{
1134-
return \str_contains($line, 'export-ignore')
1145+
return (\str_contains($line, 'export-ignore') || \str_contains($line, '-export-ignore'))
1146+
&& \str_starts_with(\trim($line), '* export-ignore') === false
11351147
&& \str_starts_with(\ltrim($line), '#') === false;
11361148
}
11371149

1150+
private function isNegatedExportIgnoreLine(string $line): bool
1151+
{
1152+
return \str_contains($line, '-export-ignore') && \str_starts_with(\ltrim($line), '#') === false;
1153+
}
1154+
11381155
/**
11391156
* Reorganise lines into a non-export-ignore section followed by an
11401157
* export-ignore section. A comment immediately preceding an export-ignore

tests/AnalyserTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,93 @@ protected function tearDown(): void
3737
}
3838
}
3939

40+
#[Test]
41+
public function returnExpectedReformattedGitattributesContentForClassicExportIgnoreDirectives(): void
42+
{
43+
$mockedAnalyser = Mockery::mock(Analyser::class)->makePartial();
44+
45+
$gitattributesContent = <<<CONTENT
46+
* text=auto eol=lf
47+
48+
bin/ export-ignore
49+
composer.json export-ignore
50+
resources/ export-ignore
51+
src/ export-ignore
52+
CHANGELOG.md export-ignore
53+
CONTENT;
54+
55+
$mockedAnalyser->shouldReceive('getPresentGitAttributesContent')
56+
->once()
57+
->withNoArgs()
58+
->andReturn($gitattributesContent);
59+
60+
$mockedAnalyser->setDirectory($this->temporaryDirectory);
61+
62+
$reformattedGitattributesContent = $mockedAnalyser->getReformattedGitattributesContent();
63+
64+
$expectedReformattedGitattributesContent = <<<CONTENT
65+
* text=auto eol=lf
66+
67+
bin/ export-ignore
68+
composer.json export-ignore
69+
resources/ export-ignore
70+
src/ export-ignore
71+
CHANGELOG.md export-ignore
72+
CONTENT;
73+
74+
$this->assertStringContainsStringIgnoringLineEndings(
75+
$expectedReformattedGitattributesContent,
76+
$reformattedGitattributesContent
77+
);
78+
}
79+
80+
#[Test]
81+
public function returnExpectedReformattedGitattributesContentForNegatedExportIgnoreDirectives(): void
82+
{
83+
$mockedAnalyser = Mockery::mock(Analyser::class)->makePartial();
84+
85+
$gitattributesContent = <<<CONTENT
86+
* text=auto eol=lf
87+
88+
# Exclude all per default
89+
* export-ignore
90+
91+
# re-include required files
92+
bin/ -export-ignore
93+
composer.json -export-ignore
94+
resources/ -export-ignore
95+
src/ -export-ignore
96+
CONTENT;
97+
98+
$mockedAnalyser->shouldReceive('getPresentGitAttributesContent')
99+
->once()
100+
->withNoArgs()
101+
->andReturn($gitattributesContent);
102+
103+
$mockedAnalyser->setDirectory($this->temporaryDirectory);
104+
105+
$reformattedGitattributesContent = $mockedAnalyser->getReformattedGitattributesContent();
106+
107+
$expectedReformattedGitattributesContent = <<<CONTENT
108+
* text=auto eol=lf
109+
110+
# Exclude all per default
111+
* export-ignore
112+
113+
# re-include required files
114+
bin/ -export-ignore
115+
composer.json -export-ignore
116+
resources/ -export-ignore
117+
src/ -export-ignore
118+
CONTENT;
119+
120+
$this->assertStringContainsStringIgnoringLineEndings(
121+
$expectedReformattedGitattributesContent,
122+
$reformattedGitattributesContent
123+
);
124+
}
125+
126+
40127
#[Test]
41128
public function throwsExceptionOnNonExpectedFlavour(): void
42129
{

0 commit comments

Comments
 (0)