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

doc update and fix tests

This commit is contained in:
Kelly Brazil
2023-10-21 12:09:18 -07:00
parent ab0e05ec82
commit 1ed69f9e6a
2 changed files with 207 additions and 198 deletions

View File

@ -1,6 +1,6 @@
jc changelog jc changelog
20231002 v1.23.5 20231021 v1.23.5
- Add `host` command parser - Add `host` command parser
- Add `nsd-control` command parser - Add `nsd-control` command parser
- Add `lsb_release` command parser - Add `lsb_release` command parser
@ -12,6 +12,7 @@ jc changelog
- Fix `x509-cert` parser to allow negative serial numbers - Fix `x509-cert` parser to allow negative serial numbers
- Fix `x509-cert` parser for cases when bitstrings are larger than standard - Fix `x509-cert` parser for cases when bitstrings are larger than standard
- Fix `xrandr` command parser for associated device issues - Fix `xrandr` command parser for associated device issues
- Fix error when pygments library is not installed
20230730 v1.23.4 20230730 v1.23.4
- Add `/etc/resolve.conf` file parser - Add `/etc/resolve.conf` file parser

View File

@ -1,8 +1,12 @@
import os import os
import unittest import unittest
from datetime import datetime, timezone from datetime import datetime, timezone
import pygments try:
from pygments.token import (Name, Number, String, Keyword) import pygments
from pygments.token import (Name, Number, String, Keyword)
PYGMENTS_INSTALLED=True
except ModuleNotFoundError:
PYGMENTS_INSTALLED=False
from jc.cli import JcCli from jc.cli import JcCli
@ -42,216 +46,220 @@ class MyTests(unittest.TestCase):
self.assertEqual(expected, resulting_attributes) self.assertEqual(expected, resulting_attributes)
def test_cli_set_env_colors(self): def test_cli_set_env_colors(self):
if pygments.__version__.startswith('2.3.'): if PYGMENTS_INSTALLED:
env = { if pygments.__version__.startswith('2.3.'):
'': { env = {
Name.Tag: 'bold #ansidarkblue', '': {
Keyword: '#ansidarkgray', Name.Tag: 'bold #ansidarkblue',
Number: '#ansipurple', Keyword: '#ansidarkgray',
String: '#ansidarkgreen' Number: '#ansipurple',
}, String: '#ansidarkgreen'
' ': { },
Name.Tag: 'bold #ansidarkblue', ' ': {
Keyword: '#ansidarkgray', Name.Tag: 'bold #ansidarkblue',
Number: '#ansipurple', Keyword: '#ansidarkgray',
String: '#ansidarkgreen' Number: '#ansipurple',
}, String: '#ansidarkgreen'
'default,default,default,default': { },
Name.Tag: 'bold #ansidarkblue', 'default,default,default,default': {
Keyword: '#ansidarkgray', Name.Tag: 'bold #ansidarkblue',
Number: '#ansipurple', Keyword: '#ansidarkgray',
String: '#ansidarkgreen' Number: '#ansipurple',
}, String: '#ansidarkgreen'
'red,red,red,red': { },
Name.Tag: 'bold #ansidarkred', 'red,red,red,red': {
Keyword: '#ansidarkred', Name.Tag: 'bold #ansidarkred',
Number: '#ansidarkred', Keyword: '#ansidarkred',
String: '#ansidarkred' Number: '#ansidarkred',
}, String: '#ansidarkred'
'red,red,yada,red': { },
Name.Tag: 'bold #ansidarkblue', 'red,red,yada,red': {
Keyword: '#ansidarkgray', Name.Tag: 'bold #ansidarkblue',
Number: '#ansipurple', Keyword: '#ansidarkgray',
String: '#ansidarkgreen' Number: '#ansipurple',
}, String: '#ansidarkgreen'
'red,red,red': { },
Name.Tag: 'bold #ansidarkblue', 'red,red,red': {
Keyword: '#ansidarkgray', Name.Tag: 'bold #ansidarkblue',
Number: '#ansipurple', Keyword: '#ansidarkgray',
String: '#ansidarkgreen' Number: '#ansipurple',
}, String: '#ansidarkgreen'
'red,red,red,red,red,red': { },
Name.Tag: 'bold #ansidarkblue', 'red,red,red,red,red,red': {
Keyword: '#ansidarkgray', Name.Tag: 'bold #ansidarkblue',
Number: '#ansipurple', Keyword: '#ansidarkgray',
String: '#ansidarkgreen' Number: '#ansipurple',
String: '#ansidarkgreen'
}
} }
} else:
else: env = {
env = { '': {
'': { Name.Tag: 'bold ansiblue',
Name.Tag: 'bold ansiblue', Keyword: 'ansibrightblack',
Keyword: 'ansibrightblack', Number: 'ansimagenta',
Number: 'ansimagenta', String: 'ansigreen'
String: 'ansigreen' },
}, ' ': {
' ': { Name.Tag: 'bold ansiblue',
Name.Tag: 'bold ansiblue', Keyword: 'ansibrightblack',
Keyword: 'ansibrightblack', Number: 'ansimagenta',
Number: 'ansimagenta', String: 'ansigreen'
String: 'ansigreen' },
}, 'default,default,default,default': {
'default,default,default,default': { Name.Tag: 'bold ansiblue',
Name.Tag: 'bold ansiblue', Keyword: 'ansibrightblack',
Keyword: 'ansibrightblack', Number: 'ansimagenta',
Number: 'ansimagenta', String: 'ansigreen'
String: 'ansigreen' },
}, 'red,red,red,red': {
'red,red,red,red': { Name.Tag: 'bold ansired',
Name.Tag: 'bold ansired', Keyword: 'ansired',
Keyword: 'ansired', Number: 'ansired',
Number: 'ansired', String: 'ansired'
String: 'ansired' },
}, 'red,red,yada,red': {
'red,red,yada,red': { Name.Tag: 'bold ansiblue',
Name.Tag: 'bold ansiblue', Keyword: 'ansibrightblack',
Keyword: 'ansibrightblack', Number: 'ansimagenta',
Number: 'ansimagenta', String: 'ansigreen'
String: 'ansigreen' },
}, 'red,red,red': {
'red,red,red': { Name.Tag: 'bold ansiblue',
Name.Tag: 'bold ansiblue', Keyword: 'ansibrightblack',
Keyword: 'ansibrightblack', Number: 'ansimagenta',
Number: 'ansimagenta', String: 'ansigreen'
String: 'ansigreen' },
}, 'red,red,red,red,red,red': {
'red,red,red,red,red,red': { Name.Tag: 'bold ansiblue',
Name.Tag: 'bold ansiblue', Keyword: 'ansibrightblack',
Keyword: 'ansibrightblack', Number: 'ansimagenta',
Number: 'ansimagenta', String: 'ansigreen'
String: 'ansigreen' }
} }
}
for jc_colors, expected_colors in env.items(): for jc_colors, expected_colors in env.items():
cli = JcCli() cli = JcCli()
os.environ["JC_COLORS"] = jc_colors os.environ["JC_COLORS"] = jc_colors
cli.set_custom_colors() cli.set_custom_colors()
self.assertEqual(cli.custom_colors, expected_colors) self.assertEqual(cli.custom_colors, expected_colors)
def test_cli_json_out(self): def test_cli_json_out(self):
test_input = [ if PYGMENTS_INSTALLED:
None, test_input = [
{}, None,
[], {},
'', [],
{"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True}, '',
] {"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True},
if pygments.__version__.startswith('2.3.'):
expected_output = [
'\x1b[30;01mnull\x1b[39;00m',
'{}',
'[]',
'\x1b[32m""\x1b[39m',
'{\x1b[34;01m"key1"\x1b[39;00m:\x1b[32m"value1"\x1b[39m,\x1b[34;01m"key2"\x1b[39;00m:\x1b[35m2\x1b[39m,\x1b[34;01m"key3"\x1b[39;00m:\x1b[30;01mnull\x1b[39;00m,\x1b[34;01m"key4"\x1b[39;00m:\x1b[35m3.14\x1b[39m,\x1b[34;01m"key5"\x1b[39;00m:\x1b[30;01mtrue\x1b[39;00m}'
]
else:
expected_output = [
'\x1b[90mnull\x1b[39m',
'{}',
'[]',
'\x1b[32m""\x1b[39m',
'{\x1b[34;01m"key1"\x1b[39;00m:\x1b[32m"value1"\x1b[39m,\x1b[34;01m"key2"\x1b[39;00m:\x1b[35m2\x1b[39m,\x1b[34;01m"key3"\x1b[39;00m:\x1b[90mnull\x1b[39m,\x1b[34;01m"key4"\x1b[39;00m:\x1b[35m3.14\x1b[39m,\x1b[34;01m"key5"\x1b[39;00m:\x1b[90mtrue\x1b[39m}'
] ]
for test_dict, expected_json in zip(test_input, expected_output): if pygments.__version__.startswith('2.3.'):
cli = JcCli() expected_output = [
os.environ["JC_COLORS"] = "default,default,default,default" '\x1b[30;01mnull\x1b[39;00m',
cli.set_custom_colors() '{}',
cli.data_out = test_dict '[]',
self.assertEqual(cli.json_out(), expected_json) '\x1b[32m""\x1b[39m',
'{\x1b[34;01m"key1"\x1b[39;00m:\x1b[32m"value1"\x1b[39m,\x1b[34;01m"key2"\x1b[39;00m:\x1b[35m2\x1b[39m,\x1b[34;01m"key3"\x1b[39;00m:\x1b[30;01mnull\x1b[39;00m,\x1b[34;01m"key4"\x1b[39;00m:\x1b[35m3.14\x1b[39m,\x1b[34;01m"key5"\x1b[39;00m:\x1b[30;01mtrue\x1b[39;00m}'
]
else:
expected_output = [
'\x1b[90mnull\x1b[39m',
'{}',
'[]',
'\x1b[32m""\x1b[39m',
'{\x1b[34;01m"key1"\x1b[39;00m:\x1b[32m"value1"\x1b[39m,\x1b[34;01m"key2"\x1b[39;00m:\x1b[35m2\x1b[39m,\x1b[34;01m"key3"\x1b[39;00m:\x1b[90mnull\x1b[39m,\x1b[34;01m"key4"\x1b[39;00m:\x1b[35m3.14\x1b[39m,\x1b[34;01m"key5"\x1b[39;00m:\x1b[90mtrue\x1b[39m}'
]
for test_dict, expected_json in zip(test_input, expected_output):
cli = JcCli()
os.environ["JC_COLORS"] = "default,default,default,default"
cli.set_custom_colors()
cli.data_out = test_dict
self.assertEqual(cli.json_out(), expected_json)
def test_cli_json_out_mono(self): def test_cli_json_out_mono(self):
test_input = [ if PYGMENTS_INSTALLED:
None, test_input = [
{}, None,
[], {},
'', [],
{"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True}, '',
] {"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True},
expected_output = [
'null',
'{}',
'[]',
'""',
'{"key1":"value1","key2":2,"key3":null,"key4":3.14,"key5":true}'
]
for test_dict, expected_json in zip(test_input, expected_output):
cli = JcCli()
cli.set_custom_colors()
cli.mono = True
cli.data_out = test_dict
self.assertEqual(cli.json_out(), expected_json)
def test_cli_json_out_pretty(self):
test_input = [
{"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True},
{"key1": [{"subkey1": "subvalue1"}, {"subkey2": [1, 2, 3]}], "key2": True}
]
if pygments.__version__.startswith('2.3.'):
expected_output = [
'{\n \x1b[34;01m"key1"\x1b[39;00m: \x1b[32m"value1"\x1b[39m,\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[35m2\x1b[39m,\n \x1b[34;01m"key3"\x1b[39;00m: \x1b[30;01mnull\x1b[39;00m,\n \x1b[34;01m"key4"\x1b[39;00m: \x1b[35m3.14\x1b[39m,\n \x1b[34;01m"key5"\x1b[39;00m: \x1b[30;01mtrue\x1b[39;00m\n}',
'{\n \x1b[34;01m"key1"\x1b[39;00m: [\n {\n \x1b[34;01m"subkey1"\x1b[39;00m: \x1b[32m"subvalue1"\x1b[39m\n },\n {\n \x1b[34;01m"subkey2"\x1b[39;00m: [\n \x1b[35m1\x1b[39m,\n \x1b[35m2\x1b[39m,\n \x1b[35m3\x1b[39m\n ]\n }\n ],\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[30;01mtrue\x1b[39;00m\n}'
]
else:
expected_output = [
'{\n \x1b[34;01m"key1"\x1b[39;00m: \x1b[32m"value1"\x1b[39m,\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[35m2\x1b[39m,\n \x1b[34;01m"key3"\x1b[39;00m: \x1b[90mnull\x1b[39m,\n \x1b[34;01m"key4"\x1b[39;00m: \x1b[35m3.14\x1b[39m,\n \x1b[34;01m"key5"\x1b[39;00m: \x1b[90mtrue\x1b[39m\n}',
'{\n \x1b[34;01m"key1"\x1b[39;00m: [\n {\n \x1b[34;01m"subkey1"\x1b[39;00m: \x1b[32m"subvalue1"\x1b[39m\n },\n {\n \x1b[34;01m"subkey2"\x1b[39;00m: [\n \x1b[35m1\x1b[39m,\n \x1b[35m2\x1b[39m,\n \x1b[35m3\x1b[39m\n ]\n }\n ],\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[90mtrue\x1b[39m\n}'
] ]
for test_dict, expected_json in zip(test_input, expected_output): expected_output = [
cli = JcCli() 'null',
cli.pretty = True '{}',
cli.set_custom_colors() '[]',
cli.data_out = test_dict '""',
self.assertEqual(cli.json_out(), expected_json) '{"key1":"value1","key2":2,"key3":null,"key4":3.14,"key5":true}'
]
for test_dict, expected_json in zip(test_input, expected_output):
cli = JcCli()
cli.set_custom_colors()
cli.mono = True
cli.data_out = test_dict
self.assertEqual(cli.json_out(), expected_json)
def test_cli_json_out_pretty(self):
test_input = [
{"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True},
{"key1": [{"subkey1": "subvalue1"}, {"subkey2": [1, 2, 3]}], "key2": True}
]
if pygments.__version__.startswith('2.3.'):
expected_output = [
'{\n \x1b[34;01m"key1"\x1b[39;00m: \x1b[32m"value1"\x1b[39m,\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[35m2\x1b[39m,\n \x1b[34;01m"key3"\x1b[39;00m: \x1b[30;01mnull\x1b[39;00m,\n \x1b[34;01m"key4"\x1b[39;00m: \x1b[35m3.14\x1b[39m,\n \x1b[34;01m"key5"\x1b[39;00m: \x1b[30;01mtrue\x1b[39;00m\n}',
'{\n \x1b[34;01m"key1"\x1b[39;00m: [\n {\n \x1b[34;01m"subkey1"\x1b[39;00m: \x1b[32m"subvalue1"\x1b[39m\n },\n {\n \x1b[34;01m"subkey2"\x1b[39;00m: [\n \x1b[35m1\x1b[39m,\n \x1b[35m2\x1b[39m,\n \x1b[35m3\x1b[39m\n ]\n }\n ],\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[30;01mtrue\x1b[39;00m\n}'
]
else:
expected_output = [
'{\n \x1b[34;01m"key1"\x1b[39;00m: \x1b[32m"value1"\x1b[39m,\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[35m2\x1b[39m,\n \x1b[34;01m"key3"\x1b[39;00m: \x1b[90mnull\x1b[39m,\n \x1b[34;01m"key4"\x1b[39;00m: \x1b[35m3.14\x1b[39m,\n \x1b[34;01m"key5"\x1b[39;00m: \x1b[90mtrue\x1b[39m\n}',
'{\n \x1b[34;01m"key1"\x1b[39;00m: [\n {\n \x1b[34;01m"subkey1"\x1b[39;00m: \x1b[32m"subvalue1"\x1b[39m\n },\n {\n \x1b[34;01m"subkey2"\x1b[39;00m: [\n \x1b[35m1\x1b[39m,\n \x1b[35m2\x1b[39m,\n \x1b[35m3\x1b[39m\n ]\n }\n ],\n \x1b[34;01m"key2"\x1b[39;00m: \x1b[90mtrue\x1b[39m\n}'
]
for test_dict, expected_json in zip(test_input, expected_output):
cli = JcCli()
cli.pretty = True
cli.set_custom_colors()
cli.data_out = test_dict
self.assertEqual(cli.json_out(), expected_json)
def test_cli_yaml_out(self): def test_cli_yaml_out(self):
test_input = [ if PYGMENTS_INSTALLED:
None, test_input = [
{}, None,
[], {},
'', [],
{"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True}, '',
] {"key1": "value1", "key2": 2, "key3": None, "key4": 3.14, "key5": True},
if pygments.__version__.startswith('2.3.'):
expected_output = [
'---\n...',
'--- {}',
'--- []',
"--- \x1b[32m'\x1b[39m\x1b[32m'\x1b[39m",
'---\nkey1: value1\nkey2: 2\nkey3:\nkey4: 3.14\nkey5: true'
]
else:
expected_output = [
'---\n...',
'--- {}',
'--- []',
"--- \x1b[32m'\x1b[39m\x1b[32m'\x1b[39m",
'---\n\x1b[34;01mkey1\x1b[39;00m: value1\n\x1b[34;01mkey2\x1b[39;00m: 2\n\x1b[34;01mkey3\x1b[39;00m:\n\x1b[34;01mkey4\x1b[39;00m: 3.14\n\x1b[34;01mkey5\x1b[39;00m: true'
] ]
for test_dict, expected_json in zip(test_input, expected_output): if pygments.__version__.startswith('2.3.'):
cli = JcCli() expected_output = [
os.environ["JC_COLORS"] = "default,default,default,default" '---\n...',
cli.set_custom_colors() '--- {}',
cli.data_out = test_dict '--- []',
self.assertEqual(cli.yaml_out(), expected_json) "--- \x1b[32m'\x1b[39m\x1b[32m'\x1b[39m",
'---\nkey1: value1\nkey2: 2\nkey3:\nkey4: 3.14\nkey5: true'
]
else:
expected_output = [
'---\n...',
'--- {}',
'--- []',
"--- \x1b[32m'\x1b[39m\x1b[32m'\x1b[39m",
'---\n\x1b[34;01mkey1\x1b[39;00m: value1\n\x1b[34;01mkey2\x1b[39;00m: 2\n\x1b[34;01mkey3\x1b[39;00m:\n\x1b[34;01mkey4\x1b[39;00m: 3.14\n\x1b[34;01mkey5\x1b[39;00m: true'
]
for test_dict, expected_json in zip(test_input, expected_output):
cli = JcCli()
os.environ["JC_COLORS"] = "default,default,default,default"
cli.set_custom_colors()
cli.data_out = test_dict
self.assertEqual(cli.yaml_out(), expected_json)
def test_cli_yaml_out_mono(self): def test_cli_yaml_out_mono(self):
test_input = [ test_input = [