1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/tests/test_ls_s.py

185 lines
8.1 KiB
Python
Raw Normal View History

2021-09-21 12:33:05 -07:00
import os
import sys
import time
import json
import unittest
from jc.exceptions import ParseError
import jc.parsers.ls_s
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
2021-09-21 22:00:05 -07:00
2021-09-21 12:33:05 -07:00
# To create streaming output use:
# $ cat ls-al.out | jc --ls-s | jello -c > ls-al-streaming.json
2021-09-21 22:00:05 -07:00
2021-09-21 12:33:05 -07:00
class MyTests(unittest.TestCase):
2022-09-23 14:02:27 -07:00
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls.out'), 'r', encoding='utf-8') as f:
centos_7_7_ls = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-al.out'), 'r', encoding='utf-8') as f:
centos_7_7_ls_al = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-al.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_al = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.11.6/ls-al.out'), 'r', encoding='utf-8') as f:
osx_10_11_6_ls_al = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-al.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_al = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-alh.out'), 'r', encoding='utf-8') as f:
centos_7_7_ls_alh = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alh.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_alh = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.11.6/ls-alh.out'), 'r', encoding='utf-8') as f:
osx_10_11_6_ls_alh = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alh.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_alh = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-alR.out'), 'r', encoding='utf-8') as f:
centos_7_7_ls_alR = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alR.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_alR = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alR.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_alR = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-lR-empty-folder.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_lR_empty_folder = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-l-iso.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_l_iso = f.read()
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
# output
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-al-streaming.json'), 'r', encoding='utf-8') as f:
centos_7_7_ls_al_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-al-streaming.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_al_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-al-streaming.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_al_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-alh-streaming.json'), 'r', encoding='utf-8') as f:
centos_7_7_ls_alh_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alh-streaming.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_alh_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alh-streaming.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_alh_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-alR-streaming.json'), 'r', encoding='utf-8') as f:
centos_7_7_ls_alR_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alR-streaming.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_alR_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alR-streaming.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_alR_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2022-09-23 14:02:27 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-lR-empty-folder-streaming.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_ls_lR_empty_folder_streaming_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-l-iso-streaming.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_ls_l_iso_streaming_json = json.loads(f.read())
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_empty_dir(self):
2021-09-21 12:33:05 -07:00
"""
Test plain 'ls' on an empty directory
"""
2021-11-30 09:56:33 -08:00
self.assertEqual(list(jc.parsers.ls_s.parse([], quiet=True)), [])
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_centos_7_7_raise_exception(self):
2021-09-21 12:33:05 -07:00
"""
Test plain 'ls /' on Centos 7.7 (raises ParseError)
"""
2021-09-23 11:48:39 -07:00
g = jc.parsers.ls_s.parse(self.centos_7_7_ls.splitlines(), quiet=True)
2021-09-21 12:33:05 -07:00
with self.assertRaises(ParseError):
list(g)
2021-10-25 11:35:01 -07:00
def test_ls_s_al_centos_7_7(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -al /' on Centos 7.7
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.centos_7_7_ls_al.splitlines(), quiet=True)), self.centos_7_7_ls_al_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_al_ubuntu_18_4(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -al /' on Ubuntu 18.4
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.ubuntu_18_4_ls_al.splitlines(), quiet=True)), self.ubuntu_18_4_ls_al_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_al_osx_10_14_6(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -al /' on OSX 10.14.6
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.osx_10_14_6_ls_al.splitlines(), quiet=True)), self.osx_10_14_6_ls_al_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_alh_centos_7_7(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -alh /' on Centos 7.7
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.centos_7_7_ls_alh.splitlines(), quiet=True)), self.centos_7_7_ls_alh_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_alh_ubuntu_18_4(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -alh /' on Ubuntu 18.4
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.ubuntu_18_4_ls_alh.splitlines(), quiet=True)), self.ubuntu_18_4_ls_alh_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_alh_osx_10_14_6(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -alh /' on OSX 10.14.6
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.osx_10_14_6_ls_alh.splitlines(), quiet=True)), self.osx_10_14_6_ls_alh_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_alR_centos_7_7(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -alR /usr' on Centos 7.7
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.centos_7_7_ls_alR.splitlines(), quiet=True)), self.centos_7_7_ls_alR_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_alR_ubuntu_18_4(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -alR /usr' on Ubuntu 18.4
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.ubuntu_18_4_ls_alR.splitlines(), quiet=True)), self.ubuntu_18_4_ls_alR_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_alR_osx_10_14_6(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -alR /usr' on OSX 10.14.6
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.osx_10_14_6_ls_alR.splitlines(), quiet=True)), self.osx_10_14_6_ls_alR_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_lR_empty_folder_osx_10_14_6(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -lR' for empty directories on OSX 10.14.6
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.osx_10_14_6_ls_lR_empty_folder.splitlines(), quiet=True)), self.osx_10_14_6_ls_lR_empty_folder_streaming_json)
2021-09-21 12:33:05 -07:00
2021-10-25 11:35:01 -07:00
def test_ls_s_l_iso_ubuntu_18_4(self):
2021-09-21 12:33:05 -07:00
"""
Test 'ls -l --time-style=full-iso' for files with convertible dates on Ubuntu 18.4
"""
2021-09-23 11:48:39 -07:00
self.assertEqual(list(jc.parsers.ls_s.parse(self.ubuntu_18_4_ls_l_iso.splitlines(), quiet=True)), self.ubuntu_18_4_ls_l_iso_streaming_json)
2021-09-21 12:33:05 -07:00
if __name__ == '__main__':
unittest.main()