mirror of
https://github.com/lllyasviel/Fooocus.git
synced 2026-08-16 13:13:16 +02:00
feat: parse env var strings to expected config value types (#3107)
* fix: add try_parse_bool for env var strings to enable config overrides of boolean values * fix: fallback to given value if not parseable * feat: extend eval to all valid types * fix: remove return type * fix: prevent strange type conversions by providing expected type * feat: add tests
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import numbers
|
||||
import os
|
||||
import unittest
|
||||
|
||||
import modules.flags
|
||||
from modules import extra_utils
|
||||
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
def test_try_eval_env_var(self):
|
||||
test_cases = [
|
||||
{
|
||||
"input": ("foo", str),
|
||||
"output": "foo"
|
||||
},
|
||||
{
|
||||
"input": ("1", int),
|
||||
"output": 1
|
||||
},
|
||||
{
|
||||
"input": ("1.0", float),
|
||||
"output": 1.0
|
||||
},
|
||||
{
|
||||
"input": ("1", numbers.Number),
|
||||
"output": 1
|
||||
},
|
||||
{
|
||||
"input": ("1.0", numbers.Number),
|
||||
"output": 1.0
|
||||
},
|
||||
{
|
||||
"input": ("true", bool),
|
||||
"output": True
|
||||
},
|
||||
{
|
||||
"input": ("True", bool),
|
||||
"output": True
|
||||
},
|
||||
{
|
||||
"input": ("false", bool),
|
||||
"output": False
|
||||
},
|
||||
{
|
||||
"input": ("False", bool),
|
||||
"output": False
|
||||
},
|
||||
{
|
||||
"input": ("True", str),
|
||||
"output": "True"
|
||||
},
|
||||
{
|
||||
"input": ("False", str),
|
||||
"output": "False"
|
||||
},
|
||||
{
|
||||
"input": ("['a', 'b', 'c']", list),
|
||||
"output": ['a', 'b', 'c']
|
||||
},
|
||||
{
|
||||
"input": ("{'a':1}", dict),
|
||||
"output": {'a': 1}
|
||||
},
|
||||
{
|
||||
"input": ("('foo', 1)", tuple),
|
||||
"output": ('foo', 1)
|
||||
}
|
||||
]
|
||||
|
||||
for test in test_cases:
|
||||
value, expected_type = test["input"]
|
||||
expected = test["output"]
|
||||
actual = extra_utils.try_eval_env_var(value, expected_type)
|
||||
self.assertEqual(expected, actual)
|
||||
Reference in New Issue
Block a user