add pep8 tests for syntax errors

This commit is contained in:
Paul Morgan 2014-02-15 18:52:08 +00:00
parent 53f9a02442
commit c108f6022a
5 changed files with 101 additions and 5 deletions

View file

@ -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

14
HACKING
View file

@ -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:

View file

@ -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()

31
test/unit/fixture.py Normal file
View file

@ -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'

50
test/unit/pep8-tests.py Normal file
View file

@ -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.")