test: Move make_fake_input_tree to testutil
This is useful for other stage tests, move it and add a test.
This commit is contained in:
parent
5416028f2d
commit
9eb9f7f7f2
3 changed files with 39 additions and 12 deletions
|
|
@ -1,6 +1,7 @@
|
|||
"""
|
||||
Test related utilities
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
|
||||
|
||||
|
|
@ -13,3 +14,20 @@ def assert_dict_has(v, keys, expected_value):
|
|||
assert key in v
|
||||
v = v[key]
|
||||
assert v == expected_value
|
||||
|
||||
|
||||
def make_fake_input_tree(tmpdir, fake_content: dict) -> str:
|
||||
"""Create a directory tree of files with content.
|
||||
|
||||
Call it with:
|
||||
{"filename": "content", "otherfile": "content"}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -7,20 +7,9 @@ from unittest import mock
|
|||
import pytest
|
||||
|
||||
import osbuild.meta
|
||||
from osbuild.testutil import has_executable
|
||||
from osbuild.testutil import has_executable, make_fake_input_tree
|
||||
from osbuild.testutil.imports import import_module_from_path
|
||||
|
||||
|
||||
def make_fake_input_tree(tmpdir, fake_content: dict) -> str:
|
||||
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
|
||||
|
||||
|
||||
TEST_INPUT = [
|
||||
({}, []),
|
||||
({"compression": {"method": "lz4"}}, ["-z", "lz4"]),
|
||||
|
|
|
|||
20
test/mod/test_testutil_fake_tree.py
Normal file
20
test/mod/test_testutil_fake_tree.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#
|
||||
# Tests for the 'osbuild.util.testutil' module.
|
||||
#
|
||||
import os.path
|
||||
|
||||
import pytest
|
||||
|
||||
from osbuild.testutil import make_fake_input_tree
|
||||
|
||||
|
||||
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",
|
||||
})
|
||||
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")).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")).read() == "Second content"
|
||||
Loading…
Add table
Add a link
Reference in a new issue