From ad8fd2f532407cf2805c83a96af3de74bdaff045 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 15 Dec 2023 20:32:33 +0100 Subject: [PATCH] testutil: extrace new `make_fake_tree()` helper Extract a new helper `make_fake_tree()` that generalizes the existing helper `make_fake_input_tree()`. The later will always create the content under `{basedir}/tree` which is convinient for input tree based tests but too specialized when using it in different contexts. The existing `make_fake_input_tree()` is preserved unchanged and becomes just a tiny wrapper. --- osbuild/testutil/__init__.py | 13 ++++++++++--- test/mod/test_testutil_fake_tree.py | 28 +++++++++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/osbuild/testutil/__init__.py b/osbuild/testutil/__init__.py index e4ab1e8c..6bf60e70 100644 --- a/osbuild/testutil/__init__.py +++ b/osbuild/testutil/__init__.py @@ -16,7 +16,7 @@ def assert_dict_has(v, keys, expected_value): assert v == expected_value -def make_fake_input_tree(tmpdir, fake_content: dict) -> str: +def make_fake_tree(basedir, fake_content: dict): """Create a directory tree of files with content. Call it with: @@ -24,10 +24,17 @@ def make_fake_input_tree(tmpdir, fake_content: dict) -> str: filename paths will have their parents created as needed, under tmpdir. """ - basedir = os.path.join(tmpdir, "tree") for path, content in fake_content.items(): dirp, name = os.path.split(os.path.join(basedir, path.lstrip("/"))) os.makedirs(dirp, exist_ok=True) with open(os.path.join(dirp, name), "w", encoding="utf-8") as fp: fp.write(content) - return basedir + + +def make_fake_input_tree(tmpdir, fake_content: dict) -> str: + """ + Wrapper around make_fake_tree for "input trees" + """ + basedir = tmpdir / "tree" + make_fake_tree(basedir, fake_content) + return os.fspath(basedir) diff --git a/test/mod/test_testutil_fake_tree.py b/test/mod/test_testutil_fake_tree.py index 7db609ab..63f5b775 100644 --- a/test/mod/test_testutil_fake_tree.py +++ b/test/mod/test_testutil_fake_tree.py @@ -3,16 +3,30 @@ # import os.path -from osbuild.testutil import make_fake_input_tree +from osbuild.testutil import make_fake_input_tree, make_fake_tree + +TEST_INPUT_TREE = { + "/fake-file-one": "Some content", + "/second/fake-file-two": "Second content", +} -def test_make_fake_tree(tmp_path): # pylint: disable=unused-argument - fake_input_tree = make_fake_input_tree(tmp_path, { - "/fake-file-one": "Some content", - "/second/fake-file-two": "Second content", - }) +def validate_test_input_tree(fake_input_tree): assert os.path.isdir(fake_input_tree) assert os.path.exists(os.path.join(fake_input_tree, "fake-file-one")) assert open(os.path.join(fake_input_tree, "fake-file-one"), encoding="utf8").read() == "Some content" assert os.path.exists(os.path.join(fake_input_tree, "second/fake-file-two")) - assert open(os.path.join(fake_input_tree, "second/fake-file-two"), encoding="utf").read() == "Second content" + assert open(os.path.join(fake_input_tree, "second/fake-file-two"), encoding="utf8").read() == "Second content" + + +def test_make_fake_tree(tmp_path): + make_fake_tree(tmp_path, TEST_INPUT_TREE) + validate_test_input_tree(tmp_path) + + +def test_make_fake_input_tree(tmp_path): + # make_fake_input_tree is a convinience wrapper around make_fake_tree + # for the osbuild input trees + fake_input_tree = make_fake_input_tree(tmp_path, TEST_INPUT_TREE) + assert fake_input_tree.endswith("/tree") + validate_test_input_tree(fake_input_tree)