File: test_helpers.py

package info (click to toggle)
python-sybil 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,148 kB
  • sloc: python: 4,510; makefile: 90
file content (50 lines) | stat: -rw-r--r-- 1,449 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
import sys
from importlib import import_module
from pathlib import Path

import pytest

from sybil.python import import_cleanup
from .helpers import Finder, ast_docstrings


def test_finder_present_but_is_not_present():
    finder = Finder('foo\nbaz\n')
    with pytest.raises(AssertionError) as info:
        finder.assert_present('bob')
    assert str(info.value) == "foo\nbaz\n\n'foo\\nbaz\\n'"


def test_finder_not_present_but_is_present():
    finder = Finder('foo baz bar')
    with pytest.raises(AssertionError) as info:
        finder.assert_not_present('baz')
    assert str(info.value) == '\nfoo baz bar'


def test_import_cleanup(tmp_path: Path):
    (tmp_path / 'some_module.py').write_text('import unittest\nfoo = 1')
    (tmp_path / 'other_module.py').write_text('import other_module\nfoo = 1')

    initial_modules = sys.modules.copy()
    initial_path = sys.path.copy()

    with import_cleanup():
        sys.path.append(str(tmp_path))
        some_module = import_module('some_module')
        assert some_module.foo == 1

    assert sys.modules == initial_modules
    assert sys.path == initial_path


def test_all_python_files(all_python_files):
    count = len(all_python_files)
    assert count > 50, count


def test_ast_docstrings(all_python_files):
    seen_docstrings = 0
    for _, source in all_python_files:
        seen_docstrings += len(tuple(ast_docstrings(source)))
    assert seen_docstrings > 50, seen_docstrings