Skip to content

Commit

Permalink
Merge pull request #24 from ivansaul/fix/input-validation
Browse files Browse the repository at this point in the history
fix: input validation and error handling
  • Loading branch information
ivansaul authored Oct 10, 2024
2 parents dbba2f9 + 463c120 commit 1479b22
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
INPUT: str = str(TEST_VIDEOS_PATH / "sample_video_1.mp4")
INPUT_2: str = str(TEST_VIDEOS_PATH / "sample_video_2.mp4")
OUTPUT: str = str(TEST_VIDEOS_PATH / "output.mp4")
WRONG_INPUT: str = str(TEST_VIDEOS_PATH / "wrong_input.mp4")


@pytest.fixture(scope="function", autouse=True)
Expand Down Expand Up @@ -115,3 +116,17 @@ def test_single_file_compression_with_output_option():
result = runner.invoke(app, [INPUT, "--output", OUTPUT])
assert result.exit_code == 0, result.stdout
assert PROGRESS_COMPLETION_OUTPUT in result.stdout


def test_single_file_compression_with_wrong_path():
"""
Test single file compression with wrong path.
Expected results:
- Exit code: 0
- Error message: "INPUT path does not exists"
"""
result = runner.invoke(app, [WRONG_INPUT])
expected_message = "INPUT path does not exists"
assert result.exit_code == 0, result.stdout
assert expected_message in result.stdout
10 changes: 5 additions & 5 deletions src/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from vidpack.helpers import add_affixes, file_exists, is_ffmpeg_installed
from vidpack.helpers import add_affixes, is_ffmpeg_installed, path_exists


@pytest.mark.parametrize(
Expand Down Expand Up @@ -52,14 +52,14 @@ def mock_run(*args, **kwargs):


@patch("vidpack.helpers.Path.exists")
def test_file_exists(mock_exists):
def test_path_exists(mock_exists):
"""
Test file_exists function with different scenarios.
Test path_exists function with different scenarios.
"""
# Simulate that the file exists
mock_exists.return_value = True
assert file_exists("/path/to/existing_file.txt") is True
assert path_exists("/path/to/existing_file.txt") is True

# Simulate that the file does not exist
mock_exists.return_value = False
assert file_exists("/path/to/non_existent_file.txt") is False
assert path_exists("/path/to/non_existent_file.txt") is False
9 changes: 8 additions & 1 deletion src/vidpack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .constants import Constants
from .core import compress_video
from .helpers import delete_path, is_dir, is_ffmpeg_installed, is_file
from .helpers import delete_path, is_dir, is_ffmpeg_installed, is_file, path_exists
from .models import VideoCodec
from .utils import list_unprocessed_videos

Expand Down Expand Up @@ -90,6 +90,13 @@ def main(
print(Constants.FFMPEG_NOT_INSTALLED)
raise typer.Exit()

if not path_exists(input):
console.print(
Constants.INVALID_INPUT_PATH_ERROR_MESSAGE,
style="bold red",
)
raise typer.Exit()

if is_file(input):
try:
console.print(
Expand Down
1 change: 1 addition & 0 deletions src/vidpack/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ class Constants:
SUPPORTED_VIDEO_FORMATS = (".mp4", ".mov", ".avi", ".mkv", ".webm")
COMPRESSED_SUFFIX = "_compressed"
FFMPEG_FILE_ALREADY_EXISTS_ERROR_PATTERN = r".*File.*already exists.*"
INVALID_INPUT_PATH_ERROR_MESSAGE = "ERROR: INPUT path does not exists. Please provide a valid file or directory path."
10 changes: 5 additions & 5 deletions src/vidpack/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ def add_affixes(file: str, prefix: str = "", suffix: str = "") -> str:
return str(path.with_name(new_file_name))


def file_exists(file: str) -> bool:
def path_exists(path: str) -> bool:
"""
Check if a file exists.
Check if a path exists.
Args:
file (str): The path to the file.
path (str): The path to check.
Returns:
bool: True if the file exists, False otherwise.
bool: True if the path exists, False otherwise.
"""
return Path(file).exists()
return Path(path).exists()


def is_file(path: str) -> bool:
Expand Down

0 comments on commit 1479b22

Please sign in to comment.