From 613db3c2f63d3074699f6a961f972e128e7ea15c Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Sun, 5 Jun 2016 09:15:12 -0700 Subject: [PATCH] Unit tests working, with pkgutil not broken, on Python 3.3!!! --- .gitignore | 1 + CONTRIBUTING.rst | 24 +++++++-------- polyloader/polyloader.py | 2 +- setup.py | 2 +- tests/polytestmix/test2.2 | 1 + tests/polytestmix/test3.3 | 1 + tests/test_polyloader.py | 65 +++++++++++++++++---------------------- tox.ini | 2 +- 8 files changed, 47 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index 22f7b6a..0ecf7ee 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ bower_components *.egg-info build/* .cache/* +.tox/* diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 49f8b35..458c85f 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -13,7 +13,7 @@ Report Bugs ~~~~~~~~~~~ Report bugs at -https://github.com/elfsternberg/py-polymorphic-loader/issues. +https://github.com/elfsternberg/polyloader/issues. If you are reporting a bug, please include: @@ -37,15 +37,15 @@ Look through the GitHub issues for features. Anything tagged with Write Documentation ~~~~~~~~~~~~~~~~~~~ -py-polymorphic-loader could always use more documentation, whether as -part of the official py-polymorphic-loader docs, in docstrings, or even +polyloader could always use more documentation, whether as +part of the official polyloader docs, in docstrings, or even on the web in blog posts, articles, and such. Submit Feedback ~~~~~~~~~~~~~~~ The best way to send feedback is to file an issue at -https://github.com/elfsternberg/py-polymorphic-loader/issues. +https://github.com/elfsternberg/polyloader/issues. If you are proposing a feature: @@ -57,15 +57,15 @@ If you are proposing a feature: Get Started! ------------ -Ready to contribute? Here's how to set up py-polymorphic-loader for +Ready to contribute? Here's how to set up polyloader for local development. -1. Fork the py-polymorphic-loader repo on GitHub. +1. Fork the polyloader repo on GitHub. 2. Clone your fork locally: :: - $ git clone git@github.com:your_name_here/py-polymorphic-loader.git + $ git clone git@github.com:your_name_here/polyloader.git 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for @@ -73,8 +73,8 @@ local development. :: - $ mkvirtualenv py-polymorphic-loader - $ cd py-polymorphic-loader/ + $ mkvirtualenv polyloader + $ cd polyloader/ $ python setup.py develop 4. Create a branch for local development: @@ -90,7 +90,7 @@ local development. :: - $ flake8 py-polymorphic-loader tests + $ flake8 polyloader tests $ python setup.py test or py.test $ tox @@ -117,7 +117,7 @@ Before you submit a pull request, check that it meets these guidelines: the feature to the list in README.rst. 3. The pull request should work for Python 2.6, 2.7, 3.3, 3.4 and 3.5, and for PyPy. Check - https://travis-ci.org/elfsternberg/py-polymorphic-loader/pull_requests + https://travis-ci.org/elfsternberg/polyloader/pull_requests and make sure that the tests pass for all supported Python versions. Tips @@ -125,4 +125,4 @@ Tips To run a subset of tests: -$ py.test tests.test\_py-polymorphic-loader +$ py.test tests.test\_polyloader diff --git a/polyloader/polyloader.py b/polyloader/polyloader.py index d414a02..3180fd4 100644 --- a/polyloader/polyloader.py +++ b/polyloader/polyloader.py @@ -174,5 +174,5 @@ def install(compiler, suffixes): ExtendedSourceFileLoader.get_extended_suffixes_inclusive()) sys.path_hooks[fpos] = ExtendedFileFinder.path_hook(*supported_loaders) iter_importer_modules.register(ExtendedFileFinder, ExtendedFileFinder.iter_modules) - if sys.path[0] !== "": + if sys.path[0] != "": sys.path.insert(0, "") diff --git a/setup.py b/setup.py index 944e0ae..d916f71 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ setup( author_email='elf.sternberg@gmail.com', url='https://github.com/elfsternberg/polyloader', packages=[ - 'py-polymorphic-loader', + 'polyloader', ], package_dir={'polyloader': 'polyloader'}, include_package_data=True, diff --git a/tests/polytestmix/test2.2 b/tests/polytestmix/test2.2 index e69de29..b3b01de 100644 --- a/tests/polytestmix/test2.2 +++ b/tests/polytestmix/test2.2 @@ -0,0 +1 @@ +Test Two diff --git a/tests/polytestmix/test3.3 b/tests/polytestmix/test3.3 index e69de29..60d7851 100644 --- a/tests/polytestmix/test3.3 +++ b/tests/polytestmix/test3.3 @@ -0,0 +1 @@ +Test Three diff --git a/tests/test_polyloader.py b/tests/test_polyloader.py index bd5f83c..82cf168 100644 --- a/tests/test_polyloader.py +++ b/tests/test_polyloader.py @@ -2,58 +2,51 @@ # -*- coding: utf-8 -*- """ -test_py-polymorphic-loader +test_polyloader ---------------------------------- -Tests for `py-polymorphic-loader` module. +Tests for `polyloader` module. """ import pytest +from polyloader import polyloader -from py_polymorphic_loader import polymorph - -# Note that these compilers don't actually load anything out of the +# Note that these compilers don't actually load much out of the # source files. That's not the point. The point is to show that the # correct compiler has been found for a given extension. -def compiler2(filename): - return compile("result='Success for %s'" % (filename), filename, "exec") - -def compiler3(filename): - return compile("result='Success for %s'" % (filename), filename, "exec") +def compiler(pt): + def _compiler(source_path, modulename): + with open(source_path, "r") as file: + return compile("result='Success for %s: %s'" % (pt, file.readline().rstrip()), modulename, "exec") + return _compiler class Test_Polymorph_1(object): def test_import1(self): - import polytestmix - polytestmix.install(compiler2, ['.2']) - polytestmix.install(compiler3, ['.3']) - assert(polytestmix.test2.result = "Success for test2") - assert(polytestmix.test3.result = "Success for test3") - -class Test_Polymorph_2(object): - def test_import2(self): - import polytestmix - polytestmix.install(compiler2, ['.2']) - polytestmix.install(compiler3, ['.3']) - assert(polytestmix.test2.result = "Success for test2") - -class Test_Polymorph_2(object): - def test_import2(self): - import polytestmix.test3 - polytestmix.install(compiler2, ['.2']) - polytestmix.install(compiler3, ['.3']) - assert(polytestmix.test3.result = "Success for test3") + polyloader.install(compiler("2"), ['.2']) + polyloader.install(compiler("3"), ['.3']) + from .polytestmix import test2 + from .polytestmix import test3 + assert(test2.result == "Success for 2: Test Two") + assert(test3.result == "Success for 3: Test Three") class Test_Polymorph_Iterator(object): - ''' The Django Compatibility test. ''' + ''' The Django Compatibility test: Can we load arbitrary modules from a package? ''' def test_iterator(self): - import polytestmix.test3 - polytestmix.install(compiler2, ['.2']) - polytestmix.install(compiler3, ['.3']) - target_dir = os.path.join('.', 'polytestmix') - files = set([name for _, name, is_pkg in pkgutil.iter_modules([targetdir]) + import os + import pkgutil + import inspect + polyloader.install(compiler("2"), ['.2']) + polyloader.install(compiler("3"), ['.3']) + from .polytestmix import test2 + from .polytestmix import test3 + target_dir = os.path.join( + os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))), + 'polytestmix') + modules = set([name for _, name, is_pkg in pkgutil.iter_modules([target_dir]) if not is_pkg and not name.startswith('_')]) - assert(files == set(['test2.2', 'test3.3', 'test1.py'])) + assert(modules == set(['test1', 'test2', 'test3'])) + diff --git a/tox.ini b/tox.ini index 0f46f77..0b8729e 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,7 @@ envlist = py26, py27, py33, py34, py35 [testenv] setenv = - PYTHONPATH = {toxinidir}:{toxinidir}/py-polymorphic-loader + PYTHONPATH = {toxinidir}:{toxinidir}/polyloader deps = -r{toxinidir}/requirements_dev.txt commands =