Skip to content

Commit

Permalink
Merge pull request #369 from eduzen/pytest-infra
Browse files Browse the repository at this point in the history
Refactor test infra
  • Loading branch information
facundobatista committed May 29, 2019
2 parents c6a6fdf + e319ff1 commit 87b2f7a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -5,6 +5,7 @@ Berenice Larsen Pereyra <berelarsenp@gmail.com>
David Litvak Bruno <david.litvakb@gmail.com>
Diego Duncan <diegoduncan21@gmail.com>
Diego Mascialino <dmascialino@gmail.com>
Eduardo Enriquez <eduardo.a.enriquez@gmail.com>
Facundo Batista <facundo@taniquetil.com.ar>
FaQ <facundofc@gmail.com>
Filipe Ximenes <filipeximenes@gmail.com>
Expand Down
11 changes: 11 additions & 0 deletions tests/__init__.py
Expand Up @@ -44,3 +44,14 @@ def create_tempfile(testcase, lines):
f.write(line + '\n')

return tempfile


def get_python_filepaths(roots):
"""Helper to retrieve paths of Python files."""
python_paths = []
for root in roots:
for dirpath, dirnames, filenames in os.walk(root):
for filename in filenames:
if filename.endswith(".py"):
python_paths.append(os.path.join(dirpath, filename))
return python_paths
68 changes: 28 additions & 40 deletions tests/test_infra.py
Expand Up @@ -18,18 +18,15 @@

import io
import logging
import os
import unittest

from unittest.mock import patch

import docutils.core
import pep257
import rst2html5_

from flake8.api.legacy import get_style_guide
from pyuca import Collator

from tests import get_python_filepaths

FLAKE8_ROOTS = ['fades', 'tests']
FLAKE8_OPTIONS = ['--max-line-length=99', '--select=E,W,F,C,N']
PEP257_ROOTS = ['fades']
Expand All @@ -40,44 +37,35 @@
logging.getLogger(logger_name).setLevel(logging.CRITICAL)


class InfrastructureTestCase(unittest.TestCase):
def test_flake8_pytest(mocker):
python_filepaths = get_python_filepaths(FLAKE8_ROOTS)
style_guide = get_style_guide(paths=FLAKE8_OPTIONS)
fake_stdout = io.StringIO()
mocker.patch('sys.stdout', fake_stdout)
report = style_guide.check_files(python_filepaths)
assert report.total_errors == 0, "There are issues!\n" + fake_stdout.getvalue()


def _get_python_filepaths(self, roots):
"""Helper to retrieve paths of Python files."""
python_paths = []
for root in roots:
for dirpath, dirnames, filenames in os.walk(root):
for filename in filenames:
if filename.endswith(".py"):
python_paths.append(os.path.join(dirpath, filename))
return python_paths
def test_pep257_pytest():
python_filepaths = get_python_filepaths(PEP257_ROOTS)
result = list(pep257.check(python_filepaths))
assert len(result) == 0, "There are issues!\n" + '\n'.join(map(str, result))

def test_flake8(self):
python_filepaths = self._get_python_filepaths(FLAKE8_ROOTS)
style_guide = get_style_guide(paths=FLAKE8_OPTIONS)
fake_stdout = io.StringIO()
with patch('sys.stdout', fake_stdout):
report = style_guide.check_files(python_filepaths)
self.assertEqual(report.total_errors, 0, "There are issues!\n" + fake_stdout.getvalue())

def test_pep257(self):
python_filepaths = self._get_python_filepaths(PEP257_ROOTS)
result = list(pep257.check(python_filepaths))
self.assertEqual(len(result), 0, "There are issues!\n" + '\n'.join(map(str, result)))
def test_readme_sanity(mocker):
fake_stdout = io.StringIO() # just to ignore the output
fake_stderr = io.StringIO() # will have content if there are problems
with open('README.rst', 'rt', encoding='utf8') as fh:
mocker.patch('sys.stdout', fake_stdout)
mocker.patch('sys.stderr', fake_stderr)
docutils.core.publish_file(source=fh, writer=rst2html5_.HTML5Writer())

def test_readme_sanity(self):
fake_stdout = io.StringIO() # just to ignore the output
fake_stderr = io.StringIO() # will have content if there are problems
with open('README.rst', 'rt', encoding='utf8') as fh:
with patch('sys.stdout', fake_stdout):
with patch('sys.stderr', fake_stderr):
docutils.core.publish_file(source=fh, writer=rst2html5_.HTML5Writer())
errors = fake_stderr.getvalue()
assert not bool(errors), "There are issues!\n" + errors

errors = fake_stderr.getvalue()
self.assertFalse(bool(errors), "There are issues!\n" + errors)

def test_authors_ordering(self):
with open('AUTHORS', 'rt', encoding='utf8') as fh:
authors = fh.readlines()
ordered_authors = sorted(authors, key=Collator().sort_key)
self.assertEqual(authors, ordered_authors)
def test_authors_ordering():
with open('AUTHORS', 'rt', encoding='utf8') as fh:
authors = fh.readlines()
ordered_authors = sorted(authors, key=Collator().sort_key)
assert authors == ordered_authors

0 comments on commit 87b2f7a

Please sign in to comment.