File: data_to_json.py

package info (click to toggle)
python-bleach 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,348 kB
  • sloc: python: 14,628; sh: 60; makefile: 51
file content (61 lines) | stat: -rwxr-xr-x 1,334 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

"""
Util to write a directory of test cases with input filenames
<testcase>.test as JSON to stdout.

example::

    $ python tests/data_to_json.py tests/data > testcases.json

"""

import argparse
import fnmatch
import json
import os
import os.path

import bleach


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "data_dir",
        help=("directory containing test cases with names like <testcase>.test"),
    )

    args = parser.parse_args()

    filenames = os.listdir(args.data_dir)
    ins = [
        os.path.join(args.data_dir, f)
        for f in filenames
        if fnmatch.fnmatch(f, "*.test")
    ]

    testcases = []
    for infn in ins:
        case_name = infn.rsplit(".test", 1)[0]

        with open(infn) as fin:
            data, expected = fin.read().split("\n--\n")
            data = data.strip()
            expected = expected.strip()

            testcases.append(
                {
                    "title": case_name,
                    "input_filename": infn,
                    "payload": data,
                    "actual": bleach.clean(data),
                    "expected": expected,
                }
            )

    print(json.dumps(testcases, indent=4, sort_keys=True))


if __name__ == "__main__":
    main()