From 463c1208fac6cd0b9483a8e050c0b061e902da2c Mon Sep 17 00:00:00 2001 From: ivansaul Date: Thu, 10 Oct 2024 03:27:46 -0500 Subject: [PATCH] fix: input validation and error handling Adds validation to ensure the input file or directory exists before processing. Also, provides a more informative error message when the input path is invalid. --- src/tests/test_cli.py | 15 +++++++++++++++ src/tests/test_helpers.py | 10 +++++----- src/vidpack/cli.py | 9 ++++++++- src/vidpack/constants.py | 1 + src/vidpack/helpers.py | 10 +++++----- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/tests/test_cli.py b/src/tests/test_cli.py index 69dd2de..857a777 100644 --- a/src/tests/test_cli.py +++ b/src/tests/test_cli.py @@ -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) @@ -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 diff --git a/src/tests/test_helpers.py b/src/tests/test_helpers.py index 50b4678..d706151 100644 --- a/src/tests/test_helpers.py +++ b/src/tests/test_helpers.py @@ -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( @@ -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 diff --git a/src/vidpack/cli.py b/src/vidpack/cli.py index 988004a..d1514a2 100644 --- a/src/vidpack/cli.py +++ b/src/vidpack/cli.py @@ -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 @@ -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( diff --git a/src/vidpack/constants.py b/src/vidpack/constants.py index 4806d61..1e8b281 100644 --- a/src/vidpack/constants.py +++ b/src/vidpack/constants.py @@ -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." diff --git a/src/vidpack/helpers.py b/src/vidpack/helpers.py index 3ac799d..ec55aca 100644 --- a/src/vidpack/helpers.py +++ b/src/vidpack/helpers.py @@ -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: