From c108f6022adb7b1a300fb1e19cdb7ddbde462528 Mon Sep 17 00:00:00 2001 From: Paul Morgan Date: Sat, 15 Feb 2014 18:52:08 +0000 Subject: [PATCH] add pep8 tests for syntax errors --- .travis.yml | 6 ++++- HACKING | 14 +++++++++--- runtests.py | 5 ++++- test/unit/fixture.py | 31 +++++++++++++++++++++++++ test/unit/pep8-tests.py | 50 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 test/unit/fixture.py create mode 100644 test/unit/pep8-tests.py diff --git a/.travis.yml b/.travis.yml index 19470b3..668d1e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,11 @@ +# http://docs.travis-ci.com/user/languages/python/ language: python python: - "2.6" - "2.7" -install: pip install 'GitPython >= 0.2.0' --pre +install: + - pip install 'GitPython >= 0.2.0' --pre + - pip install 'pep8' --use-mirrors + script: nosetests test/unit diff --git a/HACKING b/HACKING index d55fc32..16552e6 100644 --- a/HACKING +++ b/HACKING @@ -13,10 +13,18 @@ for tips on writing portable Python code. Tests ----- -To run all tests, install the python-nose and python3-nose -packages, then from the root of the project: +To run all tests, install these packages: - python ./runtests.py +* python-nose and python3-nose +* python-pep8 and python3-pep8 + +Then from the root of the project: + + python ./runtests.py -vv + python3 ./runtests.py -vv test/unit + +(Run only the unit tests on python3 since GitPython does not +support python3 yet.) When developing code for tito there are a couple ways you can test: diff --git a/runtests.py b/runtests.py index 8e53d17..4a2e639 100755 --- a/runtests.py +++ b/runtests.py @@ -33,7 +33,10 @@ SRC_BIN_DIR = os.path.abspath(os.path.join(TEST_SCRIPT_DIR, "bin/")) os.environ['TITO_SRC_BIN_DIR'] = SRC_BIN_DIR if __name__ == '__main__': + import nose + + print("Using Python %s" % sys.version[0:3]) + print("Using nose %s" % nose.__version__[0:3]) print("Running tito tests against: %s" % SRC_DIR) - import nose nose.main() diff --git a/test/unit/fixture.py b/test/unit/fixture.py new file mode 100644 index 0000000..dbd2d16 --- /dev/null +++ b/test/unit/fixture.py @@ -0,0 +1,31 @@ +# +# Copyright (c) 2008-2014 Red Hat, Inc. +# +# This software is licensed to you under the GNU General Public License, +# version 2 (GPLv2). There is NO WARRANTY for this software, express or +# implied, including the implied warranties of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 +# along with this software; if not, see +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. +# +# Red Hat trademarks are not licensed under GPLv2. No permission is +# granted to use or replicate Red Hat trademarks that are incorporated +# in this software or its documentation. +import os +import unittest + +UNIT_DIR = os.path.abspath(os.path.dirname(__file__)) +REPO_DIR = os.path.join(UNIT_DIR, '..', '..') + + +class TitoUnitTestFixture(unittest.TestCase): + """ + Fixture providing setup/teardown and utilities for unit tests. + """ + def setUp(self): + # GitPython calls os.login(), which throws OSError if there is no tty, + # but GitPython allows to avoid the call if env var USER exists. + try: + os.getlogin() + except OSError: + os.environ['USER'] = 'nobody' diff --git a/test/unit/pep8-tests.py b/test/unit/pep8-tests.py new file mode 100644 index 0000000..46257c0 --- /dev/null +++ b/test/unit/pep8-tests.py @@ -0,0 +1,50 @@ +# +# Copyright (c) 2008-2014 Red Hat, Inc. +# +# This software is licensed to you under the GNU General Public License, +# version 2 (GPLv2). There is NO WARRANTY for this software, express or +# implied, including the implied warranties of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 +# along with this software; if not, see +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. +# +# Red Hat trademarks are not licensed under GPLv2. No permission is +# granted to use or replicate Red Hat trademarks that are incorporated +# in this software or its documentation. + +""" +Use pep8 to check for errors or deprecations that can cause Python 3 to fail. +source: https://github.com/jcrocholl/pep8 +docs: http://pep8.readthedocs.org/en/latest/intro.html + +Python 3 is picky about indentation: +http://docs.python.org/3.3/reference/lexical_analysis.html +""" + +import pep8 +from fixture import * + + +class TestPep8(TitoUnitTestFixture): + def setUp(self): + TitoUnitTestFixture.setUp(self) + + def test_conformance(self): + tests = [ + # http://pep8.readthedocs.org/en/latest/intro.html#error-codes + 'E9', # runtime errors (SyntaxError, IndentationError, IOError) + ] + + try: + checker = pep8.StyleGuide(select=tests, paths=[REPO_DIR]) + result = checker.check_files().total_errors + except AttributeError: + # We don't have pep8.StyleGuide, so we must be + # using pep8 older than git tag 1.1-72-gf20d656. + os.chdir(REPO_DIR) + checks = ','.join(tests) + cmd = "pep8 --select=%s %s | wc -l" % (checks, '.') + result = int(getoutput(cmd)) + + self.assertEqual(result, 0, + "Found PEP8 errors that may break your code in Python 3.")