Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make conf.get with a check_type=bool raise exception if invalid value #16976

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def set_relative_base_folder(self, folder):
class Conf:
# Putting some default expressions to check that any value could be false
boolean_false_expressions = ("0", '"0"', "false", '"false"', "off")
boolean_true_expressions = ("1", '"1"', "true", '"true"', "on")

def __init__(self):
# It being ordered allows for Windows case-insensitive composition
Expand Down Expand Up @@ -321,8 +322,12 @@ def get(self, conf_name, default=None, check_type=None, choices=None):
raise ConanException(f"Unknown value '{v}' for '{conf_name}'")
# Some smart conversions
if check_type is bool and not isinstance(v, bool):
# Perhaps, user has introduced a "false", "0" or even "off"
return str(v).lower() not in Conf.boolean_false_expressions
if str(v).lower() in Conf.boolean_false_expressions:
return False
if str(v).lower() in Conf.boolean_true_expressions:
return True
raise ConanException(f"[conf] {conf_name} must be a boolean-like object "
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
f"(true/false, 1/0, on/off) and value '{v}' does not match it.")
elif check_type is str and not isinstance(v, str):
return str(v)
elif v is None: # value was unset
Expand Down
17 changes: 17 additions & 0 deletions test/unittests/model/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,15 @@ def test_conf_get_check_type_and_default():
zlib:user.company.check:static_str=off
user.company.list:newnames+=myname
core.download:parallel=True
user:bad_value_0=Fasle
user:bad_value_1=ture
user:bad_value_2=10
user:bad_value_3='00'
""")
c = ConfDefinition()
c.loads(text)
assert c.get("user.company.cpu:jobs", check_type=int) == 5
assert c.get("user.company.cpu:jobs", check_type=None) == 5
assert c.get("user.company.cpu:jobs", check_type=str) == "5" # smart conversion
with pytest.raises(ConanException) as exc_info:
c.get("user.company.cpu:jobs", check_type=list)
Expand All @@ -243,6 +248,18 @@ def test_conf_get_check_type_and_default():
c.get("core.download:parallel", check_type=int)
assert ("[conf] core.download:parallel must be a int-like object. "
"The value 'True' introduced is a bool object") in str(exc_info.value)
with pytest.raises(ConanException) as exc_info:
c.get("user:bad_value_0", check_type=bool)
assert ("[conf] user:bad_value_0 must be a boolean-like object (true/false, 1/0, on/off) and value 'Fasle' does not match it.") in str(exc_info.value)
with pytest.raises(ConanException) as exc_info:
c.get("user:bad_value_1", check_type=bool)
assert ("[conf] user:bad_value_1 must be a boolean-like object (true/false, 1/0, on/off) and value 'ture' does not match it.") in str(exc_info.value)
with pytest.raises(ConanException) as exc_info:
c.get("user:bad_value_2", check_type=bool)
assert ("[conf] user:bad_value_2 must be a boolean-like object (true/false, 1/0, on/off) and value '10' does not match it.") in str(exc_info.value)
with pytest.raises(ConanException) as exc_info:
c.get("user:bad_value_3", check_type=bool)
assert ("[conf] user:bad_value_3 must be a boolean-like object (true/false, 1/0, on/off) and value '\'00\'' does not match it.") in str(exc_info.value)


def test_conf_pop():
Expand Down