1
0
mirror of https://github.com/HorlogeSkynet/archey4 synced 2025-06-14 04:00:10 +02:00

[SHELL] Extends entry support when SHELL env. var. is not propagated

This commit is contained in:
Samuel FORESTIER
2020-04-21 09:32:01 +02:00
parent 022383a7c1
commit b2c116f9a4
2 changed files with 52 additions and 6 deletions

@ -2,13 +2,26 @@
import os
from subprocess import CalledProcessError, check_output
from archey.configuration import Configuration
class Shell:
"""Simple shell detection based on the `SHELL` environment variable"""
"""
Simple shell path detection based either on the `SHELL` environment variable or
the local administrative database.
"""
def __init__(self):
self.value = os.getenv(
'SHELL',
Configuration().get('default_strings')['not_detected']
)
shell = os.getenv('SHELL')
if not shell:
try:
shell = check_output(
['getent', 'passwd', str(os.getuid())],
universal_newlines=True
).rstrip().split(':')[-1]
except CalledProcessError:
# Where does this user come from ?
shell = Configuration().get('default_strings')['not_detected']
self.value = shell

@ -1,5 +1,7 @@
"""Test module for Archey's shell detection module"""
from subprocess import CalledProcessError
import unittest
from unittest.mock import patch
@ -14,10 +16,41 @@ class TestShellEntry(unittest.TestCase):
'archey.entries.shell.os.getenv',
return_value='SHELL'
)
def test(self, _):
def test_getenv(self, _):
"""Simple mock, simple test"""
self.assertEqual(Shell().value, 'SHELL')
@patch(
'archey.entries.shell.os.getenv',
return_value=None
)
@patch(
'archey.entries.shell.os.getuid', # We DO NOT HAVE TO mock this call.
return_value=1000
)
@patch(
'archey.entries.shell.check_output',
return_value="USERNAME:x:1000:1000:User Name,,,:/home/user:/bin/bash\n"
)
def test_getent_call(self, _, __, ___):
"""Mock `getent` returned value and check the correct assignment"""
self.assertEqual(Shell().value, '/bin/bash')
@patch(
'archey.entries.shell.os.getenv',
return_value=None
)
@patch(
'archey.entries.shell.check_output',
side_effect=CalledProcessError(2, 'getent')
)
@patch(
'archey.entries.shell.Configuration.get',
return_value={'not_detected': 'Not detected'}
)
def test_config_fall_back(self, _, __, ___):
"""`id` fails, but Archey must not !"""
self.assertEqual(Shell().value, 'Not detected')
if __name__ == '__main__':
unittest.main()