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

Fix init with existing file. #987

Merged
merged 1 commit into from
Sep 23, 2022
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
3 changes: 2 additions & 1 deletion src/Supportive/File/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Qossmic\Deptrac\Supportive\File\Exception\FileNotWritableException;
use SplFileInfo;
use Symfony\Component\Filesystem\Filesystem;
use function is_writable;

class Dumper
{
Expand All @@ -30,7 +31,7 @@ public function dump(string $file): void
if ($filesystem->exists($target->getPathname())) {
throw FileAlreadyExistsException::alreadyExists($target);
}
if (!$target->isWritable()) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, a file that doesn't exist is never writable. Instead, we have to check the path (i.e. the directory containing the file), if it is writable, which is why this check never worked.

if (!is_writable($target->getPath())) {
throw FileNotWritableException::notWritable($target);
}

Expand Down
89 changes: 89 additions & 0 deletions tests/Supportive/File/DumperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Tests\Qossmic\Deptrac\Supportive\File;

use PHPUnit\Framework\TestCase;
use Qossmic\Deptrac\Supportive\File\Dumper;
use Qossmic\Deptrac\Supportive\File\Exception\FileAlreadyExistsException;
use Qossmic\Deptrac\Supportive\File\Exception\FileNotWritableException;
use SplFileInfo;
use function file_exists;
use function file_get_contents;
use function is_writable;
use function rtrim;
use function sprintf;
use function sys_get_temp_dir;
use function tempnam;
use function uniqid;
use function unlink;
use const DIRECTORY_SEPARATOR;

final class DumperTest extends TestCase
{
private string $sourceFile;
private Dumper $dumper;

protected function setUp(): void
{
parent::setUp();

$this->sourceFile = __DIR__.'/Fixtures/deptrac.yaml';
$this->dumper = new Dumper($this->sourceFile);
}

protected function tearDown(): void
{
parent::tearDown();

unset($this->dumper);
}

public function testFailsWhenFileAlreadyExists(): void
{
$targetFile = $this->sourceFile;

$this->expectException(FileAlreadyExistsException::class);

$this->dumper->dump($targetFile);
}

public function testFailsWhenFileIsNotWritable(): void
{
$tempDir = sprintf('%s/%s', rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR), uniqid());
if (!mkdir($tempDir) || !chmod($tempDir, 0444)) {
$this->markTestSkipped(sprintf('Skipping test. Could not create readonly temporary directory "%s". Please check permissions', $tempDir));
}
$tempFile = new SplFileInfo($tempDir.DIRECTORY_SEPARATOR.'deptrac.yaml');

$this->expectException(FileNotWritableException::class);

$this->dumper->dump($tempFile->getPathname());
}

public function testCopiesTemplateToNewFile(): void
{
$tempFilename = tempnam(sys_get_temp_dir(), 'deptrac');
if (false === $tempFilename) {
$this->markTestSkipped('Skipping test. Could not create temporary file. Please check permissions');
}
$tempFile = new SplFileInfo($tempFilename);
unlink($tempFile->getPathname());
$tempDir = $tempFile->getPath();
if (!is_writable($tempDir)) {
$this->markTestSkipped(sprintf('Skipping test. Can not write to temporary directory "%s". Please check your permissions.', $tempDir));
}
if ($tempFile->isFile()) {
$this->fail(sprintf('Temporary file "%s" already exists.', $tempFile->getPathname()));
}

$this->dumper->dump($tempFile->getPathname());

self::assertTrue(file_exists($tempFile->getPathname()));
self::assertSame(file_get_contents($this->sourceFile), file_get_contents($tempFile->getPathname()));
dbrumann marked this conversation as resolved.
Show resolved Hide resolved

@unlink($tempFile->getPathname());
@rmdir($tempDir);
}
}
24 changes: 24 additions & 0 deletions tests/Supportive/File/Fixtures/deptrac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
deptrac:
paths:
- ./src
exclude_files:
- '#.*test.*#'
layers:
- name: Controller
collectors:
- type: className
value: .*Controller.*
- name: Repository
collectors:
- type: className
value: .*Repository.*
- name: Service
collectors:
- type: className
value: .*Service.*
ruleset:
Controller:
- Service
Service:
- Repository
Repository: