From c8248233fba631b41a90a39933f7e3742ae2745d Mon Sep 17 00:00:00 2001 From: Blank Spruce <32396809+BlankSpruce@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:05:37 +0200 Subject: [PATCH] Also fail early for non-existent source paths --- README.md | 2 +- gersemi/__version__.py | 2 +- gersemi/configuration.py | 6 +++++- gersemi/runner.py | 15 ++++++++++++--- tests/test_executable.py | 30 ++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b5ad670..c61e359 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ You can use gersemi with a pre-commit hook by adding the following to `.pre-comm ```yaml repos: - repo: https://github.com/BlankSpruce/gersemi - rev: 0.15.0 + rev: 0.15.1 hooks: - id: gersemi ``` diff --git a/gersemi/__version__.py b/gersemi/__version__.py index b854693..c62cb9a 100644 --- a/gersemi/__version__.py +++ b/gersemi/__version__.py @@ -4,4 +4,4 @@ __license__ = "MPL 2.0" __title__ = "gersemi" __url__ = "https://github.com/BlankSpruce/gersemi" -__version__ = "0.15.0" +__version__ = "0.15.1" diff --git a/gersemi/configuration.py b/gersemi/configuration.py index 01dbca1..2bdeafa 100644 --- a/gersemi/configuration.py +++ b/gersemi/configuration.py @@ -233,7 +233,11 @@ def enter_directory(target_directory): def normalize_definitions(definitions): - return [Path(d).resolve(True) for d in definitions] + try: + return [Path(d).resolve(True) for d in definitions] + except FileNotFoundError as e: + # pylint: disable=broad-exception-raised + raise Exception(f"Definition path doesn't exist: {e.filename}") from e def sanitize_list_expansion(list_expansion): diff --git a/gersemi/runner.py b/gersemi/runner.py index edab54d..0d9c621 100644 --- a/gersemi/runner.py +++ b/gersemi/runner.py @@ -42,7 +42,7 @@ def get_files_from_single_path(path): return [path] return set( - item.resolve() if item != Path("-") else item + item.resolve(True) if item != Path("-") else item for path in paths for item in get_files_from_single_path(path) ) @@ -86,7 +86,12 @@ def find_all_custom_command_definitions( ) -> Dict[str, Keywords]: result: Dict = {} - files = get_files(paths) + try: + files = get_files(paths) + except FileNotFoundError as e: + # pylint: disable=broad-exception-raised + raise Exception(f"Definition path doesn't exist: {e.filename}") from e + find = find_custom_command_definitions_in_file for defs in pool.imap_unordered(find, files, chunksize=CHUNKSIZE): @@ -246,7 +251,11 @@ def handle_files_to_format( def run(mode: Mode, configuration: Configuration, sources: Iterable[Path]): - requested_files = get_files(sources) + try: + requested_files = get_files(sources) + except FileNotFoundError as e: + # pylint: disable=broad-exception-raised + raise Exception(f"Source path doesn't exist: {e.filename}") from e pool_cm = create_pool(Path("-") in requested_files, configuration.workers) with create_cache(configuration.cache) as cache, pool_cm() as pool: diff --git a/tests/test_executable.py b/tests/test_executable.py index 967da53..a12c305 100644 --- a/tests/test_executable.py +++ b/tests/test_executable.py @@ -1174,3 +1174,33 @@ def test_definition_path_doesnt_exist(): definitions / "back_to_the_future_four.cmake", ] assert_fail(*common_args, *definitions_as_files_with_path_that_doesnt_exist) + + +def test_source_path_doesnt_exist(): + with temporary_dir_copy(TESTS / "custom_project") as project: + sources = Path(project) / "formatted" + definitions = Path(project) / "formatted" + common_args = ["--definitions", definitions, "--check"] + + assert_success(*common_args, sources) + + assert_fail(*common_args, Path(project) / "this_path_doesnt_exist") + + assert_success(*common_args, sources / ".." / "formatted") + + assert_fail(*common_args, definitions / ".." / ".." / "formatted") + + sources_as_files = [ + sources / "CMakeLists.txt", + sources / "subdirectory_one" / "CMakeLists.txt", + sources / "subdirectory_two" / "CMakeLists.txt", + ] + assert_success(*common_args, *sources_as_files) + + sources_as_files_with_path_that_doesnt_exist = [ + sources / "CMakeLists.txt", + sources / "this_subdirectory_doesnt_exist" / "CMakeLists.txt", + sources / "subdirectory_one" / "CMakeLists.txt", + sources / "subdirectory_two" / "CMakeLists.txt", + ] + assert_fail(*common_args, *sources_as_files_with_path_that_doesnt_exist)