File: utils.py

package info (click to toggle)
python-stack-data 0.6.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 664 kB
  • sloc: python: 2,204; sh: 29; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 1,074 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
import os

import pygments
from littleutils import string_to_file, file_to_string, json_to_file, file_to_json


def parse_version(version: str):
    return tuple(int(x) for x in version.split("."))


old_pygments = parse_version(pygments.__version__) < (2, 16, 1)


def compare_to_file(text, name):
    if old_pygments and "pygment" in name:
        return
    filename = os.path.join(
        os.path.dirname(__file__),
        'golden_files',
        name + '.txt',
    )
    if os.environ.get('FIX_STACK_DATA_TESTS'):
        string_to_file(text, filename)
    else:
        expected_output = file_to_string(filename)
        assert text == expected_output


def compare_to_file_json(data, name, *, pygmented):
    if old_pygments and pygmented:
        return
    filename = os.path.join(
        os.path.dirname(__file__),
        'golden_files',
        name + '.json',
    )
    if os.environ.get('FIX_STACK_DATA_TESTS'):
        json_to_file(data, filename, indent=4)
    else:
        expected_output = file_to_json(filename)
        assert data == expected_output