From 159d87c1126f4d79d5e34837bc2c353a5445803b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 22 Oct 2021 07:14:21 -0700 Subject: [PATCH] add length guard and test for uname with no -a on linux --- jc/parsers/uname.py | 11 ++++++----- tests/fixtures/centos-7.7/uname.out | 1 + tests/test_uname.py | 13 +++++++++++-- 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/centos-7.7/uname.out diff --git a/jc/parsers/uname.py b/jc/parsers/uname.py index 3eedd220..bd03582d 100644 --- a/jc/parsers/uname.py +++ b/jc/parsers/uname.py @@ -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 # there will be unexpected results during parsing. fixup = data.split() - fixup_set = set([fixup[-2], fixup[-3], fixup[-4]]) - if len(fixup_set) > 2: - fixup.insert(-1, 'unknown') - fixup.insert(-1, 'unknown') - data = ' '.join(fixup) + if len(fixup) >= 4: + fixup_set = set([fixup[-2], fixup[-3], fixup[-4]]) + if len(fixup_set) > 2: + fixup.insert(-1, 'unknown') + fixup.insert(-1, 'unknown') + data = ' '.join(fixup) parsed_line = data.split(maxsplit=3) diff --git a/tests/fixtures/centos-7.7/uname.out b/tests/fixtures/centos-7.7/uname.out new file mode 100644 index 00000000..9b075671 --- /dev/null +++ b/tests/fixtures/centos-7.7/uname.out @@ -0,0 +1 @@ +Linux diff --git a/tests/test_uname.py b/tests/test_uname.py index d53cc471..02e17e69 100644 --- a/tests/test_uname.py +++ b/tests/test_uname.py @@ -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: 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: self.ubuntu_18_4_uname_a = f.read() @@ -74,12 +77,18 @@ class MyTests(unittest.TestCase): """ 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) + 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): """ Test 'uname -a' on Centos 7.7