mirror of
https://github.com/HorlogeSkynet/archey4
synced 2025-07-14 04:00:10 +02:00
[FEATURE] [PERF] Adds support for ID_LIKE
(from os-release
)
> See <https://www.freedesktop.org/software/systemd/man/os-release.html#ID_LIKE=>. + Optimizes `Output` initialization using full `Enum` features
This commit is contained in:
archey
@ -23,13 +23,19 @@ class Output:
|
||||
if 'microsoft' in check_output(['uname', '-r'], universal_newlines=True).lower():
|
||||
self.distribution = Distributions.WINDOWS
|
||||
else:
|
||||
distribution_id = distro.id()
|
||||
for distribution in Distributions:
|
||||
if distribution_id == distribution.value:
|
||||
self.distribution = distribution
|
||||
try:
|
||||
self.distribution = Distributions(distro.id())
|
||||
except ValueError:
|
||||
# See <https://www.freedesktop.org/software/systemd/man/os-release.html#ID_LIKE=>.
|
||||
for distro_like in distro.like().split(' '):
|
||||
try:
|
||||
self.distribution = Distributions(distro_like)
|
||||
except ValueError:
|
||||
continue
|
||||
break
|
||||
else:
|
||||
self.distribution = Distributions.LINUX
|
||||
else:
|
||||
# Well, we didn't match anything so let's fall-back to default `Linux`.
|
||||
self.distribution = Distributions.LINUX
|
||||
|
||||
# Fetch the colors palette related to this distribution.
|
||||
self.colors_palette = COLOR_DICT[self.distribution]
|
||||
|
@ -41,6 +41,60 @@ class TestOutputUtil(unittest.TestCase):
|
||||
|
||||
self.assertEqual(output.distribution, Distributions.LINUX)
|
||||
|
||||
@patch(
|
||||
'archey.output.check_output',
|
||||
return_value='X.Y.Z-R-ARCH\n'
|
||||
)
|
||||
@patch(
|
||||
'archey.output.distro.id',
|
||||
return_value='' # Unknown distribution.
|
||||
)
|
||||
@patch(
|
||||
'archey.output.distro.like',
|
||||
return_value='ubuntu' # Oh, it's actually an Ubuntu-based one !
|
||||
)
|
||||
def test_init_distro_like(self, _, __, ___):
|
||||
"""Test distribution matching from the `os-release`'s `ID_LIKE` option"""
|
||||
output = Output()
|
||||
|
||||
self.assertEqual(output.distribution, Distributions.UBUNTU)
|
||||
|
||||
@patch(
|
||||
'archey.output.check_output',
|
||||
return_value='X.Y.Z-R-ARCH\n'
|
||||
)
|
||||
@patch(
|
||||
'archey.output.distro.id',
|
||||
return_value='' # Unknown distribution.
|
||||
)
|
||||
@patch(
|
||||
'archey.output.distro.like',
|
||||
return_value='linuxmint debian' # Oh, what do we got there ?!
|
||||
)
|
||||
def test_init_distro_like_multiple(self, _, __, ___):
|
||||
"""Test distribution matching from the `os-release`'s `ID_LIKE` option (multiple entries)"""
|
||||
output = Output()
|
||||
|
||||
self.assertEqual(output.distribution, Distributions.LINUX_MINT)
|
||||
|
||||
@patch(
|
||||
'archey.output.check_output',
|
||||
return_value='X.Y.Z-R-ARCH\n'
|
||||
)
|
||||
@patch(
|
||||
'archey.output.distro.id',
|
||||
return_value='' # Unknown distribution.
|
||||
)
|
||||
@patch(
|
||||
'archey.output.distro.like',
|
||||
return_value='' # No `ID_LIKE` either...
|
||||
)
|
||||
def test_init_both_distro_calls_fail(self, _, __, ___):
|
||||
"""Test distribution fall-back when `distro` soft-fail two times"""
|
||||
output = Output()
|
||||
|
||||
self.assertEqual(output.distribution, Distributions.LINUX)
|
||||
|
||||
@patch(
|
||||
'archey.output.check_output',
|
||||
return_value='X.Y.Z-R-Microsoft\n'
|
||||
|
Reference in New Issue
Block a user