Files
rosetta/tools/fixtures/ini.py
T

63 lines
2.4 KiB
Python

# data/ini fixtures: random configs written by Python's configparser (interpolation off, key case kept), and
# hand-written messy files (comments, ':' delimiters, continuation lines, odd spacing, duplicates) with what
# configparser(strict=False) reads from them.
# tests/gen/ini.luau { written = { { data, canon } }, read = { { data, canon } } }
import configparser, importlib.util, io, os
_spec = importlib.util.spec_from_file_location("_data_common", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_data_common.py"))
C = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(C)
def parser():
p = configparser.ConfigParser(interpolation=None, strict=False)
p.optionxform = str
return p
def as_dict(p):
return {s: dict(p.items(s, raw=True)) for s in p.sections()}
MESSY = [
"[a]\nx=1\ny : two words \n; comment\n # indented comment\nz =\n",
"[s]\nkey = line one\n line two\n\tline three\nk2: v\n",
"# top comment\n[one]\na = 1\n[two]\nb = 2\n[one]\nc = 3\n",
"[dup]\na = 1\na = 2\n",
"[weird name with spaces]\n key with spaces = value with = sign \n",
"[s]\nurl = http://x:8080/p?a=b\npath: C:\\dir\n",
"[x]\r\nwin = yes\r\nmore = no\r\n",
"[\u00e9]\n\u4e2d = \u00fc\n",
"[a]\nk = v ; not a comment by default\n",
"[e]\nempty =\nalso:\n",
]
def generate(ctx):
rng = ctx.rng(17)
written = []
alph = "ab xyz09_-.\u00e9/=:;#"
for i in range(80):
p = parser()
for s in range(rng.randrange(1, 5)):
name = "sec%d %s" % (s, rng.choice(["", "x", "\u00e9"]))
p.add_section(name)
for k in range(rng.randrange(0, 6)):
key = "k%d%s" % (k, rng.choice(["", "_a", ".b"]))
val = "".join(rng.choice(alph) for _ in range(rng.randrange(0, 12))).strip()
if rng.random() < 0.15:
val = val + "\nsecond line" + rng.choice(["", "\nthird"])
p.set(name, key, val)
buf = io.StringIO()
p.write(buf)
data = buf.getvalue()
back = parser()
back.read_string(data)
written.append({"data": data.encode(), "canon": C.canon(as_dict(back))})
read = []
for m in MESSY:
p = parser()
p.read_string(m)
read.append({"data": m.encode(), "canon": C.canon(as_dict(p))})
ctx.emit("ini", {"written": written, "read": read})