1
0
mirror of https://github.com/HorlogeSkynet/archey4 synced 2025-04-12 00:00:19 +02:00

[TESTS] Implements some fix-ups when running against macOS

This commit is contained in:
Samuel FORESTIER 2021-03-09 15:16:57 +01:00
parent 3cced364b8
commit d76bf18afc
6 changed files with 37 additions and 146 deletions

@ -6,6 +6,10 @@ from unittest.mock import patch
from archey.entries.desktop_environment import DesktopEnvironment
@patch(
'archey.entries.desktop_environment.platform.system',
return_value='Linux'
)
class TestDesktopEnvironmentEntry(unittest.TestCase):
"""
With the help of a fake running processes list, we test the DE matching.
@ -20,7 +24,7 @@ class TestDesktopEnvironmentEntry(unittest.TestCase):
'tea'
)
)
def test_match(self):
def test_match(self, _):
"""Simple list matching"""
self.assertEqual(DesktopEnvironment().value, 'Cinnamon')
@ -38,16 +42,15 @@ class TestDesktopEnvironmentEntry(unittest.TestCase):
'archey.entries.desktop_environment.os.getenv',
return_value='DESKTOP ENVIRONMENT'
)
def test_mismatch(self, _):
def test_mismatch(self, _, __):
"""Simple list (mis-)-matching"""
self.assertEqual(DesktopEnvironment().value, 'DESKTOP ENVIRONMENT')
@patch(
'archey.entries.desktop_environment.platform.system',
return_value='Darwin'
)
def test_darwin_aqua_deduction(self, _):
@patch('archey.entries.desktop_environment.platform.system')
def test_darwin_aqua_deduction(self, _, platform_system_mock):
"""Test "Aqua" deduction on Darwin systems"""
platform_system_mock.return_value = 'Darwin' # Override module-wide mocked value.
self.assertEqual(DesktopEnvironment().value, 'Aqua')
@patch(
@ -64,7 +67,7 @@ class TestDesktopEnvironmentEntry(unittest.TestCase):
'archey.entries.desktop_environment.os.getenv',
return_value=None # The environment variable is empty...
)
def test_non_detection(self, _):
def test_non_detection(self, _, __):
"""Simple global non-detection"""
self.assertIsNone(DesktopEnvironment().value)

@ -10,24 +10,6 @@ from archey.test.entries import HelperMethods
class TestDistroEntry(unittest.TestCase):
"""`Distro` entry simple test cases"""
@patch(
'archey.entries.distro.Distributions.get_distro_name',
return_value='NAME VERSION (CODENAME)'
)
@patch(
'archey.entries.distro.platform.machine',
return_value='ARCHITECTURE'
)
def test_init(self, _, __):
"""Test `Distro` instantiation"""
self.assertDictEqual(
Distro().value,
{
'name': 'NAME VERSION (CODENAME)',
'arch': 'ARCHITECTURE'
}
)
@patch(
'archey.entries.distro.check_output',
return_value='10\n' # Imitate `getprop` output on Android 10.
@ -70,33 +52,18 @@ class TestDistroEntry(unittest.TestCase):
)
self.assertIsNone(Distro._fetch_darwin_release()) # pylint: disable=protected-access
@patch(
'archey.entries.distro.Distributions.get_distro_name',
return_value=None # Soft-failing : No _pretty_ distribution name found...
)
@patch(
'archey.entries.distro.platform.machine',
return_value='ARCHITECTURE'
)
@patch(
'archey.entries.distro.Distro._fetch_android_release',
return_value=None # Not an Android device either...
)
@HelperMethods.patch_clean_configuration
def test_unknown_distro_output(self, _, __, ___):
"""Test for `distro` and `uname` outputs concatenation"""
distro = Distro()
def test_unknown_distro_output(self):
"""Test for `output` method when distribution name couldn't be found"""
distro_intance_mock = HelperMethods.entry_mock(Distro)
output_mock = MagicMock()
distro.output(output_mock)
self.assertDictEqual(
distro.value,
{
'name': None,
'arch': 'ARCHITECTURE'
}
)
distro_intance_mock.value = {
'name': None,
'arch': 'ARCHITECTURE'
}
Distro.output(distro_intance_mock, output_mock)
self.assertEqual(
output_mock.append.call_args[0][1],
f"{DEFAULT_CONFIG['default_strings']['not_detected']} [ARCHITECTURE]"

@ -249,31 +249,15 @@ class TestModelEntry(unittest.TestCase):
Model._fetch_android_device_model() # pylint: disable=protected-access
)
@patch(
'archey.entries.model.Model._fetch_virtual_env_info',
return_value=None # Not a virtual environment...
)
@patch(
'archey.entries.model.Model._fetch_product_info',
return_value=None # No model name could be retrieved...
)
@patch(
'archey.entries.model.Model._fetch_raspberry_pi_revision',
return_value=None # Not a Raspberry Pi device either...
)
@patch(
'archey.entries.model.Model._fetch_android_device_model',
return_value=None # Not an Android device...
)
@HelperMethods.patch_clean_configuration
def test_no_match(self, _, __, ___, ____):
def test_no_match(self):
"""Test when no information could be retrieved"""
model = Model()
model_instance_mock = HelperMethods.entry_mock(Model)
output_mock = MagicMock()
model.output(output_mock)
Model.output(model_instance_mock, output_mock)
self.assertIsNone(model.value)
self.assertIsNone(model_instance_mock.value)
self.assertEqual(
output_mock.append.call_args[0][1],
DEFAULT_CONFIG['default_strings']['not_detected']

@ -6,6 +6,7 @@ from datetime import timedelta
from itertools import product
from archey.entries.uptime import Uptime
from archey.test.entries import HelperMethods
class TestUptimeEntry(unittest.TestCase):
@ -120,8 +121,9 @@ class TestUptimeEntry(unittest.TestCase):
create=True # Required for Python < 3.7.
)
@patch(
'archey.entries.uptime.time.clock_gettime',
return_value=1000
'archey.entries.uptime.time.clock_gettime', # Ensure `clock_gettime` call succeeds anyhow.
return_value=1000,
create=True
)
def test_clock_fallback(self, _, __, ___):
"""
@ -140,7 +142,7 @@ class TestUptimeEntry(unittest.TestCase):
"""Test `uptime` command parsing"""
# Create an uptime instance to perform testing.
# It doesn't matter that its `__init__` will be called.
uptime_inst = Uptime()
uptime_instance_mock = HelperMethods.entry_mock(Uptime)
# Keys: `uptime` outputs; values: expected `timedelta` instances.
# We will test these with various time formats (and various numbers of users).
@ -238,7 +240,7 @@ class TestUptimeEntry(unittest.TestCase):
user_loadavg=variations[1]
).encode()
self.assertEqual(
uptime_inst._parse_uptime_cmd(), # pylint: disable=protected-access
uptime_instance_mock._parse_uptime_cmd(), # pylint: disable=protected-access
expected_delta,
msg='`uptime` output: "{}"'.format(
uptime_output.format(

@ -8,6 +8,10 @@ from archey.entries.window_manager import WindowManager
from archey.test.entries import HelperMethods
@patch(
'archey.entries.desktop_environment.platform.system',
return_value='Linux'
)
class TestWindowManagerEntry(unittest.TestCase):
"""
Here, we mock the `check_output` call and check afterwards
@ -22,7 +26,7 @@ Class: N/A
PID: N/A
Window manager's "showing the desktop" mode: OFF
""")
def test_wmctrl(self, _):
def test_wmctrl(self, _, __):
"""Test `wmctrl` output parsing"""
self.assertEqual(WindowManager().value, 'WINDOW MANAGER')
@ -40,7 +44,7 @@ Window manager's "showing the desktop" mode: OFF
'here'
)
)
def test_no_wmctrl_match(self, _):
def test_no_wmctrl_match(self, _, __):
"""Test basic detection based on a (fake) processes list"""
self.assertEqual(WindowManager().value, 'Awesome')
@ -59,7 +63,7 @@ Window manager's "showing the desktop" mode: OFF
)
)
@HelperMethods.patch_clean_configuration
def test_no_wmctrl_mismatch(self, _):
def test_no_wmctrl_mismatch(self, _, __):
"""Test (non-detection) when processes list do not contain any known value"""
window_manager = WindowManager()

@ -146,75 +146,6 @@ class TestDistributions(unittest.TestCase):
Distributions.ARCH
)
@patch(
'archey.distributions.platform.system',
return_value='Linux'
)
@patch(
'archey.distributions.platform.release',
return_value='X.Y.Z-R-ARCH'
)
@patch(
'archey.distributions.distro.id',
return_value='' # Unknown distribution.
)
@patch(
'archey.distributions.distro.like',
return_value='' # No `ID_LIKE` either...
)
@patch(
'archey.distributions.os.path.isdir', # Make Android detection fails.
return_value=False
)
def test_get_local_both_distro_calls_fail(self, _, __, ___, ____, _____):
"""Test distribution fall-back when `distro` soft-fail two times"""
self.assertEqual(
Distributions.get_local(),
Distributions.LINUX
)
@patch(
'archey.distributions.platform.system',
return_value='Linux'
)
@patch(
'archey.distributions.platform.release',
return_value='X.Y.Z-R-ARCH'
)
@patch(
'archey.distributions.distro.id',
return_value='debian'
)
@patch(
'archey.distributions.os.path.isfile', # Emulate a CrunchBang file-system.
side_effect=(
lambda file_path: file_path == '/etc/lsb-release-crunchbang'
)
)
def test_get_local_specific_crunchbang(self, _, __, ___, ____):
"""Test CrunchBang specific detection"""
self.assertEqual(
Distributions.get_local(),
Distributions.CRUNCHBANG
)
@patch(
'archey.distributions.Distributions._vendor_detection',
return_value=None # Base detection logic soft-fails...
)
@patch(
'archey.distributions.os.path.isdir', # Emulate an Android file-system.
side_effect=(
lambda dir_path: dir_path.startswith('/system/') and dir_path.endswith('app')
)
)
def test_get_local_specific_android(self, _, __):
"""Test Android specific detection"""
self.assertEqual(
Distributions.get_local(),
Distributions.ANDROID
)
@patch(
'archey.distributions.platform.system',
return_value='Darwin' # Mostly used by our second run.