File: test_filter.py

package info (click to toggle)
sphinxcontrib-spelling 7.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: python: 1,191; makefile: 151; sh: 27
file content (151 lines) | stat: -rw-r--r-- 3,666 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Tests for filters.
"""

import contextlib
import logging
import os
import sys

import pytest
from enchant.tokenize import get_tokenizer

from sphinxcontrib.spelling import filters # isort:skip
from tests import helpers # isort:skip

# Replace the sphinx logger with a normal one so pytest can collect
# the output.
filters.logger = logging.getLogger('test.filters')


def test_builtin_unicode():
    f = filters.PythonBuiltinsFilter(None)
    assert not f._skip('passé')


def test_builtin_regular():
    f = filters.PythonBuiltinsFilter(None)
    assert f._skip('print')


def test_acronym():
    text = 'a front-end for DBM-style databases'
    t = get_tokenizer('en_US', [])
    f = filters.AcronymFilter(t)
    words = [w[0] for w in f(text)]
    assert 'DBM' not in words, 'Failed to filter out acronym'


def test_acronym_unicode():
    text = 'a front-end for DBM-style databases'
    t = get_tokenizer('en_US', [])
    f = filters.AcronymFilter(t)
    words = [w[0] for w in f(text)]
    assert 'DBM' not in words, 'Failed to filter out acronym'


@helpers.require_git_repo
@pytest.mark.parametrize(
    "name",
    [
        "Alex",
        "Atlakson",
        "Avram",
        "Baumgold",
        "Berman",
        "Daniele",
        "Doug",
        "Finucane",
        "Gaynor",
        "Gonsiorowski",
        "Hong",
        "Hong",
        "Huon",
        "Kampik",
        "Kolosov",
        "Lubkin",
        "Marti",
        "Minhee",
        "Olausson",
        "Raggam",
        "Raudsepp",
        "sdelliot",
        "Sergey",
        "Sevilla",
        "Timotheus",
        "Tobias",
        "Tricoli",
    ]
)
def test_contributors(name):
    f = filters.ContributorFilter(None)
    assert f._skip(name)


@pytest.mark.parametrize(
    "word,expected",
    [
        ('os', True),
        ('os.name', False),
        ('__main__', False),
        ("don't", False),
    ]
)
def test_importable_module_skip(word, expected):
    f = filters.ImportableModuleFilter(None)
    assert f._skip(word) is expected


@contextlib.contextmanager
def import_path(new_path):
    "Temporarily change sys.path for imports."
    before = sys.path
    try:
        sys.path = new_path
        yield
    finally:
        sys.path = before


def test_importable_module_with_side_effets(tmpdir):
    logging.debug('tmpdir %r', tmpdir)
    logging.debug('cwd %r', os.getcwd())

    parentdir = tmpdir.join('parent')
    parentdir.mkdir()

    parentdir.join('__init__.py').write(
        'raise SystemExit("exit as side-effect")\n'
    )
    parentdir.join('child.py').write('')

    with import_path([str(tmpdir)] + sys.path):
        f = filters.ImportableModuleFilter(None)
        skip_parent = f._skip('parent')
        skip_both = f._skip('parent.child')

    # The parent module name is valid because it is not imported, only
    # discovered.
    assert skip_parent is True
    assert 'parent' in f.found_modules

    # The child module name is not valid because the parent is
    # imported to find the child and that triggers the side-effect.
    assert skip_both is False
    assert 'parent.child' not in f.found_modules


def test_importable_module_with_system_exit(tmpdir):
    path = tmpdir.join('mytestmodule.py')
    path.write('raise SystemExit("exit as side-effect")\n')

    with import_path([str(tmpdir)] + sys.path):
        f = filters.ImportableModuleFilter(None)
        skip = f._skip('mytestmodule')

    # The filter does not actually import the module in this case, so
    # it shows up as a valid word.
    assert skip is True
    assert 'mytestmodule' in f.found_modules