From 6599e9af972fd123bbd58a1078e470662679b5a4 Mon Sep 17 00:00:00 2001 From: olivier Date: Mon, 7 Oct 2019 21:43:44 +0200 Subject: [PATCH 1/2] add excluded directories --- README.md | 8 ++++++++ src/Console/LintCommand.php | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 79bf1cb..2c2bb4a 100644 --- a/README.md +++ b/README.md @@ -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 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 diff --git a/src/Console/LintCommand.php b/src/Console/LintCommand.php index 740202f..6ea6964 100644 --- a/src/Console/LintCommand.php +++ b/src/Console/LintCommand.php @@ -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) @@ -32,6 +33,7 @@ public function execute(InputInterface $input, OutputInterface $output) $limit = $this->getSeverityLimit($input); $paths = $input->getArgument('paths'); + $exclude = $input->getOption('exclude'); $files = []; foreach ($paths as $path) { @@ -39,7 +41,7 @@ public function execute(InputInterface $input, OutputInterface $output) $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); } From 752263c3b1d803d57a1c585ef4f656e75ed0d47b Mon Sep 17 00:00:00 2001 From: Olivier ALLAIN Date: Tue, 8 Oct 2019 17:05:49 +0200 Subject: [PATCH 2/2] add tests --- README.md | 2 +- tests/Console/LintCommandTest.php | 60 +++++++++++++++++++++++++++++++ tests/data/bad/bad.html.twig | 1 + tests/data/good/good.html.twig | 1 + 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/Console/LintCommandTest.php create mode 100644 tests/data/bad/bad.html.twig create mode 100644 tests/data/good/good.html.twig diff --git a/README.md b/README.md index 2c2bb4a..5eb46b6 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ 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 subfolders of path like this : +You can also exclude relative subfolders of path like this : ```bash twigcs /path/to/views --exclude vendor diff --git a/tests/Console/LintCommandTest.php b/tests/Console/LintCommandTest.php new file mode 100644 index 0000000..4ab44ba --- /dev/null +++ b/tests/Console/LintCommandTest.php @@ -0,0 +1,60 @@ +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); + } +} diff --git a/tests/data/bad/bad.html.twig b/tests/data/bad/bad.html.twig new file mode 100644 index 0000000..a80f37a --- /dev/null +++ b/tests/data/bad/bad.html.twig @@ -0,0 +1 @@ +{{dump('twig' ) }} diff --git a/tests/data/good/good.html.twig b/tests/data/good/good.html.twig new file mode 100644 index 0000000..9dbf19e --- /dev/null +++ b/tests/data/good/good.html.twig @@ -0,0 +1 @@ +{{ dump('twig') }}