Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_config.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
import shutil | |||||
import pytest | import pytest | ||||
import shutil | |||||
import pkg_resources.extern.packaging.version | import pkg_resources.extern.packaging.version | ||||
import yaml | |||||
from swh.core import config | from swh.core import config | ||||
pytest_v = pkg_resources.get_distribution("pytest").parsed_version | pytest_v = pkg_resources.get_distribution("pytest").parsed_version | ||||
if pytest_v < pkg_resources.extern.packaging.version.parse("3.9"): | if pytest_v < pkg_resources.extern.packaging.version.parse("3.9"): | ||||
@pytest.fixture | @pytest.fixture | ||||
def tmp_path(request): | def tmp_path(request): | ||||
import tempfile | import tempfile | ||||
Show All 39 Lines | parsed_conffile = { | ||||
"ls": ["list", "of", "strings"], | "ls": ["list", "of", "strings"], | ||||
"li": [1, 2, 3, 4], | "li": [1, 2, 3, 4], | ||||
} | } | ||||
@pytest.fixture | @pytest.fixture | ||||
def swh_config(tmp_path): | def swh_config(tmp_path): | ||||
# create a temporary folder | # create a temporary folder | ||||
conffile = tmp_path / "config.ini" | conffile = tmp_path / "config.yml" | ||||
conf_contents = """[main] | conf_contents = """ | ||||
a = 1 | a: 1 | ||||
b = this is a string | b: this is a string | ||||
c = true | c: true | ||||
h = false | h: false | ||||
ls = list, of, strings | ls: list, of, strings | ||||
li = 1, 2, 3, 4 | li: 1, 2, 3, 4 | ||||
""" | """ | ||||
conffile.open("w").write(conf_contents) | conffile.open("w").write(conf_contents) | ||||
return conffile | return conffile | ||||
@pytest.fixture | @pytest.fixture | ||||
def swh_config_unreadable(swh_config): | def swh_config_unreadable(swh_config): | ||||
# Create an unreadable, proper configuration file | # Create an unreadable, proper configuration file | ||||
Show All 13 Lines | def swh_config_unreadable_dir(swh_config): | ||||
yield perms_broken_dir / swh_config.name | yield perms_broken_dir / swh_config.name | ||||
# Make the broken perms items readable again to be able to remove them | # Make the broken perms items readable again to be able to remove them | ||||
os.chmod(str(perms_broken_dir), 0o755) | os.chmod(str(perms_broken_dir), 0o755) | ||||
@pytest.fixture | @pytest.fixture | ||||
def swh_config_empty(tmp_path): | def swh_config_empty(tmp_path): | ||||
# create a temporary folder | # create a temporary folder | ||||
conffile = tmp_path / "config.ini" | conffile = tmp_path / "config.yml" | ||||
conffile.touch() | conffile.touch() | ||||
return conffile | return conffile | ||||
def test_read(swh_config): | def test_read(swh_config): | ||||
# when | # when | ||||
res = config.read(str(swh_config), default_conf) | res = config.read(str(swh_config), default_conf) | ||||
# then | # then | ||||
assert res == parsed_conffile | assert res == parsed_conffile | ||||
def test_read_no_default_conf(swh_config): | |||||
"""If no default config if provided to read, this should directly parse the config file | |||||
yaml | |||||
""" | |||||
config_path = str(swh_config) | |||||
actual_config = config.read(config_path) | |||||
with open(config_path) as f: | |||||
expected_config = yaml.safe_load(f) | |||||
assert actual_config == expected_config | |||||
def test_read_empty_file(): | def test_read_empty_file(): | ||||
# when | # when | ||||
res = config.read(None, default_conf) | res = config.read(None, default_conf) | ||||
# then | # then | ||||
assert res == parsed_default_conf | assert res == parsed_default_conf | ||||
def test_support_non_existing_conffile(tmp_path): | def test_support_non_existing_conffile(tmp_path): | ||||
# when | # when | ||||
res = config.read(str(tmp_path / "void.ini"), default_conf) | res = config.read(str(tmp_path / "void.yml"), default_conf) | ||||
# then | # then | ||||
assert res == parsed_default_conf | assert res == parsed_default_conf | ||||
def test_support_empty_conffile(swh_config_empty): | def test_support_empty_conffile(swh_config_empty): | ||||
# when | # when | ||||
res = config.read(str(swh_config_empty), default_conf) | res = config.read(str(swh_config_empty), default_conf) | ||||
Show All 16 Lines | def test_merge_default_configs(): | ||||
# when | # when | ||||
res = config.merge_default_configs(default_conf, other_default_conf) | res = config.merge_default_configs(default_conf, other_default_conf) | ||||
# then | # then | ||||
assert res == full_default_conf | assert res == full_default_conf | ||||
def test_priority_read_nonexist_conf(swh_config): | def test_priority_read_nonexist_conf(swh_config): | ||||
noexist = str(swh_config.parent / "void.ini") | noexist = str(swh_config.parent / "void.yml") | ||||
# when | # when | ||||
res = config.priority_read([noexist, str(swh_config)], default_conf) | res = config.priority_read([noexist, str(swh_config)], default_conf) | ||||
# then | # then | ||||
assert res == parsed_conffile | assert res == parsed_conffile | ||||
def test_priority_read_conf_nonexist_empty(swh_config): | def test_priority_read_conf_nonexist_empty(swh_config): | ||||
noexist = swh_config.parent / "void.ini" | noexist = swh_config.parent / "void.yml" | ||||
empty = swh_config.parent / "empty.ini" | empty = swh_config.parent / "empty.yml" | ||||
empty.touch() | empty.touch() | ||||
# when | # when | ||||
res = config.priority_read( | res = config.priority_read( | ||||
[str(p) for p in (swh_config, noexist, empty)], default_conf | [str(p) for p in (swh_config, noexist, empty)], default_conf | ||||
) | ) | ||||
# then | # then | ||||
assert res == parsed_conffile | assert res == parsed_conffile | ||||
def test_priority_read_empty_conf_nonexist(swh_config): | def test_priority_read_empty_conf_nonexist(swh_config): | ||||
noexist = swh_config.parent / "void.ini" | noexist = swh_config.parent / "void.yml" | ||||
empty = swh_config.parent / "empty.ini" | empty = swh_config.parent / "empty.yml" | ||||
empty.touch() | empty.touch() | ||||
# when | # when | ||||
res = config.priority_read( | res = config.priority_read( | ||||
[str(p) for p in (empty, swh_config, noexist)], default_conf | [str(p) for p in (empty, swh_config, noexist)], default_conf | ||||
) | ) | ||||
# then | # then | ||||
assert res == parsed_default_conf | assert res == parsed_default_conf | ||||
def test_swh_config_paths(): | def test_swh_config_paths(): | ||||
res = config.swh_config_paths("foo/bar.ini") | res = config.swh_config_paths("foo/bar.yml") | ||||
assert res == [ | assert res == [ | ||||
"~/.config/swh/foo/bar.ini", | "~/.config/swh/foo/bar.yml", | ||||
"~/.swh/foo/bar.ini", | "~/.swh/foo/bar.yml", | ||||
"/etc/softwareheritage/foo/bar.ini", | "/etc/softwareheritage/foo/bar.yml", | ||||
] | ] | ||||
def test_prepare_folder(tmp_path): | def test_prepare_folder(tmp_path): | ||||
# given | # given | ||||
conf = { | conf = { | ||||
"path1": str(tmp_path / "path1"), | "path1": str(tmp_path / "path1"), | ||||
"path2": str(tmp_path / "path2" / "depth1"), | "path2": str(tmp_path / "path2" / "depth1"), | ||||
▲ Show 20 Lines • Show All 105 Lines • Show Last 20 Lines |