File: test_parser.py

package info (click to toggle)
requirement-parser 0.2.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 376 kB
  • sloc: python: 359; makefile: 148
file content (58 lines) | stat: -rw-r--r-- 1,745 bytes parent folder | download
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
import os
import json
from nose.tools import raises, assert_equal
import warnings

from requirements import parse

REQFILE_DIR = os.path.join(os.path.dirname(__file__), 'reqfiles')


def listify(iterable):
    out = []
    for item in iterable:
        if isinstance(item, dict):
            for key, value in item.items():
                if isinstance(item[key], (tuple, list)):
                    if key in ('extras', 'specs'):
                        # enforce predictability
                        item[key] = sorted(listify(value))
                    else:
                        item[key] = listify(value)
        elif isinstance(item, (tuple, list)):
            item = listify(item)
        out.append(item)
    return out


def test_requirement_files():
    for fn in os.listdir(REQFILE_DIR):
        def fancy(f):
            f.description = "%s.%s: %s" % (f.__module__, f.__name__, fn)
            return f

        @fancy
        @raises(ValueError)
        def check_fail(s):
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                list([dict(r) for r in parse(s)])

        @fancy
        def check(s, expected):
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                assert_equal(listify(dict(r) for r in parse(s)), expected)

        fp = os.path.join(REQFILE_DIR, fn)

        # skip ".expected" files
        if not os.path.isfile(fp) or not fp.endswith('.txt'):
            continue

        if 'fail' in fn:
            yield check_fail, open(fp)
        else:
            with open(fp[:-4] + '.expected', 'r') as f2:
                expected = json.loads(f2.read())
                yield check, open(fp), expected