Skip to content

Commit 5fcbc6d

Browse files
opajonkBlankSpruce
andcommitted
Fail early if definitions contain non-existent path
If the configuration file explicitly lists paths for CMake definitions, gersemi should fail early if one of them does not exist. Otherwise, problems (like typos) with these paths may be detected only later - especially when using gersemi as pre-commit hook. Co-Authored-By: Blank Spruce <32396809+BlankSpruce@users.noreply.github.com>
1 parent 331c7e8 commit 5fcbc6d

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

gersemi/__main__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,12 @@ def main():
224224
if len(args.sources) < 1:
225225
sys.exit(SUCCESS)
226226

227-
configuration = make_configuration(args)
228-
mode = get_mode(args)
227+
try:
228+
configuration = make_configuration(args)
229+
mode = get_mode(args)
230+
except Exception as exception: # pylint: disable=broad-except
231+
print_to_stderr(exception)
232+
sys.exit(FAIL)
229233

230234
sys.exit(run(mode, configuration, args.sources))
231235

gersemi/configuration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def enter_directory(target_directory):
233233

234234

235235
def normalize_definitions(definitions):
236-
return [Path(d).resolve() for d in definitions]
236+
return [Path(d).resolve(True) for d in definitions]
237237

238238

239239
def sanitize_list_expansion(list_expansion):

tests/test_executable.py

+28
Original file line numberDiff line numberDiff line change
@@ -1146,3 +1146,31 @@ def assert_that_cache_was_used(inspector):
11461146
gersemi_("--cache", "--check", target, "--definitions", target, cwd=target)
11471147

11481148
assert_that_cache_was_used(inspector)
1149+
1150+
1151+
def test_definition_path_doesnt_exist():
1152+
with temporary_dir_copy(TESTS / "custom_project") as project:
1153+
sources = Path(project) / "formatted"
1154+
definitions = Path(project) / "formatted"
1155+
common_args = ["--check", sources, "--definitions"]
1156+
1157+
assert_success(*common_args, definitions)
1158+
1159+
assert_fail(*common_args, Path(project) / "this_path_doesnt_exist")
1160+
1161+
assert_success(*common_args, definitions / ".." / "formatted")
1162+
1163+
assert_fail(*common_args, definitions / ".." / ".." / "formatted")
1164+
1165+
definitions_as_files = [
1166+
definitions / "back_to_the_future.cmake",
1167+
definitions / "back_to_the_future_sequels.cmake",
1168+
]
1169+
assert_success(*common_args, *definitions_as_files)
1170+
1171+
definitions_as_files_with_path_that_doesnt_exist = [
1172+
definitions / "back_to_the_future.cmake",
1173+
definitions / "back_to_the_future_sequels.cmake",
1174+
definitions / "back_to_the_future_four.cmake",
1175+
]
1176+
assert_fail(*common_args, *definitions_as_files_with_path_that_doesnt_exist)

0 commit comments

Comments
 (0)