| 1 |
#! /usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
Import tests from <https://github.com/iarna/toml-spec-tests/>. |
| 5 |
|
| 6 |
Run this script from the top-level directory in ada-toml's repository, passing |
| 7 |
to it the path to a checkout of the toml-spec-test repository as its first |
| 8 |
argument. |
| 9 |
""" |
| 10 |
|
| 11 |
import datetime |
| 12 |
import glob |
| 13 |
import json |
| 14 |
import os.path |
| 15 |
import shutil |
| 16 |
import sys |
| 17 |
|
| 18 |
import yaml |
| 19 |
|
| 20 |
|
| 21 |
# Some test YAML documents create big recursions in the YAML parser |
| 22 |
sys.setrecursionlimit(10000) |
| 23 |
|
| 24 |
|
| 25 |
iarna_root = sys.argv[1] |
| 26 |
output_dir = os.path.join(os.getcwd(), 'tests', 'iarna-toml-spec-tests') |
| 27 |
os.mkdir(output_dir) |
| 28 |
|
| 29 |
|
| 30 |
def in_path(*args): |
| 31 |
return os.path.join(iarna_root, *args) |
| 32 |
|
| 33 |
|
| 34 |
def out_path(*args): |
| 35 |
return os.path.join(output_dir, *args) |
| 36 |
|
| 37 |
|
| 38 |
def create_test_name(toml_file): |
| 39 |
return os.path.basename(toml_file)[:-5] |
| 40 |
|
| 41 |
|
| 42 |
def write_test_yaml(test_dir, content): |
| 43 |
with open(os.path.join(test_dir, 'test.yaml'), 'w', encoding='utf-8') as f: |
| 44 |
print(content, file=f) |
| 45 |
|
| 46 |
|
| 47 |
def yaml_to_json(yaml): |
| 48 |
""" |
| 49 |
Turn a YAML document to the equivalent JSON document that ada_toml_decode |
| 50 |
program will output. |
| 51 |
""" |
| 52 |
if isinstance(yaml, dict): |
| 53 |
return {key: yaml_to_json(value) |
| 54 |
for key, value in yaml.items()} |
| 55 |
elif isinstance(yaml, list): |
| 56 |
return [yaml_to_json(item) for item in yaml] |
| 57 |
|
| 58 |
if yaml is False: |
| 59 |
kind = 'bool', |
| 60 |
image = 'false' |
| 61 |
elif yaml is True: |
| 62 |
kind = 'bool' |
| 63 |
image = 'true' |
| 64 |
elif isinstance(yaml, int): |
| 65 |
kind = 'integer' |
| 66 |
image = str(yaml) |
| 67 |
elif isinstance(yaml, str): |
| 68 |
kind = 'string' |
| 69 |
image = yaml |
| 70 |
elif isinstance(yaml, float): |
| 71 |
kind = 'float' |
| 72 |
image = str(yaml) |
| 73 |
elif isinstance(yaml, datetime.datetime): |
| 74 |
kind = 'local-datetime' |
| 75 |
image = '{:>04}-{:>02}-{:>02}T{:>02}:{:>02}:{:>02}'.format( |
| 76 |
yaml.year, yaml.month, yaml.day, |
| 77 |
yaml.hour, yaml.minute, yaml.second |
| 78 |
) |
| 79 |
if yaml.microsecond: |
| 80 |
image = '{}.{:>03}'.format(image, yaml.microsecond) |
| 81 |
else: |
| 82 |
raise ValueError('Unsupported YAML value ({}): {}' |
| 83 |
.format(type(yaml), yaml)) |
| 84 |
|
| 85 |
return {'type': kind, 'value': image} |
| 86 |
|
| 87 |
|
| 88 |
def import_valid(toml_file): |
| 89 |
assert toml_file.endswith('.toml') |
| 90 |
test_name = create_test_name(toml_file) |
| 91 |
yaml_file = os.path.join(os.path.dirname(toml_file), test_name + '.yaml') |
| 92 |
|
| 93 |
# Create the test directory |
| 94 |
test_dir = out_path('values', test_name) |
| 95 |
os.mkdir(test_dir) |
| 96 |
|
| 97 |
# Copy the TOML to parse |
| 98 |
shutil.copy(toml_file, os.path.join(test_dir, 'input.toml')) |
| 99 |
|
| 100 |
# Create the test.yaml, including the expected JSON |
| 101 |
try: |
| 102 |
with open(yaml_file, 'r', encoding='utf-8') as f: |
| 103 |
data = yaml.safe_load(f) |
| 104 |
except FileNotFoundError: |
| 105 |
print('warning: could not find YAML for {}'.format(toml_file)) |
| 106 |
return |
| 107 |
data = yaml_to_json(data) |
| 108 |
write_test_yaml(test_dir, |
| 109 |
'driver: decoder' |
| 110 |
'\noutput: {}'.format(json.dumps(data))) |
| 111 |
|
| 112 |
|
| 113 |
def import_invalid(toml_file): |
| 114 |
assert toml_file.endswith('.toml') |
| 115 |
test_name = create_test_name(toml_file) |
| 116 |
|
| 117 |
# Create the test directory |
| 118 |
test_dir = out_path('errors', test_name) |
| 119 |
os.mkdir(test_dir) |
| 120 |
|
| 121 |
# Copy the TOML to parse |
| 122 |
shutil.copy(toml_file, os.path.join(test_dir, 'input.toml')) |
| 123 |
|
| 124 |
# Create the test.yaml |
| 125 |
write_test_yaml(test_dir, |
| 126 |
'driver: decoder' |
| 127 |
'\nerror: True') |
| 128 |
|
| 129 |
|
| 130 |
os.mkdir(out_path('values')) |
| 131 |
for toml_file in glob.glob(in_path('values', '*.toml')): |
| 132 |
import_valid(toml_file) |
| 133 |
|
| 134 |
os.mkdir(out_path('errors')) |
| 135 |
for toml_file in glob.glob(in_path('errors', '*.toml')): |
| 136 |
import_invalid(toml_file) |