-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from timoschinkel/feature/list-owners
Introduce command `list-owners`
- Loading branch information
Showing
6 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeOwners\Cli\Command; | ||
|
||
use CodeOwners\Cli\FileLocator\FileLocatorFactoryInterface; | ||
use CodeOwners\ParserInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class ListOwnersCommand extends Command | ||
{ | ||
private const NAME = 'list-owners'; | ||
|
||
/** @var string */ | ||
private $workingDirectory; | ||
|
||
/** @var FileLocatorFactoryInterface */ | ||
private $fileLocatorFactory; | ||
|
||
/** @var ParserInterface */ | ||
private $parser; | ||
|
||
public function __construct( | ||
string $workingDirectory, | ||
FileLocatorFactoryInterface $fileLocatorFactory, | ||
ParserInterface $parser | ||
) { | ||
$this->workingDirectory = rtrim($workingDirectory, DIRECTORY_SEPARATOR); | ||
$this->fileLocatorFactory = $fileLocatorFactory; | ||
$this->parser = $parser; | ||
|
||
parent::__construct(self::NAME); | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this | ||
->setDescription('List all code owners in a CODEOWNERS file') | ||
->addOption( | ||
'codeowners', | ||
'c', | ||
InputArgument::OPTIONAL, | ||
'Location of code owners file, defaults to <working_dir>/CODEOWNERS' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
// Parsing input parameters: | ||
$codeownersLocation = $input->getOption('codeowners'); | ||
if (is_string($codeownersLocation) !== true) { | ||
$codeownersLocation = null; | ||
} | ||
|
||
$codeownersFile = $this->fileLocatorFactory | ||
->getFileLocator($this->workingDirectory, $codeownersLocation) | ||
->locateFile(); | ||
|
||
$output->writeln( | ||
"Using CODEOWNERS definition from {$codeownersFile}" . PHP_EOL, | ||
OutputInterface::VERBOSITY_VERBOSE | ||
); | ||
|
||
if (is_file($codeownersFile) === false) { | ||
throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $codeownersFile)); | ||
} | ||
|
||
$owners = []; | ||
|
||
foreach ($this->parser->parse($codeownersFile) as $pattern) { | ||
$owners = array_merge($owners, $pattern->getOwners()); | ||
} | ||
|
||
foreach (array_unique($owners) as $owner) { | ||
$output->writeln($owner); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeOwners\Cli\Tests\Command; | ||
|
||
use CodeOwners\Cli\Command\ListOwnersCommand; | ||
use CodeOwners\Cli\FileLocator\FileLocatorFactoryInterface; | ||
use CodeOwners\Cli\FileLocator\FileLocatorInterface; | ||
use CodeOwners\Cli\FileLocator\UnableToLocateFileException; | ||
use CodeOwners\Cli\PatternMatcherFactoryInterface; | ||
use CodeOwners\ParserInterface; | ||
use CodeOwners\Pattern; | ||
use org\bovigo\vfs\vfsStream; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
final class ListOwnersCommandTest extends TestCase | ||
{ | ||
/** @var FileLocatorFactoryInterface|ObjectProphecy */ | ||
private $fileLocatorFactory; | ||
|
||
/** @var PatternMatcherFactoryInterface|ObjectProphecy */ | ||
private $patternMatcherFactory; | ||
|
||
/** @var ParserInterface|ObjectProphecy */ | ||
private $parser; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->fileLocatorFactory = $this->prophesize(FileLocatorFactoryInterface::class); | ||
$this->patternMatcherFactory = $this->prophesize(PatternMatcherFactoryInterface::class); | ||
$this->parser = $this->prophesize(ParserInterface::class); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testCommandDisplaysOwnersFoundInCodeOwnersFile(): void | ||
{ | ||
$filesystem = vfsStream::setup('root', 444, [ | ||
'CODEOWNERS' => '#', | ||
]); | ||
|
||
$fileLocator = $this->prophesize(FileLocatorInterface::class); | ||
$fileLocator->locateFile() | ||
->shouldBeCalled() | ||
->willReturn($filesystem->url() . '/CODEOWNERS'); | ||
|
||
$this->fileLocatorFactory | ||
->getFileLocator(Argument::type('string'), null) | ||
->shouldBeCalled() | ||
->willReturn($fileLocator->reveal()); | ||
|
||
$this->parser | ||
->parse($filesystem->url() . '/CODEOWNERS') | ||
->shouldBeCalled() | ||
->willReturn([ | ||
new Pattern('pattern 01', ['owner 01', 'owner 02']), | ||
new Pattern('pattern 02', ['owner 01', 'owner 03']), | ||
new Pattern('pattern 03', ['owner 04']), | ||
]); | ||
|
||
$command = new ListOwnersCommand( | ||
$filesystem->url(), | ||
$this->fileLocatorFactory->reveal(), | ||
$this->parser->reveal() | ||
); | ||
|
||
$output = $this->executeCommand($command, []); | ||
self::assertEquals( | ||
join(PHP_EOL, ['owner 01', 'owner 02', 'owner 03', 'owner 04']) . PHP_EOL, | ||
$output | ||
); | ||
} | ||
|
||
public function testCommandPassesCodeownerFileLocation(): void | ||
{ | ||
$filesystem = vfsStream::setup('root', 444, []); | ||
|
||
$fileLocator = $this->prophesize(FileLocatorInterface::class); | ||
$fileLocator->locateFile() | ||
->shouldBeCalled() | ||
->willThrow(UnableToLocateFileException::class); | ||
|
||
$this->fileLocatorFactory | ||
->getFileLocator(Argument::type('string'), 'CODEOWNERS') | ||
->shouldBeCalled() | ||
->willReturn($fileLocator->reveal()); | ||
|
||
$command = new ListOwnersCommand( | ||
$filesystem->url(), | ||
$this->fileLocatorFactory->reveal(), | ||
$this->parser->reveal() | ||
); | ||
|
||
$this->expectException(UnableToLocateFileException::class); | ||
$this->executeCommand($command, [ | ||
'--codeowners' => 'CODEOWNERS', | ||
]); | ||
} | ||
|
||
public function testCommandThrowsExceptionIfCodeOwnersFileDoesNotExist(): void | ||
{ | ||
$filesystem = vfsStream::setup('root', 444, []); | ||
|
||
$fileLocator = $this->prophesize(FileLocatorInterface::class); | ||
$fileLocator->locateFile() | ||
->shouldBeCalled() | ||
->willReturn($filesystem->url() . '/CODEOWNERS'); | ||
|
||
$this->fileLocatorFactory | ||
->getFileLocator(Argument::type('string'), Argument::any()) | ||
->willReturn($fileLocator->reveal()); | ||
|
||
$command = new ListOwnersCommand( | ||
$filesystem->url(), | ||
$this->fileLocatorFactory->reveal(), | ||
$this->parser->reveal() | ||
); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->executeCommand($command, []); | ||
} | ||
|
||
private function executeCommand(Command $command, array $parameters): string | ||
{ | ||
$application = new Application(); | ||
$application->add($command); | ||
|
||
$tester = new CommandTester($application->find($command->getName())); | ||
$tester->execute($parameters); | ||
|
||
return $tester->getDisplay(); | ||
} | ||
} |