Exemplo n.º 1
0
def _pytest_integration_that_actually_works() -> typing.Callable:
    """
    Sybil matching is pretty broken. We'll have to help it out here. The problem is that
    exclude patterns passed into the Sybil object are matched against file name stems such that
    files cannot be excluded by path.
    """

    _excludes = [
        '**/markupsafe/*',
        '**/jinja2/*',
    ]

    _sy = Sybil(parsers=[
        DocTestParser(optionflags=ELLIPSIS),
        CodeBlockParser(),
    ],
                fixtures=['jinja_filter_tester'])

    def pytest_collect_file(parent: str,
                            path: str) -> typing.Optional[SybilFile]:
        if fnmatch(path, '**/nunavut/**/*.py') and not any(
                fnmatch(path, pattern) for pattern in _excludes):
            return SybilFile(path, parent, _sy)
        else:
            return None

    return pytest_collect_file
Exemplo n.º 2
0
def test_basic():
    document = document_from_sample('codeblock.txt')
    regions = list(CodeBlockParser()(document))
    assert len(regions) == 7
    namespace = document.namespace
    namespace['y'] = namespace['z'] = 0
    assert evaluate_region(regions[0], namespace) is None
    assert namespace['y'] == 1
    assert namespace['z'] == 0
    with pytest.raises(Exception) as excinfo:
        evaluate_region(regions[1], namespace)
    assert str(excinfo.value) == 'boom!'
    assert evaluate_region(regions[2], namespace) is None
    assert namespace['y'] == 1
    assert namespace['z'] == 1
    assert evaluate_region(regions[3], namespace) is None
    assert namespace['bin'] == b'x'
    assert namespace['uni'] == u'x'
    assert evaluate_region(regions[4], namespace) is None
    assert 'NoVars' in namespace
    assert evaluate_region(regions[5], namespace) is None
    assert namespace['define_this'] == 1
    assert evaluate_region(regions[6], namespace) is None
    assert 'YesVars' in namespace
    assert '__builtins__' not in namespace
Exemplo n.º 3
0
def _pytest_integration_that_actually_works() -> typing.Callable:
    """
    Sybil matching is pretty broken. We'll have to help it out here. The problem is that
    exclude patterns passed into the Sybil object are matched against file name stems such that
    files cannot be excluded by path.
    """

    _sy = Sybil(parsers=[
        DocTestParser(optionflags=ELLIPSIS),
        CodeBlockParser(),
    ],
                pattern='**/nunavut/**/*.py',
                excludes=[
                    '**/markupsafe/*',
                    '**/jinja2/*',
                ],
                fixtures=[
                    'jinja_filter_tester', 'gen_paths',
                    'assert_language_config_value',
                    'configurable_language_context_factory'
                ])

    def pytest_collect_file(parent: typing.Any,
                            path: typing.Any) -> typing.Optional[typing.Any]:
        if _sy.should_test_filename(str(path)):
            return NewSybilFile.from_parent(parent, fspath=path, sybil=_sy)
        else:
            return None

    return pytest_collect_file
Exemplo n.º 4
0
def test_windows_line_endings(tmp_path):
    p = tmp_path / "example.txt"
    p.write_bytes(b'This is my example:\r\n\r\n'
                  b'.. code-block:: python\r\n\r\n'
                  b'    from math import cos\r\n'
                  b'    x = 123\r\n\r\n'
                  b'That was my example.\r\n')
    document = Document.parse(str(p), CodeBlockParser())
    example, = document
    example.evaluate()
    assert document.namespace['x'] == 123
Exemplo n.º 5
0
def test_future_imports():
    document = document_from_sample('codeblock_future_imports.txt')
    regions = list(CodeBlockParser(['print_function'])(document))
    assert len(regions) == 2
    buffer = StringIO()
    namespace = {'buffer': buffer}
    assert evaluate_region(regions[0], namespace) is None
    assert buffer.getvalue() == ('pathalogical worst case for line numbers\n')
    # the future import line drops the firstlineno by 1
    assert regions[0].parsed.co_firstlineno == 2
    assert evaluate_region(regions[1], namespace) is None
    assert buffer.getvalue() == (
        'pathalogical worst case for line numbers\n'
        'still should work and have good line numbers\n')
    # the future import line drops the firstlineno by 1
    assert regions[1].parsed.co_firstlineno == 8
Exemplo n.º 6
0
def test_conditional_edge_cases():
    document = Document.parse(sample_path('skip-conditional-edges.txt'),
                              CodeBlockParser(), DocTestParser(), skip)
    document.namespace['sys'] = sys
    document.namespace['run'] = []
    skipped = []
    for example in document:
        try:
            example.evaluate()
        except SkipTest as e:
            skipped.append(str(e))
    assert document.namespace['run'] == [1, 2]
    # we should always have one and only one skip from this document.
    if PY3:
        assert skipped == ['only true on python 2']
    else:
        assert skipped == ['only true on python 3'] * 3
Exemplo n.º 7
0
        return message


def parse_for(letter, document):
    for m in re.finditer(r'(%s+) (\d+) check' % letter, document.text):
        yield Region(m.start(), m.end(), (m.group(1), int(m.group(2))),
                     partial(check, letter))


def sybil_setup(namespace):
    print('sybil setup', end=' ')
    namespace['x'] = 0


def sybil_teardown(namespace):
    print('sybil teardown', namespace['x'])


pytest_collect_file = Sybil(parsers=[
    partial(parse_for, 'X'),
    partial(parse_for, 'Y'),
    CodeBlockParser(['print_function'])
],
                            pattern='*.rst',
                            setup=sybil_setup,
                            teardown=sybil_teardown,
                            fixtures=[
                                'function_fixture', 'class_fixture',
                                'module_fixture', 'session_fixture'
                            ]).pytest()
Exemplo n.º 8
0
from doctest import ELLIPSIS

import pytest

from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser, FIX_BYTE_UNICODE_REPR
from sybil.parsers.skip import skip

pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(optionflags=ELLIPSIS | FIX_BYTE_UNICODE_REPR),
        CodeBlockParser(),
        skip,
    ],
    pattern='*.rst',
).pytest()
Exemplo n.º 9
0
import os
from doctest import ELLIPSIS, NORMALIZE_WHITESPACE

from scrapy.http.response.html import HtmlResponse
from sybil import Sybil
from sybil.parsers.codeblock import CodeBlockParser
from sybil.parsers.doctest import DocTestParser
from sybil.parsers.skip import skip


def load_response(url, filename):
    input_path = os.path.join(os.path.dirname(__file__), '_tests', filename)
    with open(input_path, 'rb') as input_file:
        return HtmlResponse(url, body=input_file.read())


def setup(namespace):
    namespace['load_response'] = load_response


pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(optionflags=ELLIPSIS | NORMALIZE_WHITESPACE),
        CodeBlockParser(future_imports=['print_function']),
        skip,
    ],
    pattern='*.rst',
    setup=setup,
).pytest()
Exemplo n.º 10
0
def test_basic():
    document = Document.parse(sample_path('skip.txt'), CodeBlockParser(), skip)
    for example in document:
        example.evaluate()
    assert document.namespace['run'] == [2, 5]