mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
feat(aurweb-config): add unset action and simplify
Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
199622c53f
commit
0e938209af
3 changed files with 78 additions and 26 deletions
|
@ -56,8 +56,13 @@ def get_section(section):
|
||||||
return _get_parser()[section]
|
return _get_parser()[section]
|
||||||
|
|
||||||
|
|
||||||
def replace_key(section: str, option: str, value: Any) -> Any:
|
def unset_option(section: str, option: str) -> None:
|
||||||
|
_get_parser().remove_option(section, option)
|
||||||
|
|
||||||
|
|
||||||
|
def set_option(section: str, option: str, value: Any) -> None:
|
||||||
_get_parser().set(section, option, value)
|
_get_parser().set(section, option, value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def save() -> None:
|
def save() -> None:
|
||||||
|
|
|
@ -11,35 +11,43 @@ import sys
|
||||||
import aurweb.config
|
import aurweb.config
|
||||||
|
|
||||||
|
|
||||||
def action_set(args):
|
def do_action(func, *args, save: bool = True):
|
||||||
# If AUR_CONFIG_IMMUTABLE is defined, skip out on config setting.
|
# If AUR_CONFIG_IMMUTABLE is defined, skip out on config setting.
|
||||||
if os.environ.get("AUR_CONFIG_IMMUTABLE", 0):
|
if int(os.environ.get("AUR_CONFIG_IMMUTABLE", 0)):
|
||||||
return
|
|
||||||
|
|
||||||
if not args.value:
|
|
||||||
print("error: no value provided", file=sys.stderr)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
value = None
|
||||||
try:
|
try:
|
||||||
aurweb.config.replace_key(args.section, args.option, args.value)
|
value = func(*args)
|
||||||
|
if save:
|
||||||
aurweb.config.save()
|
aurweb.config.save()
|
||||||
except configparser.NoSectionError:
|
except configparser.NoSectionError:
|
||||||
print("error: no section found", file=sys.stderr)
|
print("error: no section found", file=sys.stderr)
|
||||||
|
except configparser.NoOptionError:
|
||||||
|
print("error: no option found", file=sys.stderr)
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def action_set(args):
|
||||||
|
if not args.value:
|
||||||
|
print("error: no value provided", file=sys.stderr)
|
||||||
|
return
|
||||||
|
do_action(aurweb.config.set_option, args.section, args.option, args.value)
|
||||||
|
|
||||||
|
|
||||||
|
def action_unset(args):
|
||||||
|
do_action(aurweb.config.unset_option, args.section, args.option)
|
||||||
|
|
||||||
|
|
||||||
def action_get(args):
|
def action_get(args):
|
||||||
try:
|
val = do_action(aurweb.config.get, args.section, args.option, save=False)
|
||||||
value = aurweb.config.get(args.section, args.option)
|
print(val)
|
||||||
print(value)
|
|
||||||
except (configparser.NoSectionError):
|
|
||||||
print("error: no section found", file=sys.stderr)
|
|
||||||
except (configparser.NoOptionError):
|
|
||||||
print("error: no option found", file=sys.stderr)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
fmt_cls = argparse.RawDescriptionHelpFormatter
|
fmt_cls = argparse.RawDescriptionHelpFormatter
|
||||||
actions = ["get", "set"]
|
actions = ["get", "set", "unset"]
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="aurweb configuration tool",
|
description="aurweb configuration tool",
|
||||||
formatter_class=lambda prog: fmt_cls(prog=prog, max_help_position=80))
|
formatter_class=lambda prog: fmt_cls(prog=prog, max_help_position=80))
|
||||||
|
|
|
@ -5,6 +5,8 @@ import re
|
||||||
|
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
|
import py
|
||||||
|
|
||||||
from aurweb import config
|
from aurweb import config
|
||||||
from aurweb.scripts.config import main
|
from aurweb.scripts.config import main
|
||||||
|
|
||||||
|
@ -77,32 +79,69 @@ def test_config_main_get_unknown_option(get: str):
|
||||||
def test_config_main_set(save: None):
|
def test_config_main_set(save: None):
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
def mock_replace_key(section: str, option: str, value: str) -> None:
|
def set_option(section: str, option: str, value: str) -> None:
|
||||||
nonlocal data
|
nonlocal data
|
||||||
data = value
|
data = value
|
||||||
|
|
||||||
args = ["aurweb-config", "set", "options", "salt_rounds", "666"]
|
args = ["aurweb-config", "set", "options", "salt_rounds", "666"]
|
||||||
with mock.patch("sys.argv", args):
|
with mock.patch("sys.argv", args):
|
||||||
with mock.patch("aurweb.config.replace_key",
|
with mock.patch("aurweb.config.set_option", side_effect=set_option):
|
||||||
side_effect=mock_replace_key):
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
expected = "666"
|
expected = "666"
|
||||||
assert data == expected
|
assert data == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_config_main_set_real(tmpdir: py.path.local):
|
||||||
|
"""
|
||||||
|
Test a real set_option path.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Copy AUR_CONFIG to {tmpdir}/aur.config.
|
||||||
|
aur_config = os.environ.get("AUR_CONFIG")
|
||||||
|
tmp_aur_config = os.path.join(str(tmpdir), "aur.config")
|
||||||
|
with open(aur_config) as f:
|
||||||
|
with open(tmp_aur_config, "w") as o:
|
||||||
|
o.write(f.read())
|
||||||
|
|
||||||
|
# Force reset the parser. This should NOT be done publicly.
|
||||||
|
config._parser = None
|
||||||
|
|
||||||
|
value = 666
|
||||||
|
args = ["aurweb-config", "set", "options", "fake-key", str(value)]
|
||||||
|
with mock.patch.dict("os.environ", {"AUR_CONFIG": tmp_aur_config}):
|
||||||
|
with mock.patch("sys.argv", args):
|
||||||
|
# Run aurweb.config.main().
|
||||||
|
main()
|
||||||
|
|
||||||
|
# Update the config; fake-key should be set.
|
||||||
|
config.rehash()
|
||||||
|
assert config.getint("options", "fake-key") == 666
|
||||||
|
|
||||||
|
# Restore config back to normal.
|
||||||
|
args = ["aurweb-config", "unset", "options", "fake-key"]
|
||||||
|
with mock.patch("sys.argv", args):
|
||||||
|
main()
|
||||||
|
|
||||||
|
# Return the config back to normal.
|
||||||
|
config.rehash()
|
||||||
|
|
||||||
|
# fake-key should no longer exist.
|
||||||
|
assert config.getint("options", "fake-key") is None
|
||||||
|
|
||||||
|
|
||||||
def test_config_main_set_immutable():
|
def test_config_main_set_immutable():
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
def mock_replace_key(section: str, option: str, value: str) -> None:
|
def mock_set_option(section: str, option: str, value: str) -> None:
|
||||||
nonlocal data
|
nonlocal data
|
||||||
data = value
|
data = value
|
||||||
|
|
||||||
args = ["aurweb-config", "set", "options", "salt_rounds", "666"]
|
args = ["aurweb-config", "set", "options", "salt_rounds", "666"]
|
||||||
with mock.patch.dict(os.environ, {"AUR_CONFIG_IMMUTABLE": "1"}):
|
with mock.patch.dict(os.environ, {"AUR_CONFIG_IMMUTABLE": "1"}):
|
||||||
with mock.patch("sys.argv", args):
|
with mock.patch("sys.argv", args):
|
||||||
with mock.patch("aurweb.config.replace_key",
|
with mock.patch("aurweb.config.set_option",
|
||||||
side_effect=mock_replace_key):
|
side_effect=mock_set_option):
|
||||||
main()
|
main()
|
||||||
|
|
||||||
expected = None
|
expected = None
|
||||||
|
@ -125,14 +164,14 @@ def test_config_main_set_invalid_value():
|
||||||
def test_config_main_set_unknown_section(save: None):
|
def test_config_main_set_unknown_section(save: None):
|
||||||
stderr = io.StringIO()
|
stderr = io.StringIO()
|
||||||
|
|
||||||
def mock_replace_key(section: str, option: str, value: str) -> None:
|
def mock_set_option(section: str, option: str, value: str) -> None:
|
||||||
raise configparser.NoSectionError(section=section)
|
raise configparser.NoSectionError(section=section)
|
||||||
|
|
||||||
args = ["aurweb-config", "set", "options", "salt_rounds", "666"]
|
args = ["aurweb-config", "set", "options", "salt_rounds", "666"]
|
||||||
with mock.patch("sys.argv", args):
|
with mock.patch("sys.argv", args):
|
||||||
with mock.patch("sys.stderr", stderr):
|
with mock.patch("sys.stderr", stderr):
|
||||||
with mock.patch("aurweb.config.replace_key",
|
with mock.patch("aurweb.config.set_option",
|
||||||
side_effect=mock_replace_key):
|
side_effect=mock_set_option):
|
||||||
main()
|
main()
|
||||||
|
|
||||||
assert stderr.getvalue().strip() == "error: no section found"
|
assert stderr.getvalue().strip() == "error: no section found"
|
||||||
|
|
Loading…
Add table
Reference in a new issue