Skip to content

Commit

Permalink
Also fail early for non-existent source paths
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Aug 19, 2024
1 parent 5fcbc6d commit c824823
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion gersemi/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
__license__ = "MPL 2.0"
__title__ = "gersemi"
__url__ = "https://github.com/BlankSpruce/gersemi"
__version__ = "0.15.0"
__version__ = "0.15.1"
6 changes: 5 additions & 1 deletion gersemi/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 12 additions & 3 deletions gersemi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c824823

Please sign in to comment.