Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add excluded directories #81

Merged
merged 2 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') }}