archey4/archey/test/test_archey_processes.py
2022-09-18 11:09:21 +02:00

69 lines
1.7 KiB
Python

"""Test module for `archey.processes`"""
import unittest
from unittest.mock import patch
from archey.processes import Processes
from archey.test import CustomAssertions
# 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 TestProcesses(unittest.TestCase, CustomAssertions):
"""
Test cases for the `Processes` (singleton) class.
To work around the singleton, we reset the internal `_instances` dictionary.
This way, `check_output` can be mocked here.
"""
@patch(
"archey.processes.check_output",
return_value="""\
COMMAND
what
an
awesome
processes
list
you
got
there
""",
)
def test_ps_ok(self, check_output_mock):
"""Simple test with a plausible `ps` output"""
# We'll create two `Processes` instances.
processes_1 = Processes()
_ = Processes()
self.assertTupleEqual(
processes_1.list,
(
"what",
"an",
"awesome",
"processes",
"list",
"you",
"got",
"there",
),
)
self.assertEqual(processes_1.number, 8)
# The class has been instantiated twice, but `check_output` has been called only once.
self.assertTrue(check_output_mock.assert_called_once)
@patch("archey.processes.check_output", side_effect=FileNotFoundError())
def test_ps_not_available(self, _):
"""Checks behavior when `ps` is not available"""
self.assertTupleEmpty(Processes().list)
if __name__ == "__main__":
unittest.main()