1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

add length guard and test for uname with no -a on linux

This commit is contained in:
Kelly Brazil
2021-10-22 07:14:21 -07:00
parent 9e7b1621cf
commit 159d87c112
3 changed files with 18 additions and 7 deletions

View File

@ -119,11 +119,12 @@ def parse(data, raw=False, quiet=False):
# case of only two existing is undefined. Must either be one or all three existing, otherwise # case of only two existing is undefined. Must either be one or all three existing, otherwise
# there will be unexpected results during parsing. # there will be unexpected results during parsing.
fixup = data.split() fixup = data.split()
fixup_set = set([fixup[-2], fixup[-3], fixup[-4]]) if len(fixup) >= 4:
if len(fixup_set) > 2: fixup_set = set([fixup[-2], fixup[-3], fixup[-4]])
fixup.insert(-1, 'unknown') if len(fixup_set) > 2:
fixup.insert(-1, 'unknown') fixup.insert(-1, 'unknown')
data = ' '.join(fixup) fixup.insert(-1, 'unknown')
data = ' '.join(fixup)
parsed_line = data.split(maxsplit=3) parsed_line = data.split(maxsplit=3)

1
tests/fixtures/centos-7.7/uname.out vendored Normal file
View File

@ -0,0 +1 @@
Linux

View File

@ -13,6 +13,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/uname-a.out'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/uname-a.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_uname_a = f.read() self.centos_7_7_uname_a = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/uname.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_uname = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/uname-a.out'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/uname-a.out'), 'r', encoding='utf-8') as f:
self.ubuntu_18_4_uname_a = f.read() self.ubuntu_18_4_uname_a = f.read()
@ -74,12 +77,18 @@ class MyTests(unittest.TestCase):
""" """
self.assertEqual(jc.parsers.uname.parse('', quiet=True), {}) self.assertEqual(jc.parsers.uname.parse('', quiet=True), {})
def test_uname_no_a(self): def test_uname_no_a_osx(self):
""" """
Test 'uname' without -a option. Should generate a ParseError exception Test 'uname' without -a option on OSX. Should generate a ParseError exception
""" """
self.assertRaises(jc.parsers.uname.ParseError, jc.parsers.uname.parse, self.osx_10_14_6_uname) self.assertRaises(jc.parsers.uname.ParseError, jc.parsers.uname.parse, self.osx_10_14_6_uname)
def test_uname_no_a_centos(self):
"""
Test 'uname' without -a option on Centos. Should generate a ParseError exception
"""
self.assertRaises(jc.parsers.uname.ParseError, jc.parsers.uname.parse, self.centos_7_7_uname)
def test_uname_centos_7_7(self): def test_uname_centos_7_7(self):
""" """
Test 'uname -a' on Centos 7.7 Test 'uname -a' on Centos 7.7