From 47ed4626e52e0674191ecbe795f5de4eb9d0ca87 Mon Sep 17 00:00:00 2001 From: "Eduardo Enriquez (eduzen)" Date: Thu, 23 May 2019 22:12:48 +0200 Subject: [PATCH 1/6] Refactor test infra --- AUTHORS | 1 + tests/test_infra.py | 68 +++++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/AUTHORS b/AUTHORS index 34e6452..c514925 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,6 +5,7 @@ Berenice Larsen Pereyra David Litvak Bruno Diego Duncan Diego Mascialino +Eduardo Enriquez Facundo Batista FaQ Filipe Ximenes diff --git a/tests/test_infra.py b/tests/test_infra.py index d1bfa20..713d2c7 100644 --- a/tests/test_infra.py +++ b/tests/test_infra.py @@ -18,11 +18,6 @@ import io import logging -import os -import unittest - -from unittest.mock import patch - import docutils.core import pep257 import rst2html5_ @@ -30,6 +25,8 @@ from flake8.api.legacy import get_style_guide from pyuca import Collator +from tests.conftest import _get_python_filepaths + FLAKE8_ROOTS = ['fades', 'tests'] FLAKE8_OPTIONS = ['--max-line-length=99', '--select=E,W,F,C,N'] PEP257_ROOTS = ['fades'] @@ -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 From 884ce62604b98788f8b4cdba154aa17dfaab5698 Mon Sep 17 00:00:00 2001 From: "Eduardo Enriquez (eduzen)" Date: Thu, 23 May 2019 22:19:37 +0200 Subject: [PATCH 2/6] Removed conftest --- tests/test_infra.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_infra.py b/tests/test_infra.py index 713d2c7..fda2965 100644 --- a/tests/test_infra.py +++ b/tests/test_infra.py @@ -17,6 +17,7 @@ """Tests for infrastructure stuff.""" import io +import os import logging import docutils.core import pep257 @@ -25,8 +26,6 @@ from flake8.api.legacy import get_style_guide from pyuca import Collator -from tests.conftest import _get_python_filepaths - FLAKE8_ROOTS = ['fades', 'tests'] FLAKE8_OPTIONS = ['--max-line-length=99', '--select=E,W,F,C,N'] PEP257_ROOTS = ['fades'] @@ -37,6 +36,17 @@ logging.getLogger(logger_name).setLevel(logging.CRITICAL) +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 + + def test_flake8_pytest(mocker): python_filepaths = _get_python_filepaths(FLAKE8_ROOTS) style_guide = get_style_guide(paths=FLAKE8_OPTIONS) From ab340736a4ad85b6369030410c7d820f38a1409b Mon Sep 17 00:00:00 2001 From: Eduardo Enriquez Date: Tue, 28 May 2019 20:27:20 +0200 Subject: [PATCH 3/6] Moved common function to conftest --- tests/conftest.py | 12 ++++++++++++ tests/test_infra.py | 18 ++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 29ec3c7..e419c15 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ +import os import shutil from pytest import fixture @@ -9,3 +10,14 @@ def tmp_file(tmpdir_factory): dir_path = tmpdir_factory.mktemp("test") yield str(dir_path.join("testfile")) # Converted to str to support python <3.6 versions shutil.rmtree(str(dir_path)) + + +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 diff --git a/tests/test_infra.py b/tests/test_infra.py index fda2965..c95bb0f 100644 --- a/tests/test_infra.py +++ b/tests/test_infra.py @@ -17,7 +17,6 @@ """Tests for infrastructure stuff.""" import io -import os import logging import docutils.core import pep257 @@ -26,6 +25,8 @@ from flake8.api.legacy import get_style_guide from pyuca import Collator +from tests.conftest import get_python_filepaths + FLAKE8_ROOTS = ['fades', 'tests'] FLAKE8_OPTIONS = ['--max-line-length=99', '--select=E,W,F,C,N'] PEP257_ROOTS = ['fades'] @@ -36,19 +37,8 @@ logging.getLogger(logger_name).setLevel(logging.CRITICAL) -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 - - def test_flake8_pytest(mocker): - python_filepaths = _get_python_filepaths(FLAKE8_ROOTS) + 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) @@ -57,7 +47,7 @@ def test_flake8_pytest(mocker): def test_pep257_pytest(): - python_filepaths = _get_python_filepaths(PEP257_ROOTS) + 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)) From fe69d9b7d4f1811ec2f687735ef02b3df71e442c Mon Sep 17 00:00:00 2001 From: Eduardo Enriquez Date: Wed, 29 May 2019 20:27:50 +0200 Subject: [PATCH 4/6] Moved common function to __init__ --- tests/__init__.py | 11 +++++++++++ tests/test_infra.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/__init__.py b/tests/__init__.py index 6c311de..332d5dc 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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 diff --git a/tests/test_infra.py b/tests/test_infra.py index c95bb0f..49c975a 100644 --- a/tests/test_infra.py +++ b/tests/test_infra.py @@ -25,7 +25,7 @@ from flake8.api.legacy import get_style_guide from pyuca import Collator -from tests.conftest import get_python_filepaths +from fades import get_python_filepaths FLAKE8_ROOTS = ['fades', 'tests'] FLAKE8_OPTIONS = ['--max-line-length=99', '--select=E,W,F,C,N'] From f92dba3d0a85c66026ddd01c6204b2979eeee1fa Mon Sep 17 00:00:00 2001 From: Eduardo Enriquez Date: Wed, 29 May 2019 20:41:02 +0200 Subject: [PATCH 5/6] Removed get_python_filepaths from conftest --- tests/conftest.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e419c15..29ec3c7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -import os import shutil from pytest import fixture @@ -10,14 +9,3 @@ def tmp_file(tmpdir_factory): dir_path = tmpdir_factory.mktemp("test") yield str(dir_path.join("testfile")) # Converted to str to support python <3.6 versions shutil.rmtree(str(dir_path)) - - -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 From e319ff1d7b2a8a0d856a021a0242a5f1bdf5966f Mon Sep 17 00:00:00 2001 From: Eduardo Enriquez Date: Wed, 29 May 2019 20:52:25 +0200 Subject: [PATCH 6/6] Fixed import --- tests/test_infra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_infra.py b/tests/test_infra.py index 49c975a..1e62833 100644 --- a/tests/test_infra.py +++ b/tests/test_infra.py @@ -25,7 +25,7 @@ from flake8.api.legacy import get_style_guide from pyuca import Collator -from fades import get_python_filepaths +from tests import get_python_filepaths FLAKE8_ROOTS = ['fades', 'tests'] FLAKE8_OPTIONS = ['--max-line-length=99', '--select=E,W,F,C,N']