Skip to content

Commit

Permalink
convert test suite to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
xmo-odoo authored and peterjc committed Dec 9, 2021
1 parent 1455989 commit e3dee19
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 60 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install package
- name: Install package and pytest
run: |
python setup.py install
pip install pytest
- name: Run tests
run: |
echo "On this machine \$LANG=$LANG"
echo "Checking our own docstrings are valid RST"
flake8 --select RST setup.py flake8_rst_docstrings.py
cd tests/
./run_tests.sh
pytest --verbose
57 changes: 0 additions & 57 deletions tests/run_tests.sh

This file was deleted.

106 changes: 106 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Test suite."""
import glob
import os.path
from os.path import join
from subprocess import Popen, PIPE, DEVNULL

import pytest


def pytest_generate_tests(metafunc):
"""Handle listing and generating the ``expected_failure`` test cases."""
if "expected_failure" in metafunc.fixturenames:
modpath = os.path.dirname(metafunc.module.__file__)
pattern = os.path.join(modpath, "RST???", "*.py")
metafunc.parametrize(
"expected_failure",
[os.path.relpath(p, modpath) for p in sorted(glob.glob(pattern))],
)


@pytest.fixture(scope="module")
def modpath(request):
"""Provide the path of the module being run, for access to test files."""
return os.path.dirname(request.module.__file__)


def flake8(path, *, select="RST", roles=None, directives=None):
"""Run flake8 on a specific ``path`` and report the results.
:param str path: path to run flake8 on
:param str select: error codes to check, defaults to ``RST``
:param str roles: comma-separated list of roles to ignore (for ``RST304``)
:param str directives: comma-separated list of directives to ignore
(for ``RST303``)
:returns: a pair of flake8"s return code and its decoded stdout
:rtype: tuple[int, str]
"""
args = ["flake8", "--select", select]
if roles:
args.extend(["--rst-roles", roles])
if directives:
args.extend(["--rst-directives", directives])
args.append(path)

p = Popen(args, stdin=DEVNULL, stdout=PIPE)
out, _ = p.communicate()
return p.returncode, out.decode()


def test_expected_failures(modpath, expected_failure):
"""Check a single expected failure (negative test case).
Runs flake8 on ``expected_failure`` which should be of the form
:samp:`{code}/{file}`.
Validates that the run fails, and outputs the expected error code.
"""
code = os.path.dirname(expected_failure)
retcode, out = flake8(join(modpath, expected_failure))
assert retcode, "expected failure (%s), got success" % code
needle = ": %s " % code
assert needle in out


def test_expected_successes(modpath):
"""Verify the positive test cases (expected successes)."""
retcode, out = flake8(join(modpath, "test_cases"))
assert not retcode, out


def test_extra_directives(modpath):
"""Verify that ``RST303`` can be fixed by specifying directives to allow."""
retcode, out = flake8(
join(modpath, "RST303/sphinx-directives"),
directives="req,spec,needfilter",
)
assert not retcode, out


def test_extra_roles(modpath):
"""Verify that ``RST304`` can be fixed by specifying roles to allow."""
retcode, out = flake8(
join(modpath, "RST304/sphinx-roles.py"),
roles="need,need_incoming",
)
assert not retcode, out


def test_help():
"""Check that ``rst-docstrings``" options show up in the help text."""
p = Popen(["flake8", "-h"], stdin=DEVNULL, stdout=PIPE)
out, err = p.communicate()

assert not p.returncode, err
assert b" --rst-" in out, "rst options should appear in help text"
assert not err


def test_version():
"""Check that ``rst-docstrings`` appears in the help text."""
p = Popen(["flake8", "--version"], stdin=DEVNULL, stdout=PIPE)
out, err = p.communicate()

assert not p.returncode, err
assert b"rst-docstrings:" in out, "should appear in flake8 version string"
assert not err

0 comments on commit e3dee19

Please sign in to comment.