From 0625e69912f8b07fc0540429b0bfb8bd7b19ed07 Mon Sep 17 00:00:00 2001 From: "Commentator2.0" Date: Tue, 3 Jun 2025 10:13:15 +0200 Subject: [PATCH] tests/functional2: fix bad error message when merging files The error message used to only contain the last key of the merge failure this commit changes the message to contain the full path to the merge conflict, resolving ambiguity Change-Id: I9848a559b1b888e50a548eef8609bf34506040de --- .../testlib/fixtures/file_helper.py | 8 +++- .../testlib/fixtures/test_file_helper.py | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/functional2/testlib/fixtures/file_helper.py b/tests/functional2/testlib/fixtures/file_helper.py index cbab611a5..037ebaae8 100644 --- a/tests/functional2/testlib/fixtures/file_helper.py +++ b/tests/functional2/testlib/fixtures/file_helper.py @@ -146,9 +146,13 @@ def merge_file_declaration(a: FileDeclaration, b: FileDeclaration) -> FileDeclar result[key] = a.get(key) or b.get(key) continue if isinstance(a[key], Fileish) or isinstance(b[key], Fileish): - msg = "Cannot merge files; got two different values for the same path %s" + msg = "Cannot merge files; got two different values for the same path" raise ValueError(msg, key) - result[key] = merge_file_declaration(a[key], b[key]) + try: + result[key] = merge_file_declaration(a[key], b[key]) + except ValueError as e: + msg, path = e.args + raise ValueError(msg, f"{key}/{path}") return result diff --git a/tests/functional2/testlib/fixtures/test_file_helper.py b/tests/functional2/testlib/fixtures/test_file_helper.py index 565f2b16b..f25f4929f 100644 --- a/tests/functional2/testlib/fixtures/test_file_helper.py +++ b/tests/functional2/testlib/fixtures/test_file_helper.py @@ -9,6 +9,7 @@ from functional2.testlib.fixtures.file_helper import ( File, Symlink, AssetSymlink, + merge_file_declaration, ) @@ -249,3 +250,39 @@ def test_file_symlink(files: Path): assert link.is_symlink() # check that this is actually relative and not an absolute path assert str(link.readlink()) == "../tg" + + +def test_merge_fd_merges_correctly(): + fa = File("a") + fc = File("c") + fd = File("d") + ff = File("f") + fd1 = {"a": fa, "b": {"c": fc}} + + fd2 = {"b": {"d": fd}, "e": {"f": ff}} + + expected = {"a": fa, "b": {"c": fc, "d": fd}, "e": {"f": ff}} + + result = merge_file_declaration(fd1, fd2) + assert result == expected + + +def test_merge_fd_throws_on_conflict(): + fd1 = {"a": {"b": File("b")}} + fd2 = {"a": File("a")} + + with pytest.raises( + ValueError, match="('Cannot merge files; got two different values for the same path', 'a')" + ): + merge_file_declaration(fd1, fd2) + + +def test_merge_fd_throws_on_conflict_with_full_path(): + fd1 = {"a": {"b": {"c": {"d": File("d")}}}} + fd2 = {"a": {"b": {"c": {"d": File("different file")}}}} + + with pytest.raises( + ValueError, + match="('Cannot merge files; got two different values for the same path', 'a/b/c/d')", + ): + merge_file_declaration(fd1, fd2)