stages,test: fix lint errors and add basic unit tests

Add very simple unit tests as a starting point for the new
parsing functions in `util/parsing.py`.
This commit is contained in:
Michael Vogt 2024-02-28 17:43:00 +01:00
parent 6d4d1962eb
commit 249107a028
6 changed files with 47 additions and 12 deletions

View file

@ -2,8 +2,7 @@
import os
import re
from typing import Union, Dict
from typing import Dict, Union
from urllib.parse import ParseResult, urlparse
@ -36,7 +35,7 @@ def parse_size(s: str) -> Union[int, str]:
raise TypeError(f"invalid size value: '{s}'")
def parse_mount(url: ParseResult, args: Dict):
def parse_mount(url: ParseResult, args: Dict) -> os.PathLike:
"""
Parses the mount URL to extract the root path.
@ -55,7 +54,7 @@ def parse_mount(url: ParseResult, args: Dict):
return root
def parse_input(url: ParseResult, args: Dict):
def parse_input(url: ParseResult, args: Dict) -> os.PathLike:
"""
Parses the input URL to extract the root path.
@ -71,7 +70,7 @@ def parse_input(url: ParseResult, args: Dict):
return root
def parse_location(location, args):
def parse_location(location: str, args: Dict) -> str:
"""
Parses the location URL to derive the corresponding file path.

View file

@ -1,5 +1,4 @@
#!/usr/bin/python3
import os
import subprocess
import sys
@ -38,6 +37,7 @@ SCHEMA_2 = r"""
}
"""
def main(args, options):
for path, cmdargs in options["items"].items():
immutable = cmdargs["immutable"]

View file

@ -1,5 +1,4 @@
#!/usr/bin/python3
import os
import subprocess
import sys

View file

@ -1,9 +1,8 @@
#!/usr/bin/python3
import sys
from urllib.parse import urlparse
import osbuild.api
from osbuild.util import bls
from osbuild.util import bls, parsing
def main(args, options):
@ -15,6 +14,6 @@ def main(args, options):
if __name__ == '__main__':
args = osbuild.api.arguments()
r = main(args, args["options"])
_args = osbuild.api.arguments()
r = main(_args, _args["options"])
sys.exit(r)

View file

@ -37,3 +37,41 @@ def test_parse_size():
else:
res = parsing.parse_size(s)
assert res == num, f"{s} parsed as {res} (wanted {num})"
def test_parse_location_mounts():
args = {
"paths": {
"mounts": "/run/osbuild/mounts",
},
"mounts": {
"root": {
"path": "/run/osbuild/mounts/.",
},
},
}
location = "mount://root/"
path = parsing.parse_location(location, args)
assert path == "/run/osbuild/mounts/."
def test_parse_location_tree():
args = {
"tree": "/run/osbuild/tree",
}
location = "tree:///disk.img"
path = parsing.parse_location(location, args)
assert path == "/run/osbuild/tree/disk.img"
def test_parse_location_inputs():
args = {
"inputs": {
"tree": {
"path": "/run/osbuild/inputs/tree",
},
},
}
location = "input://tree/"
path = parsing.parse_location(location, args)
assert path == "/run/osbuild/inputs/tree/."

View file

@ -28,7 +28,7 @@ setenv =
LINTABLES_EXCLUDES = "*.json,*.sh"
LINTABLES_EXCLUDES_RE = ".*\.json$,.*\.sh"
TYPEABLES = osbuild
TYPEABLES_STRICT = ./osbuild/main_cli.py
TYPEABLES_STRICT = ./osbuild/main_cli.py ./osbuild/util/parsing.py
passenv =
TEST_CATEGORY