Skip to content

Commit ccdf8d6

Browse files
committed
Update isOnExcludeList defaults to root dir
1 parent bfcaa63 commit ccdf8d6

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

src/Commands/BaseCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ protected function isExcluded(string $filepath): bool
9494
return false;
9595
}
9696

97-
protected function isBlacklisted($filepath)
97+
protected function isOnExcludeList($filepath)
9898
{
9999
$DS = DIRECTORY_SEPARATOR;
100+
$root = realpath($this->cwd ?? getcwd());
100101

101-
return strpos($filepath, 'vendor') !== false
102-
|| strpos($filepath, 'node_modules') !== false
102+
return strpos($filepath, "{$root}{$DS}vendor{$DS}") === 0
103+
|| strpos($filepath, "{$root}{$DS}node_modules{$DS}") === 0
104+
|| strpos($filepath, "{$root}{$DS}bootstrap{$DS}") === 0
103105
|| strpos($filepath, "storage{$DS}framework{$DS}views") !== false
104-
|| strpos($filepath, 'bootstrap') !== false
105106
|| $this->isExcluded($filepath);
106107
}
107108
}

src/Commands/FormatCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4949
$fileOrDirectory = $this->resolveFileOrDirectory($input->getArgument('file or directory'));
5050
$finalResponseCode = self::SUCCESS;
5151

52-
if ($this->isBlacklisted($fileOrDirectory)) {
52+
if ($this->isOnExcludeList($fileOrDirectory)) {
5353
return self::SUCCESS;
5454
}
5555

@@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8484

8585
private function formatFile(InputInterface $input, OutputInterface $output, $file)
8686
{
87-
if ($this->isBlacklisted($file)) {
87+
if ($this->isOnExcludeList($file)) {
8888
return self::SUCCESS;
8989
}
9090

src/Commands/LintCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5858
$fileOrDirectory = $this->resolveFileOrDirectory($input->getArgument('file or directory'));
5959
$finalResponseCode = self::NO_LINTS_FOUND_OR_SUCCESS;
6060

61-
if ($this->isBlacklisted($fileOrDirectory)) {
61+
if ($this->isOnExcludeList($fileOrDirectory)) {
6262
return self::NO_LINTS_FOUND_OR_SUCCESS;
6363
}
6464

@@ -109,7 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109109

110110
private function lintFile(InputInterface $input, OutputInterface $output, $file)
111111
{
112-
if ($this->isBlacklisted($file)) {
112+
if ($this->isOnExcludeList($file)) {
113113
return self::NO_LINTS_FOUND_OR_SUCCESS;
114114
}
115115

tests/ExcludedPathTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ReflectionMethod;
7+
use Tighten\TLint\Commands\LintCommand;
8+
9+
class ExcludedPathTest extends TestCase
10+
{
11+
private string $root;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->root = realpath(sys_get_temp_dir());
18+
}
19+
20+
private function isOnExcludeList(string $filepath): bool
21+
{
22+
$command = new LintCommand(sys_get_temp_dir());
23+
24+
$method = new ReflectionMethod($command, 'isOnExcludeList');
25+
26+
return $method->invoke($command, $filepath);
27+
}
28+
29+
/** @test */
30+
public function it_excludes_root_vendor_directory()
31+
{
32+
$DS = DIRECTORY_SEPARATOR;
33+
34+
$this->assertTrue($this->isOnExcludeList("{$this->root}{$DS}vendor{$DS}league{$DS}commonmark{$DS}src{$DS}File.php"));
35+
}
36+
37+
/** @test */
38+
public function it_allows_vendor_views_directory()
39+
{
40+
$DS = DIRECTORY_SEPARATOR;
41+
42+
$this->assertFalse($this->isOnExcludeList("{$this->root}{$DS}resources{$DS}views{$DS}vendor{$DS}mail{$DS}html{$DS}header.blade.php"));
43+
}
44+
45+
/** @test */
46+
public function it_does_not_exclude_files_containing_vendor_in_name()
47+
{
48+
$DS = DIRECTORY_SEPARATOR;
49+
50+
$this->assertFalse($this->isOnExcludeList("{$this->root}{$DS}app{$DS}Http{$DS}Controllers{$DS}VendorController.php"));
51+
}
52+
53+
/** @test */
54+
public function it_excludes_root_node_modules_directory()
55+
{
56+
$DS = DIRECTORY_SEPARATOR;
57+
58+
$this->assertTrue($this->isOnExcludeList("{$this->root}{$DS}node_modules{$DS}lodash{$DS}index.js"));
59+
}
60+
61+
/** @test */
62+
public function it_does_not_exclude_files_containing_node_modules_in_name()
63+
{
64+
$DS = DIRECTORY_SEPARATOR;
65+
66+
$this->assertFalse($this->isOnExcludeList("{$this->root}{$DS}app{$DS}Http{$DS}Controllers{$DS}NodeModulesController.php"));
67+
}
68+
69+
/** @test */
70+
public function it_excludes_root_bootstrap_directory()
71+
{
72+
$DS = DIRECTORY_SEPARATOR;
73+
74+
$this->assertTrue($this->isOnExcludeList("{$this->root}{$DS}bootstrap{$DS}app.php"));
75+
}
76+
77+
/** @test */
78+
public function it_does_not_exclude_files_containing_bootstrap_in_name()
79+
{
80+
$DS = DIRECTORY_SEPARATOR;
81+
82+
$this->assertFalse($this->isOnExcludeList("{$this->root}{$DS}app{$DS}Http{$DS}Controllers{$DS}BootstrapController.php"));
83+
$this->assertFalse($this->isOnExcludeList("{$this->root}{$DS}resources{$DS}views{$DS}bootstrap-layout.blade.php"));
84+
}
85+
86+
/** @test */
87+
public function it_excludes_storage_framework_views_directory()
88+
{
89+
$DS = DIRECTORY_SEPARATOR;
90+
91+
$this->assertTrue($this->isOnExcludeList("{$this->root}{$DS}storage{$DS}framework{$DS}views{$DS}cached.php"));
92+
}
93+
}

tlint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"excluded": ["tests/fixtures"]
3+
}

0 commit comments

Comments
 (0)