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

Filesystem - add argument values into InvariantViolationException message #205

Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion docs/component/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
- [is_readable](./../../src/Psl/Filesystem/is_readable.php#L20)
- [is_symbolic_link](./../../src/Psl/Filesystem/is_symbolic_link.php#L19)
- [is_writable](./../../src/Psl/Filesystem/is_writable.php#L20)
- [read_directory](./../../src/Psl/Filesystem/read_directory.php#L19)
- [read_directory](./../../src/Psl/Filesystem/read_directory.php#L20)
- [read_file](./../../src/Psl/Filesystem/read_file.php#L24)
- [read_symbolic_link](./../../src/Psl/Filesystem/read_symbolic_link.php#L21)
- [write_file](./../../src/Psl/Filesystem/write_file.php#L18)
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Filesystem/Internal/write_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
function write_file(string $file, string $content, bool $append): void
{
if (Filesystem\exists($file)) {
Psl\invariant(Filesystem\is_file($file), '$file is not a file.');
Psl\invariant(Filesystem\is_writable($file), '$file is not writeable.');
Psl\invariant(Filesystem\is_file($file), 'File "%s" is not a file.', $file);
Psl\invariant(Filesystem\is_writable($file), 'File "%s" is not writeable.', $file);
} else {
Filesystem\create_file($file);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/change_group.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
function change_group(string $filename, int $group): void
{
Psl\invariant(exists($filename), '$filename does not exist.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

if (is_symbolic_link($filename)) {
$fun = static fn(): bool => lchgrp($filename, $group);
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/change_owner.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
function change_owner(string $filename, int $user): void
{
Psl\invariant(exists($filename), '$filename does not exist.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);
if (is_symbolic_link($filename)) {
$fun = static fn(): bool => lchown($filename, $user);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/change_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
function change_permissions(string $filename, int $permissions): void
{
Psl\invariant(exists($filename), '$filename does not exist.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$success, $error] = Internal\box(static fn(): bool => chmod($filename, $permissions));
// @codeCoverageIgnoreStart
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
function copy(string $source, string $destination, bool $overwrite = false): void
{
Psl\invariant(is_file($source) && is_readable($source), '$source does not exist or is not readable.');
Psl\invariant(is_file($source) && is_readable($source), 'Source "%s" does not exist or is not readable.', $source);

if (!$overwrite && is_file($destination)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Filesystem/create_hard_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
function create_hard_link(string $source, string $destination): void
{
Psl\invariant(exists($source), '$source file does not exist.');
Psl\invariant(is_file($source), '$source is not a file.');
Psl\invariant(exists($source), 'Source file "%s" does not exist.', $source);
Psl\invariant(is_file($source), 'Source "%s" is not a file.', $source);

$destination_directory = get_directory($destination);
if (!is_directory($destination_directory)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Filesystem/create_temporary_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
function create_temporary_file(?string $directory = null, ?string $prefix = null): string
{
if (null !== $directory) {
Psl\invariant(is_directory($directory), '$directory is not a directory.');
Psl\invariant(is_writable($directory), '$directory is not writable.');
Psl\invariant(is_directory($directory), 'Directory "%s" is not a directory.', $directory);
Psl\invariant(is_writable($directory), 'Directory "%s" is not writable.', $directory);
} else {
$directory = Env\temp_dir();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/delete_directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
function delete_directory(string $directory, bool $recursive = false): void
{
Psl\invariant(is_directory($directory), '$directory does not exists.');
Psl\invariant(is_directory($directory), 'Directory "%s" does not exist.', $directory);

if ($recursive && !is_symbolic_link($directory)) {
[$symbolic_nodes, $nodes] = Vec\partition(
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/delete_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
function delete_file(string $filename): void
{
Psl\invariant(is_file($filename), '$filename does not exists.');
Psl\invariant(is_file($filename), 'File "%s" does not exist.', $filename);

[$result, $error_message] = Internal\box(static fn() => unlink($filename));
// @codeCoverageIgnoreStart
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/file_size.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
function file_size(string $filename): int
{
Psl\invariant(is_file($filename) && is_readable($filename), '$filename does not exist.');
Psl\invariant(is_file($filename) && is_readable($filename), 'File "%s" does not exist.', $filename);

// @codeCoverageIgnoreStart
[$size, $message] = Internal\box(static fn() => filesize($filename));
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/get_access_time.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
function get_access_time(string $filename): int
{
Psl\invariant(exists($filename), '$filename does not exists.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$result, $message] = Psl\Internal\box(
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/get_change_time.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
function get_change_time(string $filename): int
{
Psl\invariant(exists($filename), '$filename does not exists.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$result, $message] = Psl\Internal\box(
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/get_group.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
function get_group(string $filename): int
{
Psl\invariant(exists($filename), '$filename does not exists.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$result, $message] = Psl\Internal\box(
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/get_inode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
function get_inode(string $filename): int
{
Psl\invariant(exists($filename), '$filename does not exists.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$result, $message] = Psl\Internal\box(
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/get_modification_time.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
function get_modification_time(string $filename): int
{
Psl\invariant(exists($filename), '$filename does not exists.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$result, $message] = Internal\box(
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/get_owner.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
function get_owner(string $filename): int
{
Psl\invariant(exists($filename), '$filename does not exists.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$result, $message] = Psl\Internal\box(
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Filesystem/get_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
function get_permissions(string $filename): int
{
Psl\invariant(exists($filename), '$filename does not exists.');
Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename);

[$result, $message] = Psl\Internal\box(
/**
Expand Down
6 changes: 3 additions & 3 deletions src/Psl/Filesystem/read_directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
function read_directory(string $directory): array
{
Psl\invariant(exists($directory), '$directory does not exists.');
Psl\invariant(is_directory($directory), '$directory is not a directory.');
Psl\invariant(is_readable($directory), '$directory is not readable.');
Psl\invariant(exists($directory), 'Directory "%s" does not exist.', $directory);
Psl\invariant(is_directory($directory), 'Directory "%s" is not a directory.', $directory);
Psl\invariant(is_readable($directory), 'Directory "%s" is not readable.', $directory);

/** @var list<string> */
return Vec\values(new FilesystemIterator(
Expand Down
6 changes: 3 additions & 3 deletions src/Psl/Filesystem/read_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
function read_file(string $file, int $offset = 0, ?int $length = null): string
{
Psl\invariant(exists($file), '$file does not exist.');
Psl\invariant(is_file($file), '$file is not a file.');
Psl\invariant(is_readable($file), '$file is not readable.');
Psl\invariant(exists($file), 'File "%s" does not exist.', $file);
Psl\invariant(is_file($file), 'File "%s" is not a file.', $file);
Psl\invariant(is_readable($file), 'File "%s" is not readable.', $file);

if (null === $length) {
[$content, $error] = Internal\box(
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Filesystem/read_symbolic_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
function read_symbolic_link(string $symbolic_link): string
{
Psl\invariant(exists($symbolic_link), '$symbolic_link does not exists.');
Psl\invariant(is_symbolic_link($symbolic_link), '$symbolic_link is not a symbolic link.');
Psl\invariant(exists($symbolic_link), 'Symbolic link "%s" does not exist.', $symbolic_link);
Psl\invariant(is_symbolic_link($symbolic_link), 'Symbolic link "%s" is not a symbolic link.', $symbolic_link);

[$result, $message] = Internal\box(
/**
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Filesystem/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testTemporaryFileIsCreateInTempDirectoryByDefault(): void
public function testTemporaryFileThrowsForNonExistingDirectory(): void
{
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('$directory is not a directory.');
$this->expectExceptionMessage('"' . __FILE__ . '" is not a directory.');

Filesystem\create_temporary_file(__FILE__);
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public function testWriteFile(): void
public function testWriteFileThrowsForDirectories(): void
{
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('$file is not a file.');
$this->expectExceptionMessage('File "' . $this->directory . '" is not a file.');

Filesystem\write_file($this->directory, 'hello');
}
Expand All @@ -110,7 +110,7 @@ public function testWriteFileThrowsForNonWritableFiles(): void
Filesystem\change_permissions($file, 0111);

$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('$file is not writeable.');
$this->expectExceptionMessage('File "' . $file . '" is not writeable.');

Filesystem\write_file($file, 'hello');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Filesystem/ReadDirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testReadDirectory(): void
public function testReadDirectoryThrowsIfDirectoryDoesNotExist(): void
{
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('$directory does not exists.');
$this->expectExceptionMessage('Directory "' . Env\temp_dir() . '/foo-bar-baz' . '" does not exist.');

Filesystem\read_directory(Env\temp_dir() . '/foo-bar-baz');
}
Expand All @@ -50,7 +50,7 @@ public function testReadDirectoryThrowsIfNotDirectory(): void
Filesystem\create_file($filename);

$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('$directory is not a directory.');
$this->expectExceptionMessage('Directory "' . $filename . '" is not a directory.');

Filesystem\read_directory($filename);
}
Expand All @@ -60,7 +60,7 @@ public function testReadDirectoryThrowsIfNotReadable(): void
Filesystem\change_permissions($this->directory, 0077);

$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('$directory is not readable.');
$this->expectExceptionMessage('Directory "' . $this->directory . '" is not readable.');

try {
Filesystem\read_directory($this->directory);
Expand Down