Skip to content

Commit

Permalink
Adds new stdin-input option
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Mar 3, 2025
1 parent 186ec0c commit 69aa34f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 7 deletions.
4 changes: 3 additions & 1 deletion bin/lean-package-validator
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use Stolt\LeanPackage\Commands\ValidateCommand;
use Stolt\LeanPackage\Analyser;
use Stolt\LeanPackage\Archive;
use Stolt\LeanPackage\Archive\Validator;
use Stolt\LeanPackage\Helpers\PhpInputReader;
use Stolt\LeanPackage\Presets\Finder;
use Stolt\LeanPackage\Presets\PhpPreset;
use Stolt\LeanPackage\Tree;
Expand All @@ -44,7 +45,8 @@ $initCommand = new InitCommand(
);
$validateCommand = new ValidateCommand(
$analyser,
new Validator($archive)
new Validator($archive),
new PhpInputReader()
);
$treeCommand = new TreeCommand(new Tree(new Archive(WORKING_DIRECTORY,'tree-temp')));

Expand Down
37 changes: 34 additions & 3 deletions src/Commands/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Stolt\LeanPackage\Commands;

use function PHPUnit\Framework\stringContains;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use SplFileInfo;
Expand All @@ -17,6 +18,7 @@
use Stolt\LeanPackage\Exceptions\InvalidGlobPatternFile;
use Stolt\LeanPackage\Exceptions\NoLicenseFilePresent;
use Stolt\LeanPackage\Exceptions\NonExistentGlobPatternFile;
use Stolt\LeanPackage\Helpers\InputReader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -47,13 +49,22 @@ final class ValidateCommand extends Command
protected Validator $archiveValidator;

/**
* @param Analyser $analyser
* Input reader.
*
* @var InputReader
*/
protected InputReader $inputReader;

/**
* @param Analyser $analyser
* @param Validator $archiveValidator
* @param InputReader $inputReader
*/
public function __construct(Analyser $analyser, Validator $archiveValidator)
public function __construct(Analyser $analyser, Validator $archiveValidator, InputReader $inputReader)
{
$this->analyser = $analyser;
$this->archiveValidator = $archiveValidator;
$this->inputReader = $inputReader;

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

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

$this->addOption('stdin-input', null, InputOption::VALUE_NONE, $stdinInputDescription);
$this->addOption('create', 'c', InputOption::VALUE_NONE, $createDescription);
$this->addOption(
'enforce-strict-order',
Expand Down Expand Up @@ -220,6 +233,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$stdinInput = $input->getOption('stdin-input');

if ($stdinInput !== false) {
$stdinInputContents = $this->inputReader->get();

if (!\strpos($stdinInputContents, 'export-ignore')) {
$warning = "Warning: The provided input stream seems to be no .gitattributes content.";
$outputContent = '<error>' . $warning . '</error>';
$output->writeln($outputContent);

return Command::FAILURE;
}

$verboseOutput = '+ Validating .gitattributes content from STDIN.';
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);

return Command::SUCCESS;
}

$verboseOutput = '+ Scanning directory ' . $directory . '.';
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);

Expand Down Expand Up @@ -302,7 +334,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}


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

if ($alignExportIgnores) {
Expand Down
10 changes: 10 additions & 0 deletions src/Helpers/InputReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Stolt\LeanPackage\Helpers;

interface InputReader
{
public function get(): string;
}
15 changes: 15 additions & 0 deletions src/Helpers/PhpInputReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Stolt\LeanPackage\Helpers;

use Stolt\LeanPackage\Helpers\InputReader;

class PhpInputReader implements InputReader
{
public function get(): string
{
return \file_get_contents('php://stdin');

Check failure on line 13 in src/Helpers/PhpInputReader.php

View workflow job for this annotation

GitHub Actions / static-analyse (8.3)

Method Stolt\LeanPackage\Helpers\PhpInputReader::get() should return string but returns string|false.
}
}
40 changes: 37 additions & 3 deletions tests/Commands/ValidateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use phpmock\MockBuilder;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\Ticket;
use Stolt\LeanPackage\Analyser;
Expand All @@ -21,6 +22,7 @@
use Stolt\LeanPackage\Presets\Finder;
use Stolt\LeanPackage\Presets\PhpPreset;
use Stolt\LeanPackage\Tests\CommandTester;
use Stolt\LeanPackage\Tests\Helpers\FakeInputReader;
use Stolt\LeanPackage\Tests\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
Expand All @@ -43,7 +45,8 @@ protected function setUp(): void

$analyserCommand = new ValidateCommand(
new Analyser(new Finder(new PhpPreset())),
new Validator(new Archive($this->temporaryDirectory))
new Validator(new Archive($this->temporaryDirectory)),
new FakeInputReader()
);

$this->application = $this->getApplication($analyserCommand);
Expand Down Expand Up @@ -2196,6 +2199,35 @@ public function gitignoredFilesAreExcludedFromValidation(): void
$commandTester->assertCommandIsSuccessful();
}


#[Test]
#[RunInSeparateProcess]
public function detectsNonGitattributeContentInStdinInput(): void
{
$application = new Application();
$fakeInputReader = new FakeInputReader();
$fakeInputReader->set('Some non .gitattributes content.');

$analyserCommand = new ValidateCommand(
new Analyser(new Finder(new PhpPreset())),
new Validator(new Archive(\getcwd())),
$fakeInputReader
);

$application->add($analyserCommand);
$command = $application->find('validate');

$expectedDisplay = <<<CONTENT
Warning: The provided input stream seems to be no .gitattributes content.
CONTENT;

TestCommand::for($command)
->addOption('stdin-input')
->execute()
->assertOutputContains($expectedDisplay)
->assertFaulty();
}

/**
* @return array
*/
Expand All @@ -2221,7 +2253,8 @@ protected function getApplicationWithMockedAnalyser(MockInterface $mockedAnalyse

$analyserCommand = new ValidateCommand(
$mockedAnalyser,
new Validator($archive)
new Validator($archive),
new FakeInputReader()
);

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

$analyserCommand = new ValidateCommand(
new Analyser(new Finder(new PhpPreset())),
$mockedArchiveValidator
$mockedArchiveValidator,
new FakeInputReader()
);

$application->add($analyserCommand);
Expand Down
21 changes: 21 additions & 0 deletions tests/Helpers/FakeInputReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Stolt\LeanPackage\Tests\Helpers;

use Stolt\LeanPackage\Helpers\InputReader;

class FakeInputReader implements InputReader
{
private string $input = '';

public function set(string $input): void
{
$this->input = $input;
}
public function get(): string
{
return $this->input;
}
}

0 comments on commit 69aa34f

Please sign in to comment.