-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmd_testsuite.py
107 lines (94 loc) · 3.53 KB
/
md_testsuite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Shared functionality.
"""
import imp
import os
# Default config values here.
config_file_noext = 'config_local'
config_file = config_file_noext + '.py'
config = dict(
gfm_oauth_token = '',
# GFM off by default because it is too slow.
run_all_disable = ['gfm'],
timeout = 5
)
try:
config_custom = imp.load_source(config_file_noext, config_file).config
config.update(config_custom)
except IOError:
# Config file not present.
pass
# Encoding of all out outputs when we can chose it, hopefully equal to most inputs.
encoding = 'utf-8'
in_ext = u".md"
out_ext = u".out"
test_dir = u"tests"
engines_dir = os.path.join(test_dir, u"extensions")
# Output UTF-8 by default.
import sys
reload(sys)
sys.setdefaultencoding(encoding)
def same_basename_on_parent(path):
path_split = path.split(os.sep)
return os.sep.join(path_split[0:-2] + [path_split[-1]])
def io_iterator():
"""
Iterator over all input output pairs of the Original markdown, no engines.
Each yield returns a 3-tuple `(path, input, output)` where:
- `path` is the path of the test relative to the `test_dir` and without engine (`.md` or `.out`).
- `input` and `output` are the content of the input and output files.
"""
for basename in sorted(os.listdir(test_dir)):
input_path = os.path.join(test_dir, basename)
if os.path.isfile(input_path):
path_noext, ext = os.path.splitext(input_path)
if ext == in_ext:
with open(input_path, "r") as input_file:
input = input_file.read().decode(encoding)
output_path = path_noext + out_ext
with open(output_path, "r") as output_file:
output = output_file.read().decode(encoding)
yield (os.path.splitext(basename)[0], input, output)
def io_iterator_engine(id):
"""
Iterator over all input output pairs of a given engine.
Original markdown is not included.
"""
engine_dir = os.path.join(engines_dir, id)
for basename in sorted(os.listdir(engine_dir)):
input_path = os.path.join(engine_dir, basename)
if os.path.isfile(input_path):
path_noext, ext = os.path.splitext(input_path)
if ext == in_ext:
# Get input
with open(input_path, "r") as input_file:
input = input_file.read().decode(encoding)
if not input:
with open(same_basename_on_parent(input_path), "r") as input_file:
input = input_file.read().decode(encoding)
# Get output
output_path = path_noext + out_ext
if not os.path.exists(output_path):
output_path = same_basename_on_parent(output_path)
with open(output_path, "r") as output_file:
output = output_file.read().decode(encoding)
yield (os.path.splitext(basename)[0], input, output)
def get_engine_ids():
"""
Returns a sorted list of ids of all supported engines
based on the directories present under the engines directory.
"""
output = []
for basename in sorted(os.listdir(engines_dir)):
path = os.path.join(engines_dir, basename)
if os.path.isdir(path):
output.append(basename)
return output
def io_iterator_all_engines():
"""
Iterator over all input output pairs of a all engines
Original markdown is not included.
"""
for id in get_engine_ids():
for io in io_iterator_engine(id):
yield io