Debian Codegraph @

deps/ada-toml/test-importers/burntsushi-toml-test.py

raw
1 #! /usr/bin/env python3
2
3 """
4 Import tests from <https://github.com/BurntSushi/toml-test/>.
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-test repository as its first argument.
8 """
9
10 import json
11 import os.path
12 import shutil
13 import sys
14
15 import yaml
16
17
18 burntsushi_root = sys.argv[1]
19 tests_subdir = os.path.join(burntsushi_root, "tests")
20 output_dir = os.path.join(os.getcwd(), 'tests', 'burntsushi-toml-test')
21 os.mkdir(output_dir)
22
23
24 def in_path(*args):
25 return os.path.join(burntsushi_root, *args)
26
27
28 def out_path(*args):
29 return os.path.join(output_dir, *args)
30
31
32 def create_test_name(toml_file):
33 relname = os.path.relpath(toml_file, tests_subdir)
34 return relname[:-5]
35
36
37 def write_test_yaml(test_dir, content):
38 with open(os.path.join(test_dir, 'test.yaml'), 'w', encoding='utf-8') as f:
39 yaml.dump(content, f)
40
41
42 def import_valid(toml_file):
43 assert toml_file.endswith('.toml')
44 test_name = create_test_name(toml_file)
45 json_file = toml_file[:-5] + '.json'
46
47 # Create the test directory
48 test_dir = out_path(test_name)
49 os.makedirs(test_dir)
50
51 # Copy the TOML to parse
52 shutil.copy(toml_file, os.path.join(test_dir, 'input.toml'))
53
54 # Create the test.yaml, including the expected JSON
55 with open(json_file, 'r', encoding='utf-8') as f:
56 json_doc = json.load(f)
57 write_test_yaml(test_dir, {"driver": "decoder", "output": json_doc})
58
59
60 def import_invalid(toml_file):
61 assert toml_file.endswith('.toml')
62 test_name = create_test_name(toml_file)
63
64 # Create the test directory
65 test_dir = out_path(test_name)
66 os.makedirs(test_dir)
67
68 # Copy the TOML to parse
69 shutil.copy(toml_file, os.path.join(test_dir, 'input.toml'))
70
71 # Create the test.yaml
72 write_test_yaml(test_dir, {"driver": "decoder", "error": True})
73
74
75 def iter_tests(root_dir):
76 for path, _, filenames in os.walk(root_dir):
77 for fn in filenames:
78 if fn.endswith(".toml"):
79 yield os.path.join(path, fn)
80
81
82 for valid in iter_tests(in_path('tests', 'valid')):
83 import_valid(valid)
84
85 for valid in iter_tests(in_path('tests', 'invalid')):
86 import_invalid(valid)