mirror of
https://github.com/HorlogeSkynet/archey4
synced 2024-12-28 16:00:13 +01:00
220 lines
7.6 KiB
Python
220 lines
7.6 KiB
Python
"""Test module for `archey.configuration`"""
|
|
|
|
import logging
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import Mock, patch
|
|
|
|
from archey.configuration import Configuration
|
|
|
|
|
|
# To avoid edge-case issues due to singleton, we automatically reset internal `_instances`.
|
|
# This is done at the class-level.
|
|
@patch.dict(
|
|
"archey.singleton.Singleton._instances",
|
|
clear=True,
|
|
)
|
|
class TestConfiguration(unittest.TestCase):
|
|
"""
|
|
Simple test cases to check the behavior of `Configuration` singleton utility class.
|
|
Values will be manually set in the tests below.
|
|
"""
|
|
|
|
@patch(
|
|
# `_load_configuration` method is mocked to "ignore" local system configurations.
|
|
"archey.configuration.Configuration._load_configuration",
|
|
Mock(),
|
|
)
|
|
def test_get(self):
|
|
"""Test the `get` binding method to configuration elements"""
|
|
configuration = Configuration()
|
|
with patch.dict(
|
|
configuration._config, # pylint: disable=protected-access
|
|
{
|
|
"ip_settings": {
|
|
"lan_ip_max_count": 2,
|
|
},
|
|
"temperature": {
|
|
"use_fahrenheit": False,
|
|
},
|
|
},
|
|
):
|
|
self.assertEqual(configuration.get("ip_settings")["lan_ip_max_count"], 2)
|
|
self.assertFalse(configuration.get("temperature")["use_fahrenheit"])
|
|
self.assertTrue(configuration.get("does_not_exist", True))
|
|
self.assertIsNone(configuration.get("does_not_exist_either"))
|
|
|
|
def test_load_configuration(self):
|
|
"""Test for configuration loading from file, and overriding flag"""
|
|
configuration = Configuration()
|
|
with patch.dict(
|
|
configuration._config, # pylint: disable=protected-access
|
|
{
|
|
"allow_overriding": True,
|
|
"suppress_warnings": False,
|
|
"colors_palette": {
|
|
"use_unicode": False,
|
|
},
|
|
"ip_settings": {
|
|
"lan_ip_max_count": 2,
|
|
},
|
|
"temperature": {
|
|
"use_fahrenheit": False,
|
|
},
|
|
},
|
|
clear=True,
|
|
), tempfile.TemporaryDirectory() as temp_dir:
|
|
# We create a fake temporary configuration file.
|
|
with open(
|
|
os.path.join(temp_dir, "config.json"), mode="w", encoding="UTF-8"
|
|
) as f_config:
|
|
f_config.write(
|
|
"""\
|
|
{
|
|
"allow_overriding": false,
|
|
"suppress_warnings": true,
|
|
"colors_palette": {
|
|
"use_unicode": false
|
|
},
|
|
"ip_settings": {
|
|
"lan_ip_max_count": 4
|
|
},
|
|
"temperature": {
|
|
"use_fahrenheit": true
|
|
}
|
|
}
|
|
"""
|
|
)
|
|
|
|
# Let's load it into our `Configuration` instance.
|
|
configuration._load_configuration(temp_dir) # pylint: disable=protected-access
|
|
|
|
# Let's check the result :S
|
|
self.assertDictEqual(
|
|
configuration._config, # pylint: disable=protected-access
|
|
{
|
|
"allow_overriding": False,
|
|
"suppress_warnings": True,
|
|
"colors_palette": {
|
|
"use_unicode": False,
|
|
},
|
|
"ip_settings": {
|
|
"lan_ip_max_count": 4,
|
|
},
|
|
"temperature": {
|
|
"use_fahrenheit": True,
|
|
},
|
|
},
|
|
)
|
|
# The logging level has changed due to the `suppress_warnings` option being set.
|
|
self.assertEqual(logging.getLogger().getEffectiveLevel(), logging.ERROR)
|
|
|
|
# Let's try to load the `config.json` file present in this project.
|
|
configuration._load_configuration("archey/") # pylint: disable=protected-access
|
|
|
|
# It should not happen as `allow_overriding` has been set to false.
|
|
# Thus, the configuration is supposed to be the same as before.
|
|
self.assertDictEqual(
|
|
configuration._config, # pylint: disable=protected-access
|
|
{
|
|
"allow_overriding": False,
|
|
"suppress_warnings": True,
|
|
"colors_palette": {
|
|
"use_unicode": False,
|
|
},
|
|
"ip_settings": {
|
|
"lan_ip_max_count": 4,
|
|
},
|
|
"temperature": {
|
|
"use_fahrenheit": True,
|
|
},
|
|
},
|
|
)
|
|
|
|
@patch(
|
|
# `_load_configuration` method is mocked to "ignore" local system configurations.
|
|
"archey.configuration.Configuration._load_configuration",
|
|
Mock(),
|
|
)
|
|
def test_validate_configuration(self):
|
|
"""Test configuration option values validation"""
|
|
configuration = Configuration()
|
|
|
|
# OK
|
|
with patch.dict(
|
|
configuration._config, # pylint: disable=protected-access
|
|
{
|
|
"entries_color": "0;1;2",
|
|
},
|
|
):
|
|
configuration._validate_configuration() # pylint: disable=protected-access
|
|
self.assertEqual(configuration.get("entries_color"), "0;1;2")
|
|
|
|
# KO
|
|
with patch.dict(
|
|
configuration._config, # pylint: disable=protected-access
|
|
{
|
|
"entries_color": True,
|
|
},
|
|
):
|
|
configuration._validate_configuration() # pylint: disable=protected-access
|
|
self.assertEqual(configuration.get("entries_color"), "")
|
|
|
|
# KO
|
|
with patch.dict(
|
|
configuration._config, # pylint: disable=protected-access
|
|
{
|
|
"entries_color": "true",
|
|
},
|
|
):
|
|
configuration._validate_configuration() # pylint: disable=protected-access
|
|
self.assertEqual(configuration.get("entries_color"), "")
|
|
|
|
def test_instantiation_config_path(self):
|
|
"""Test for configuration loading from specific user-defined path"""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
# We create a fake temporary configuration file.
|
|
config_file = os.path.join(temp_dir, "user.cfg") # A pure arbitrary name.
|
|
with open(config_file, mode="w", encoding="UTF-8") as f_config:
|
|
f_config.write(
|
|
"""\
|
|
{
|
|
"allow_overriding": false,
|
|
"suppress_warnings": true,
|
|
"colors_palette": {
|
|
"use_unicode": false
|
|
},
|
|
"ip_settings": {
|
|
"lan_ip_max_count": 4
|
|
},
|
|
"temperature": {
|
|
"use_fahrenheit": true
|
|
}
|
|
}
|
|
"""
|
|
)
|
|
|
|
configuration = Configuration(config_path=config_file)
|
|
|
|
# We can't use `assertDictEqual` here as the resulting `_config` internal object
|
|
# directly depends on the default one (which constantly evolves).
|
|
# We safely check that above entries have correctly been overridden.
|
|
self.assertFalse(configuration.get("allow_overriding"))
|
|
self.assertTrue(configuration.get("suppress_warnings"))
|
|
self.assertFalse(configuration.get("colors_palette")["use_unicode"])
|
|
self.assertEqual(configuration.get("ip_settings")["lan_ip_max_count"], 4)
|
|
self.assertTrue(configuration.get("temperature")["use_fahrenheit"])
|
|
|
|
def test__iter__(self):
|
|
"""Very simple method checking our `__iter__` implementation"""
|
|
configuration = Configuration()
|
|
self.assertEqual(
|
|
configuration._config, # pylint: disable=protected-access
|
|
dict(configuration),
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|