Skip to content

Commit 784dc95

Browse files
committed
Adds new stdin-input option
1 parent 186ec0c commit 784dc95

File tree

8 files changed

+151
-10
lines changed

8 files changed

+151
-10
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ Would expect the following .gitattributes file content:
179179
The `--report-stale-export-ignores` option extends the validation to look for export-ignore statements referencing non-existent
180180
repository artifacts. In combination with the `--diff` option these will be shown in the output.
181181

182+
The `--stdin-input` option allows the validate command to read from `STDIN` so that the following can be used for validation.
183+
It currently only does a strict comparison.
184+
185+
```bash
186+
cat .gitattributes | lean-package-validator validate --stdin-input
187+
```
188+
182189
### Additional commands
183190

184191
#### Init command

bin/lean-package-validator

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use Stolt\LeanPackage\Commands\ValidateCommand;
3030
use Stolt\LeanPackage\Analyser;
3131
use Stolt\LeanPackage\Archive;
3232
use Stolt\LeanPackage\Archive\Validator;
33+
use Stolt\LeanPackage\Helpers\PhpInputReader;
3334
use Stolt\LeanPackage\Presets\Finder;
3435
use Stolt\LeanPackage\Presets\PhpPreset;
3536
use Stolt\LeanPackage\Tree;
@@ -44,7 +45,8 @@ $initCommand = new InitCommand(
4445
);
4546
$validateCommand = new ValidateCommand(
4647
$analyser,
47-
new Validator($archive)
48+
new Validator($archive),
49+
new PhpInputReader()
4850
);
4951
$treeCommand = new TreeCommand(new Tree(new Archive(WORKING_DIRECTORY,'tree-temp')));
5052

src/Analyser.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,15 +853,18 @@ public function getPresentNonExportIgnoresContent(): string
853853
* the .gitattributes file.
854854
*
855855
* @param bool $applyGlob
856+
* @param string $gitattributesContent
856857
* @return array
857858
*/
858-
public function getPresentExportIgnores(bool $applyGlob = true): array
859+
public function getPresentExportIgnores(bool $applyGlob = true, string $gitattributesContent = ''): array
859860
{
860-
if ($this->hasGitattributesFile() === false) {
861+
if ($this->hasGitattributesFile() === false && $gitattributesContent === '') {
861862
return [];
862863
}
863864

864-
$gitattributesContent = (string) \file_get_contents($this->gitattributesFile);
865+
if ($gitattributesContent === '') {
866+
$gitattributesContent = (string) \file_get_contents($this->gitattributesFile);
867+
}
865868

866869
$gitattributesLines = \preg_split(
867870
'/\\r\\n|\\r|\\n/',
@@ -936,6 +939,14 @@ private function getByDirectoriesToFilesExportIgnoreArtifacts(array $artifacts):
936939
return \array_merge($directories, $files);
937940
}
938941

942+
public function hasCompleteExportIgnoresFromString(string $gitattributesContent): bool
943+
{
944+
$expectedExportIgnores = $this->collectExpectedExportIgnores();
945+
$presentExportIgnores = $this->getPresentExportIgnores(true, $gitattributesContent);
946+
947+
return \array_values($expectedExportIgnores) === \array_values($presentExportIgnores);
948+
}
949+
939950
/**
940951
* Is existing .gitattributes file having all export-ignore(s).
941952
*

src/Commands/ValidateCommand.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Stolt\LeanPackage\Commands;
66

7+
use function PHPUnit\Framework\stringContains;
78
use SebastianBergmann\Diff\Differ;
89
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
910
use SplFileInfo;
@@ -17,6 +18,7 @@
1718
use Stolt\LeanPackage\Exceptions\InvalidGlobPatternFile;
1819
use Stolt\LeanPackage\Exceptions\NoLicenseFilePresent;
1920
use Stolt\LeanPackage\Exceptions\NonExistentGlobPatternFile;
21+
use Stolt\LeanPackage\Helpers\InputReader;
2022
use Symfony\Component\Console\Command\Command;
2123
use Symfony\Component\Console\Input\InputArgument;
2224
use Symfony\Component\Console\Input\InputInterface;
@@ -47,13 +49,22 @@ final class ValidateCommand extends Command
4749
protected Validator $archiveValidator;
4850

4951
/**
50-
* @param Analyser $analyser
52+
* Input reader.
53+
*
54+
* @var InputReader
55+
*/
56+
protected InputReader $inputReader;
57+
58+
/**
59+
* @param Analyser $analyser
5160
* @param Validator $archiveValidator
61+
* @param InputReader $inputReader
5262
*/
53-
public function __construct(Analyser $analyser, Validator $archiveValidator)
63+
public function __construct(Analyser $analyser, Validator $archiveValidator, InputReader $inputReader)
5464
{
5565
$this->analyser = $analyser;
5666
$this->archiveValidator = $archiveValidator;
67+
$this->inputReader = $inputReader;
5768

5869
parent::__construct();
5970
}
@@ -105,7 +116,9 @@ protected function configure(): void
105116
$sortDescription = 'Sort from directories to files';
106117

107118
$alignExportIgnoresDescription = 'Align export-ignores on create or overwrite';
119+
$stdinInputDescription = "Read .gitattributes content from standard input";
108120

121+
$this->addOption('stdin-input', null, InputOption::VALUE_NONE, $stdinInputDescription);
109122
$this->addOption('create', 'c', InputOption::VALUE_NONE, $createDescription);
110123
$this->addOption(
111124
'enforce-strict-order',
@@ -220,6 +233,35 @@ protected function execute(InputInterface $input, OutputInterface $output): int
220233
}
221234
}
222235

236+
$stdinInput = $input->getOption('stdin-input');
237+
238+
if ($stdinInput !== false) {
239+
$stdinInputContents = $this->inputReader->get();
240+
241+
if (!\strpos($stdinInputContents, 'export-ignore')) {
242+
$warning = "Warning: The provided input stream seems to be no .gitattributes content.";
243+
$outputContent = '<error>' . $warning . '</error>';
244+
$output->writeln($outputContent);
245+
246+
return Command::FAILURE;
247+
}
248+
249+
$verboseOutput = '+ Validating .gitattributes content from STDIN.';
250+
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
251+
252+
if ($this->analyser->hasCompleteExportIgnoresFromString($stdinInputContents)) {
253+
$info = 'The provided .gitattributes content is considered <info>valid</info>.';
254+
$output->writeln($info);
255+
256+
return Command::SUCCESS;
257+
}
258+
259+
$outputContent = 'The provided .gitattributes file is considered <error>invalid</error>.';
260+
$output->writeln($outputContent);
261+
262+
return Command::FAILURE;
263+
}
264+
223265
$verboseOutput = '+ Scanning directory ' . $directory . '.';
224266
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
225267

@@ -302,7 +344,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
302344
}
303345
}
304346

305-
306347
$alignExportIgnores = $input->getOption('align-export-ignores');
307348

308349
if ($alignExportIgnores) {

src/Helpers/InputReader.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Helpers;
6+
7+
interface InputReader
8+
{
9+
public function get(): string|false;
10+
}

src/Helpers/PhpInputReader.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Helpers;
6+
7+
use Stolt\LeanPackage\Helpers\InputReader;
8+
9+
class PhpInputReader implements InputReader
10+
{
11+
public function get(): string|false
12+
{
13+
return \file_get_contents('php://stdin');
14+
}
15+
}

tests/Commands/ValidateCommandTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use phpmock\MockBuilder;
1111
use PHPUnit\Framework\Attributes\DataProvider;
1212
use PHPUnit\Framework\Attributes\Group;
13+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
1314
use PHPUnit\Framework\Attributes\Test;
1415
use PHPUnit\Framework\Attributes\Ticket;
1516
use Stolt\LeanPackage\Analyser;
@@ -21,6 +22,7 @@
2122
use Stolt\LeanPackage\Presets\Finder;
2223
use Stolt\LeanPackage\Presets\PhpPreset;
2324
use Stolt\LeanPackage\Tests\CommandTester;
25+
use Stolt\LeanPackage\Tests\Helpers\FakeInputReader;
2426
use Stolt\LeanPackage\Tests\TestCase;
2527
use Symfony\Component\Console\Application;
2628
use Symfony\Component\Console\Command\Command;
@@ -43,7 +45,8 @@ protected function setUp(): void
4345

4446
$analyserCommand = new ValidateCommand(
4547
new Analyser(new Finder(new PhpPreset())),
46-
new Validator(new Archive($this->temporaryDirectory))
48+
new Validator(new Archive($this->temporaryDirectory)),
49+
new FakeInputReader()
4750
);
4851

4952
$this->application = $this->getApplication($analyserCommand);
@@ -2196,6 +2199,35 @@ public function gitignoredFilesAreExcludedFromValidation(): void
21962199
$commandTester->assertCommandIsSuccessful();
21972200
}
21982201

2202+
2203+
#[Test]
2204+
#[RunInSeparateProcess]
2205+
public function detectsNonGitattributeContentInStdinInput(): void
2206+
{
2207+
$application = new Application();
2208+
$fakeInputReader = new FakeInputReader();
2209+
$fakeInputReader->set('Some non .gitattributes content.');
2210+
2211+
$analyserCommand = new ValidateCommand(
2212+
new Analyser(new Finder(new PhpPreset())),
2213+
new Validator(new Archive(\getcwd())),
2214+
$fakeInputReader
2215+
);
2216+
2217+
$application->add($analyserCommand);
2218+
$command = $application->find('validate');
2219+
2220+
$expectedDisplay = <<<CONTENT
2221+
Warning: The provided input stream seems to be no .gitattributes content.
2222+
CONTENT;
2223+
2224+
TestCommand::for($command)
2225+
->addOption('stdin-input')
2226+
->execute()
2227+
->assertOutputContains($expectedDisplay)
2228+
->assertFaulty();
2229+
}
2230+
21992231
/**
22002232
* @return array
22012233
*/
@@ -2221,7 +2253,8 @@ protected function getApplicationWithMockedAnalyser(MockInterface $mockedAnalyse
22212253

22222254
$analyserCommand = new ValidateCommand(
22232255
$mockedAnalyser,
2224-
new Validator($archive)
2256+
new Validator($archive),
2257+
new FakeInputReader()
22252258
);
22262259

22272260
$application->add($analyserCommand);
@@ -2239,7 +2272,8 @@ protected function getApplicationWithMockedArchiveValidator(MockInterface $mocke
22392272

22402273
$analyserCommand = new ValidateCommand(
22412274
new Analyser(new Finder(new PhpPreset())),
2242-
$mockedArchiveValidator
2275+
$mockedArchiveValidator,
2276+
new FakeInputReader()
22432277
);
22442278

22452279
$application->add($analyserCommand);

tests/Helpers/FakeInputReader.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Tests\Helpers;
6+
7+
use Stolt\LeanPackage\Helpers\InputReader;
8+
9+
class FakeInputReader implements InputReader
10+
{
11+
private string $input = '';
12+
13+
public function set(string $input): void
14+
{
15+
$this->input = $input;
16+
}
17+
public function get(): string|false
18+
{
19+
return $this->input;
20+
}
21+
}

0 commit comments

Comments
 (0)