diff --git a/src/GitlabApiClient.php b/src/GitlabApiClient.php index e748cfb..a3c6c4d 100644 --- a/src/GitlabApiClient.php +++ b/src/GitlabApiClient.php @@ -57,10 +57,6 @@ public function lint(): array $yamlEncoder = new YamlEncoder(); $jsonEncoder = new JsonEncoder(); - if (! file_exists($this->configFile)) { - throw GitlabLinterException::fileNotFound($this->configFile); - } - $fileContents = (string) file_get_contents($this->configFile); $jsonData = $jsonEncoder->encode( diff --git a/src/GitlabLintTask.php b/src/GitlabLintTask.php index 87b110b..4e4eeb2 100644 --- a/src/GitlabLintTask.php +++ b/src/GitlabLintTask.php @@ -49,6 +49,15 @@ public function run(ContextInterface $context): TaskResultInterface { $config = $this->getConfig()->getOptions(); + if (! file_exists($config['gitlab_file'])) { + throw GitlabLinterException::fileNotFound($config['gitlab_file']); + } + + $files = $context->getFiles()->name(basename($config['gitlab_file'])); + if (count($files) === 0) { + return TaskResult::createSkipped($this, $context); + } + $apiClient = new GitlabApiClient($config); /** @var array{valid: bool, errors: array} $response */ diff --git a/tests/GitlabLintTaskTest.php b/tests/GitlabLintTaskTest.php index 54609f1..a2a85b6 100644 --- a/tests/GitlabLintTaskTest.php +++ b/tests/GitlabLintTaskTest.php @@ -22,6 +22,7 @@ use Headsnet\GrumPHP\GitlabLint\GitlabLinterException; use Headsnet\GrumPHP\GitlabLint\GitlabLintTask; use PHPUnit\Framework\TestCase; +use SplFileInfo; class GitlabLintTaskTest extends TestCase { @@ -96,12 +97,20 @@ public function gitlabFileDataProvider(): array */ private function buildTask(string $file, string $apiToken): array { + $gitlabFile = sprintf('%s/%s', __DIR__, $file); $processBuilder = $this->createMock(ProcessBuilder::class); $rawProcessFormatter = $this->createMock(RawProcessFormatter::class); - $context = new class() implements ContextInterface { + $context = new class($gitlabFile) implements ContextInterface { + private string $gitlabFile; + + public function __construct(string $gitlabFile) + { + $this->gitlabFile = $gitlabFile; + } + public function getFiles(): FilesCollection { - return new FilesCollection(); + return new FilesCollection([new SplFileInfo($this->gitlabFile)]); } }; @@ -115,7 +124,7 @@ public function getFiles(): FilesCollection 'gitlab_lint', [ 'api_token' => $apiToken, - 'gitlab_file' => sprintf('%s/%s', __DIR__, $file), + 'gitlab_file' => $gitlabFile, 'gitlab_url' => 'gitlab.com', ], new Metadata([])