1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-23 00:29:59 +02:00

strip whitespace in string fields and add tests

This commit is contained in:
Kelly Brazil
2020-05-30 17:01:59 -07:00
parent a3af8662bd
commit b3c6c1ea92
4 changed files with 29 additions and 5 deletions

View File

@ -83,7 +83,7 @@ import jc.utils
class info(): class info():
version = '1.0' version = '1.1'
description = 'w command parser' description = 'w command parser'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'
@ -174,6 +174,12 @@ def parse(data, raw=False, quiet=False):
output_line = dict(zip(headers, temp_line)) output_line = dict(zip(headers, temp_line))
raw_output.append(output_line) raw_output.append(output_line)
# strip whitespace from beginning and end of all string values
for row in raw_output:
for item in row:
if isinstance(row[item], str):
row[item] = row[item].strip()
if raw: if raw:
return raw_output return raw_output
else: else:

1
tests/fixtures/nixos/w.json vendored Normal file
View File

@ -0,0 +1 @@
[{"user": "kbrazil", "tty": "tty1", "login_at": "23:40", "idle": "0.00s", "jcpu": "0.18s", "pcpu": "0.07s", "what": "ssh localhost"}, {"user": "kbrazil", "tty": "pts/0", "login_at": "23:43", "idle": "1:37", "jcpu": "0.05s", "pcpu": "0.05s", "what": "-bash"}, {"user": "kbrazil", "tty": "pts/1", "login_at": "23:46", "idle": "0.00s", "jcpu": "0.02s", "pcpu": "0.00s", "what": "sshd: kbrazil [priv]"}]

5
tests/fixtures/nixos/w.out vendored Normal file
View File

@ -0,0 +1,5 @@
23:50:09 up 10 min, 3 users, load average: 0.00, 0.03, 0.01
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
kbrazil tty1 23:40 0.00s 0.18s 0.07s ssh localhost
kbrazil pts/0 23:43 1:37 0.05s 0.05s -bash
kbrazil pts/1 23:46 0.00s 0.02s 0.00s sshd: kbrazil [priv]

View File

@ -22,6 +22,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/w.out'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/w.out'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_w = f.read() self.osx_10_14_6_w = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/nixos/w.out'), 'r', encoding='utf-8') as f:
self.nixos_w = f.read()
# output # output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/w.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/w.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_w_json = json.loads(f.read()) self.centos_7_7_w_json = json.loads(f.read())
@ -30,10 +33,13 @@ class MyTests(unittest.TestCase):
self.ubuntu_18_4_w_json = json.loads(f.read()) self.ubuntu_18_4_w_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.11.6/w.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.11.6/w.json'), 'r', encoding='utf-8') as f:
self.osx_10_11_6_json = json.loads(f.read()) self.osx_10_11_6_w_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/w.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/w.json'), 'r', encoding='utf-8') as f:
self.osx_10_14_6_json = json.loads(f.read()) self.osx_10_14_6_w_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/nixos/w.json'), 'r', encoding='utf-8') as f:
self.nixos_w_json = json.loads(f.read())
def test_w_centos_7_7(self): def test_w_centos_7_7(self):
""" """
@ -51,13 +57,19 @@ class MyTests(unittest.TestCase):
""" """
Test 'w' on OSX 10.11.6 Test 'w' on OSX 10.11.6
""" """
self.assertEqual(jc.parsers.w.parse(self.osx_10_11_6_w, quiet=True), self.osx_10_11_6_json) self.assertEqual(jc.parsers.w.parse(self.osx_10_11_6_w, quiet=True), self.osx_10_11_6_w_json)
def test_w_osx_10_14_6(self): def test_w_osx_10_14_6(self):
""" """
Test 'w' on OSX 10.14.6 Test 'w' on OSX 10.14.6
""" """
self.assertEqual(jc.parsers.w.parse(self.osx_10_14_6_w, quiet=True), self.osx_10_14_6_json) self.assertEqual(jc.parsers.w.parse(self.osx_10_14_6_w, quiet=True), self.osx_10_14_6_w_json)
def test_w_nixos(self):
"""
Test 'w' on nixos
"""
self.assertEqual(jc.parsers.w.parse(self.nixos_w, quiet=True), self.nixos_w_json)
if __name__ == '__main__': if __name__ == '__main__':