Skip to content

Commit

Permalink
Merge pull request #81 from oallain/feature-excluded-directories
Browse files Browse the repository at this point in the history
add excluded directories
  • Loading branch information
OwlyCode authored Oct 8, 2019
2 parents e404c43 + 752263c commit 46abeb8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ twigcs /path/to/views --severity warning # Allow notices

With the example above, notices are still displayed but not altering the exit code.

You can also exclude relative subfolders of path like this :

```bash
twigcs /path/to/views --exclude vendor
```

Tips : You can use multiple _exclude_ parameters.

### Continuous Integration

Twigcs can be used with your favorite CI server. The command itself will return a consistent exit code telling
Expand Down
4 changes: 3 additions & 1 deletion src/Console/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function configure()
$this
->setName('lint')
->addArgument('paths', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'The path to scan for twig files.', ['.'])
->addOption('exclude', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Excluded folder of path.', [])
->addOption('severity', 's', InputOption::VALUE_REQUIRED, 'The maximum allowed error level.', 'warning')
->addOption('reporter', 'r', InputOption::VALUE_REQUIRED, 'The reporter to use.', 'console')
->addOption('ruleset', null, InputOption::VALUE_REQUIRED, 'Ruleset class to use', Official::class)
Expand All @@ -32,14 +33,15 @@ public function execute(InputInterface $input, OutputInterface $output)
$limit = $this->getSeverityLimit($input);

$paths = $input->getArgument('paths');
$exclude = $input->getOption('exclude');

$files = [];
foreach ($paths as $path) {
if (is_file($path)) {
$files[] = new \SplFileInfo($path);
} else {
$finder = new Finder();
$found = iterator_to_array($finder->in($path)->name('*.twig'));
$found = iterator_to_array($finder->in($path)->exclude($exclude)->name('*.twig'));
if (!empty($found)) {
$files = array_merge($files, $found);
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Console/LintCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace FriendsOfTwig\Twigcs\Tests\Console;

use FriendsOfTwig\Twigcs\Console\LintCommand;
use FriendsOfTwig\Twigcs\Container;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

class LintCommandTest extends TestCase
{
/** @var CommandTester */
private $commandTester;

public function setUp()
{
$container = new Container();
$command = new LintCommand();
$command->setContainer($container);

$this->commandTester = new CommandTester($command);
}

public function testExecute()
{
$this->commandTester->execute([
'paths' => ['tests/data/good'],
]);

$output = $this->commandTester->getDisplay();
$statusCode = $this->commandTester->getStatusCode();
$this->assertSame($statusCode, 0);
$this->assertContains('No violation found.', $output);
}

public function testExecuteWithError()
{
$this->commandTester->execute([
'paths' => ['tests/data'],
]);

$output = $this->commandTester->getDisplay();
$statusCode = $this->commandTester->getStatusCode();
$this->assertSame($statusCode, 1);
$this->assertContains('ERROR', $output);
}

public function testExecuteWithExclude()
{
$this->commandTester->execute([
'paths' => ['tests/data'],
'--exclude' => ['bad'],
]);

$output = $this->commandTester->getDisplay();
$statusCode = $this->commandTester->getStatusCode();
$this->assertSame($statusCode, 0);
$this->assertContains('No violation found.', $output);
}
}
1 change: 1 addition & 0 deletions tests/data/bad/bad.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{dump('twig' ) }}
1 change: 1 addition & 0 deletions tests/data/good/good.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ dump('twig') }}

0 comments on commit 46abeb8

Please sign in to comment.