1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-06 22:32:54 +02:00

add ini-dup tests

This commit is contained in:
Kelly Brazil
2023-01-10 16:06:05 -08:00
parent 5c5ff9324f
commit ea4332d8e4
9 changed files with 111 additions and 4 deletions

View File

@ -6,7 +6,10 @@
jc - JSON Convert INI with duplicate key file parser
Parses standard INI files and preserves duplicate values. All values are
contained in lists/arrays. Multi-line values are not supported.
contained in lists/arrays.
If multi-line values are used, each line will be a separate item in the
value list. Blank lines in multi-line values are not supported.
- Delimiter can be `=` or `:`. Missing values are supported.
- Comment prefix can be `#` or `;`. Comments must be on their own line.

View File

@ -109,6 +109,7 @@ __version__ = info.version
class MultiDict(dict):
# https://stackoverflow.com/a/38286559/12303989
def __setitem__(self, key, value):
if key in self:
if isinstance(value, list):

View File

@ -1,4 +1,4 @@
.TH jc 1 2023-01-08 1.22.5 "JSON Convert"
.TH jc 1 2023-01-10 1.22.5 "JSON Convert"
.SH NAME
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings
.SH SYNOPSIS

View File

@ -0,0 +1 @@
{"client":{"user":["foo"],"host":["localhost"],"password":["bar"]}}

View File

@ -0,0 +1 @@
{"Settings":{"DetailedLog":["1"],"RunStatus":["1"],"StatusPort":["6090"],"StatusRefresh":["10"],"Archive":["1"],"LogFile":["/opt/ecs/mvuser/MV_IPTel/log/MV_IPTel.log"],"Version":["0.9 Build 4 Created July 11 2004 14:00"],"ServerName":["Unknown"]},"FTP":{"RunFTP":["1"],"FTPPort":["21"],"FTPDataPort":["20"],"FTPDir":["/opt/ecs/mvuser/MV_IPTel/data/FTPdata"],"FTP_TimeOut":["5"],"EnableSU":["1"],"SUUserName":["mvuser"],"SUPassword":["Avaya"]},"FTPS":{"RunFTPS":["0"],"FTPPort":["990"],"FTPDataPort":["889"]},"TFTP":{"RunTrivialFTP":["1"],"TrivialFTPPort":["69"],"TFTPDir":["/opt/ecs/mvuser/MV_IPTel/data/TFTPdata"]},"HTTP":{"RunHTTP":["1"],"HTTPPort":["81"],"HTTPDir":["/opt/ecs/mvuser/MV_IPTel/data/HTTPdata"]},"HTTPS":{"RunHTTPS":["0"],"HTTPSPort":["411"],"HTTPSDir":["/opt/ecs/mvuser/MV_IPTel/data/HTTPSdata"],"CertFile":["/opt/ecs/mvuser/MV_IPTel/certs/IPTelcert.pem"],"KeyFile":["/opt/ecs/mvuser/MV_IPTel/certs/IPTelkey.pem"],"ClientAuth":["0"],"IPTel":["0"],"SSLV2":["0"],"SSLV3":["0"],"TLSV1":["1"],"UseProxy":["0"],"ProxyAddr":["simon.avaya.com"],"ProxyPort":["9000"]},"BACKUP_SERVERS":{"FileServer":["0"],"RequestUpdates":["0"],"RequestBackup":["0"],"UsePrimarySvr":["0"],"PrimaryIP":["192.168.0.13"],"UseSecondarySvr":["0"],"SecondaryIP":["192.168.0.10"],"UpdateInterval":["2"],"CustomFTP":["1"],"CustomFTPDir":["home/mvuser/backup"],"CustomFTPUName":["tom"],"CustomFTPPwd":["jerry"],"CDRBackup":["0"],"BCMSBackup":["0"],"RetainDays":["7.0"]},"SNMP":{"UseSNMP":["1"]}}

View File

@ -0,0 +1 @@
{"client":{"user":["foo"],"host":["localhost"],"password":["bar"]}}

View File

@ -0,0 +1 @@
{"DEFAULT":{"ServerAliveInterval":["45"],"Compression":["yes"],"CompressionLevel":["9"],"ForwardX11":["yes"]},"bitbucket.org":{"User":["hg"]},"topsecret.server.com":{"Port":["50022"],"ForwardX11":["no"]}}

View File

@ -68,8 +68,7 @@ duplicate_key = value2
def test_ini_missing_top_section(self):
"""
Test INI file missing top-level section header. Should output a fake
section header called `_top_level_section_`
Test INI file missing top-level section header.
"""
data = '''
key: value1

100
tests/test_ini_dup.py Normal file
View File

@ -0,0 +1,100 @@
import os
import unittest
import json
import jc.parsers.ini_dup
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MyTests(unittest.TestCase):
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-test.ini'), 'r', encoding='utf-8') as f:
generic_ini_test = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-iptelserver.ini'), 'r', encoding='utf-8') as f:
generic_ini_iptelserver = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-double-quote.ini'), 'r', encoding='utf-8') as f:
generic_ini_double_quote = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-single-quote.ini'), 'r', encoding='utf-8') as f:
generic_ini_single_quote = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-dup-test.json'), 'r', encoding='utf-8') as f:
generic_ini_dup_test_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-dup-iptelserver.json'), 'r', encoding='utf-8') as f:
generic_ini_dup_iptelserver_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-dup-double-quote.json'), 'r', encoding='utf-8') as f:
generic_ini_dup_double_quote_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-dup-single-quote.json'), 'r', encoding='utf-8') as f:
generic_ini_dup_single_quote_json = json.loads(f.read())
def test_ini_dup_nodata(self):
"""
Test the test ini file with no data
"""
self.assertEqual(jc.parsers.ini_dup.parse('', quiet=True), {})
def test_ini_dup_test(self):
"""
Test the test ini file
"""
self.assertEqual(jc.parsers.ini_dup.parse(self.generic_ini_test, quiet=True), self.generic_ini_dup_test_json)
def test_ini_dup_iptelserver(self):
"""
Test the iptelserver ini file
"""
self.assertEqual(jc.parsers.ini_dup.parse(self.generic_ini_iptelserver, quiet=True), self.generic_ini_dup_iptelserver_json)
def test_ini_dup_duplicate_keys(self):
"""
Test input that contains duplicate keys.
"""
data = '''
[section]
duplicate_key: value1
another_key = foo
duplicate_key = value2
'''
expected = {"section":{"duplicate_key":["value1","value2"],"another_key":["foo"]}}
self.assertEqual(jc.parsers.ini_dup.parse(data, quiet=True), expected)
def test_ini_dup_missing_top_section(self):
"""
Test INI file missing top-level section header.
"""
data = '''
key: value1
another_key = foo
[section2]
key3: bar
key4 =
[section 3]
key5 = "quoted"
'''
expected = {"key":["value1"],"another_key":["foo"],"section2":{"key3":["bar"],"key4":[""]},"section 3":{"key5":["quoted"]}}
self.assertEqual(jc.parsers.ini_dup.parse(data, quiet=True), expected)
def test_ini_dup_doublequote(self):
"""
Test ini file with double quotes around a value
"""
self.assertEqual(jc.parsers.ini_dup.parse(self.generic_ini_double_quote, quiet=True), self.generic_ini_dup_double_quote_json)
def test_ini_dup_singlequote(self):
"""
Test ini file with single quotes around a value
"""
self.assertEqual(jc.parsers.ini_dup.parse(self.generic_ini_single_quote, quiet=True), self.generic_ini_dup_single_quote_json)
if __name__ == '__main__':
unittest.main()