From 6a59e740e4ccb761f9d87c2c6f837fa748908a90 Mon Sep 17 00:00:00 2001 From: Nikita Dubrovskii Date: Mon, 28 Oct 2024 11:20:23 +0100 Subject: [PATCH] parsing: treat locations without scheme as belonging to 'tree://' --- osbuild/util/parsing.py | 6 +++++- stages/org.osbuild.mkdir | 9 +++------ test/mod/test_util_parsing.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/osbuild/util/parsing.py b/osbuild/util/parsing.py index f75ffd67..d6d16f22 100644 --- a/osbuild/util/parsing.py +++ b/osbuild/util/parsing.py @@ -77,11 +77,15 @@ def parse_location_into_parts(location: str, args: Dict) -> Tuple[str, str]: Parses the location URL to derive the corresponding root and url path. Parameters: - - location (str): The location URL to be parsed. + - location (str): The location URL to be parsed. If the URL has no scheme, + then 'tree://' is implied - args (Dict): A dictionary containing arguments including mounts and path information as passed by osbuild.api.arguments() """ + if "://" not in location: + location = f"tree://{location}" + url = urlparse(location) scheme = url.scheme diff --git a/stages/org.osbuild.mkdir b/stages/org.osbuild.mkdir index d2d11a7a..01f5f431 100755 --- a/stages/org.osbuild.mkdir +++ b/stages/org.osbuild.mkdir @@ -15,12 +15,9 @@ def main(args): parents = item.get("parents", False) exist_ok = item.get("exist_ok", False) - if "://" not in path: - if not path.startswith("/"): - print("WARNING: relative path used, this is discouraged!") - path = f"tree:///{path}" - else: - path = f"tree://{path}" + if "://" not in path and not path.startswith("/"): + print("WARNING: relative path used, this is discouraged!") + path = f"tree:///{path}" target = parsing.parse_location(path, args) if parents: diff --git a/test/mod/test_util_parsing.py b/test/mod/test_util_parsing.py index 4a9c513b..8284b4c1 100644 --- a/test/mod/test_util_parsing.py +++ b/test/mod/test_util_parsing.py @@ -90,3 +90,37 @@ def test_parse_location_inputs(): assert [root, path] == ["/run/osbuild/inputs/tree", "/"] path = parsing.parse_location(location, args) assert path == "/run/osbuild/inputs/tree/." + + +def test_parse_location_abspath(): + args = { + "tree": "/run/osbuild/tree", + } + location = "/path/to/file" + root, path = parsing.parse_location_into_parts(location, args) + assert [root, path] == ["/run/osbuild/tree", "/path/to/file"] + path = parsing.parse_location(location, args) + assert path == "/run/osbuild/tree/path/to/file" + + +def test_parse_location_badpath(): + args = { + "tree": "/run/osbuild/tree", + "paths": { + "mounts": "/run/osbuild/mounts", + }, + "mounts": { + "root": { + "path": "/run/osbuild/mounts/." + } + }, + } + location = "file" + with pytest.raises(ValueError) as ex: + parsing.parse_location_into_parts(location, args) + assert "url.path from location must start with '/'" in str(ex.value) + + location = "mount://disk/file" + with pytest.raises(ValueError) as ex: + parsing.parse_location_into_parts(location, args) + assert "Unknown mount" in str(ex.value)