Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pytest as a test runner #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ jobs:
run: |
python -m pip install -U pip
python -m pip install -U setuptools wheel
python -m pip install -U coverage coveralls
python -m pip install -U coverage coveralls pytest
python -m pip install -e .

- name: Run tests
run: coverage run testsuite.py
run: coverage run -m pytest

- name: Check test coverage
run: |
Expand Down
13 changes: 5 additions & 8 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
include *.mk
include *.rst
include *.yml
include LICENSE
include Makefile
include .coveragerc
include .gitignore
include tox.ini
include testsuite.py
include pytest.ini
include conftest.py
include tests.py
include tests/*.txt
include tests/sample-tree/snake.egg-info
recursive-include tests/sample-tree *.py *.zip

# added by check_manifest.py
include *.yml
include LICENSE

# added by check_manifest.py
include *.mk
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Licence: MIT (https://mit-license.org/)

|buildstatus|_ |appveyor|_ |coverage|_

.. |buildstatus| image:: https://github.com/mgedmin/findimports/workflows/build/badge.svg?branch=master
.. |buildstatus| image:: https://github.com/mgedmin/findimports/actions/workflows/build.yml/badge.svg?branch=master
.. _buildstatus: https://github.com/mgedmin/findimports/actions

.. |appveyor| image:: https://ci.appveyor.com/api/projects/status/github/mgedmin/findimports?branch=master&svg=true
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ install:
- ps: if (-not (Test-Path $env:PYTHON)) { throw "No $env:PYTHON" }
- "set PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
- python --version
- pip install tox

build: off

test_script:
- python testsuite.py
- tox -e py
69 changes: 69 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import linecache
import os
import pathlib
import sys

import pytest


here = pathlib.Path(__file__).resolve().parent
sample_tree = pathlib.Path(here, 'tests/sample-tree')


class RedirectToStdout(object):
"""A file-like object that prints to sys.stdout

A reason to use sys.stderr = RedirectToStdout() instead of assigning
sys.stderr = sys.stdout is when sys.stdout is later reassigned to a
different object (e.g. the StringIO that doctests use) and you want
sys.stderr to always refer to whatever sys.stdout is printing to.

Not all file methods are implemented, just the ones that were actually
needed.
"""

def write(self, msg):
sys.stdout.write(msg)


class RewriteBackslashes(object):
"""A file-like object that normalizes path separators.

pytest doesn't allow custom doctest checkers, so I have to do terrible
crimes like this class.
"""

def __init__(self):
self.real_stdout = sys.stdout

def write(self, msg):
self.real_stdout.write(msg.replace(os.path.sep, '/'))


def create_tree(files):
f = None
try:
for line in files.splitlines():
if line.startswith('-- ') and line.endswith(' --'):
pathname = pathlib.Path(line.strip('- '))
pathname.parent.mkdir(parents=True, exist_ok=True)
if f is not None:
f.close()
f = pathname.open('w')
elif f is not None:
print(line, file=f)
finally:
if f is not None:
f.close()


@pytest.fixture(autouse=True)
def doctest_setup(doctest_namespace, tmp_path, monkeypatch):
doctest_namespace['create_tree'] = create_tree
doctest_namespace['sample_tree'] = str(sample_tree)
monkeypatch.syspath_prepend(str(sample_tree.joinpath('zippedmodules.zip')))
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, 'stderr', RedirectToStdout())
monkeypatch.setattr(sys, 'stdout', RewriteBackslashes())
yield
linecache.clearcache()
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = tests.py tests/*.txt
addopts = -ra -s
7 changes: 1 addition & 6 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import os
import unittest
from io import StringIO

import findimports


try:
from cStringIO import StringIO
except ImportError:
from io import StringIO


here = os.path.dirname(__file__)


Expand Down
2 changes: 0 additions & 2 deletions tests/cyclic-imports.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Test cyclic imports

>>> from testsuite import create_tree

>>> create_tree('''
... -- foo/__init__.py --
...
Expand Down
12 changes: 6 additions & 6 deletions tests/import-statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ All kinds of import statements are handled
>>> with open('marmalade.py', 'w') as f: _ = f.write('''
... import sys
... import os.path
... import email.Message as EM
... import email.message as EM
... from . import foobar
... from cStringIO import StringIO
... from cPickle import dumps as D
... from io import StringIO
... from pickle import dumps as D
... from sys import (argv,
... exc_info as EI, # complexity is fun
... exit)
Expand All @@ -23,10 +23,10 @@ All kinds of import statements are handled
... print(imp)
ImportInfo('sys', 'marmalade.py', 2, None)
ImportInfo('os.path', 'marmalade.py', 3, None)
ImportInfo('email.Message', 'marmalade.py', 4, None)
ImportInfo('email.message', 'marmalade.py', 4, None)
ImportInfo('foobar', 'marmalade.py', 5, 1)
ImportInfo('cStringIO.StringIO', 'marmalade.py', 6, 0)
ImportInfo('cPickle.dumps', 'marmalade.py', 7, 0)
ImportInfo('io.StringIO', 'marmalade.py', 6, 0)
ImportInfo('pickle.dumps', 'marmalade.py', 7, 0)
ImportInfo('sys.argv', 'marmalade.py', 8, 0)
ImportInfo('sys.exc_info', 'marmalade.py', 9, 0)
ImportInfo('sys.exit', 'marmalade.py', 10, 0)
Expand Down
2 changes: 0 additions & 2 deletions tests/package-graph.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Test collapsing to packages
===========================

>>> from testsuite import create_tree

>>> create_tree('''
... -- foo/__init__.py --
...
Expand Down
2 changes: 0 additions & 2 deletions tests/relative-imports.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Test handling of relative imports
=================================

>>> from testsuite import create_tree

>>> create_tree('''
... -- foo/__init__.py --
...
Expand Down
2 changes: 0 additions & 2 deletions tests/test-packages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ Test handling of test packages
>>> g.removeTestPackage('tests')
'tests'

>>> from testsuite import create_tree

>>> create_tree('''
... -- foo/__init__.py --
...
Expand Down
3 changes: 2 additions & 1 deletion tests/unknown-modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ We print warnings when we cannot find a module/package
>>> with open('marmalade.py', 'w') as f: _ = f.write('''
... import sys
... import os.path
... import email.Message as EM
... import email.message as EM
... from . import foobar
... from io import StringIO
... from pickle import dumps as D
Expand All @@ -17,6 +17,7 @@ We print warnings when we cannot find a module/package
... exit)
... from email import *
... import imaginary.package
... import curses.panel
... ''')

>>> with open('foo.py', 'w') as f: _ = f.write('''
Expand Down
96 changes: 0 additions & 96 deletions testsuite.py

This file was deleted.

16 changes: 12 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,38 @@ envlist =
py313
pypy3
flake8
ruff
isort
check-python-versions

[testenv]
deps =
pytest
commands =
python testsuite.py {posargs}
pytest {posargs}

[testenv:coverage]
deps =
pytest
coverage
commands =
coverage run testsuite.py
coverage run -m pytest
coverage report -m --fail-under=100

[testenv:flake8]
deps = flake8
skip_install = true
commands = flake8 findimports.py setup.py tests.py testsuite.py
commands = flake8 findimports.py setup.py tests.py conftest.py

[testenv:ruff]
deps = ruff
skip_install = true
commands = ruff check findimports.py setup.py tests.py conftest.py

[testenv:isort]
deps = isort
skip_install = true
commands = isort {posargs: -c --diff findimports.py setup.py tests.py testsuite.py}
commands = isort {posargs: -c --diff findimports.py setup.py tests.py conftest.py}

[testenv:check-manifest]
deps = check-manifest
Expand Down
Loading