util/path: add join_abs() to join potentially absolute paths

It turned out that in many cases, stages need to join two absolute
paths, the pipeline tree path and the path on a booted system. However,
the standard `os.path.join()` function can't handle such situation as
just prepending the root to the subsequent paths.

Add a new helper function, which is able to join any paths together,
regardless if any of them is absolute or not. If the root is not
absolute, the result will be made absolute to the filesystem root `/`.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2024-12-19 14:14:12 +01:00 committed by Brian C. Lane
parent 67d9663c83
commit 8463394d2c
2 changed files with 54 additions and 1 deletions

View file

@ -68,3 +68,41 @@ def test_in_tree():
for args, expected in cases.items():
assert path.in_tree(*args) == expected, args
@pytest.mark.parametrize("test_case", (
{
"args": ("",),
"expected": "/"
},
{
"args": ("", "file"),
"expected": "/file"
},
{
"args": ("", "/file"),
"expected": "/file"
},
{
"args": ("/tmp", "/file"),
"expected": "/tmp/file"
},
{
"args": ("/tmp", "/foo", "/bar"),
"expected": "/tmp/foo/bar"
},
{
"args": ("/", "/foo"),
"expected": "/foo"
},
{
"args": ("/", "/foo", "/bar"),
"expected": "/foo/bar"
},
{
"args": (Path("/tmp"), "/foo", "/bar"),
"expected": "/tmp/foo/bar"
},
))
def test_join_abs(test_case):
assert path.join_abs(*test_case["args"]) == test_case["expected"]