summaryrefslogtreecommitdiffstats
path: root/testsuite
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-12-10 20:04:22 -0600
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-12-10 20:04:22 -0600
commitfc8c4d81fd81b66c424480ce5aaa91bcf49809bb (patch)
treea9fbadf3975e9036a9f2df71c655fe615796fa70 /testsuite
parent9d6e6241954d001a5b49e4ea9a48c10e2a792958 (diff)
downloadbcfg2-fc8c4d81fd81b66c424480ce5aaa91bcf49809bb.tar.gz
bcfg2-fc8c4d81fd81b66c424480ce5aaa91bcf49809bb.tar.bz2
bcfg2-fc8c4d81fd81b66c424480ce5aaa91bcf49809bb.zip
testsuite: test for exceptions raised without messages
Diffstat (limited to 'testsuite')
-rw-r--r--testsuite/Testsrc/test_code_checks.py21
-rw-r--r--testsuite/ext/__init__.py0
-rw-r--r--testsuite/ext/exception_messages.py35
-rw-r--r--testsuite/pylintrc.conf2
4 files changed, 50 insertions, 8 deletions
diff --git a/testsuite/Testsrc/test_code_checks.py b/testsuite/Testsrc/test_code_checks.py
index d4ab0aa12..f6b7e991a 100644
--- a/testsuite/Testsrc/test_code_checks.py
+++ b/testsuite/Testsrc/test_code_checks.py
@@ -2,6 +2,7 @@ import os
import re
import sys
import glob
+import copy
from subprocess import Popen, PIPE, STDOUT
# add all parent testsuite directories to sys.path to allow (most)
@@ -15,13 +16,14 @@ while _path != '/':
_path = os.path.dirname(_path)
from common import *
+# path to base testsuite directory
+testdir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
+
# path to Bcfg2 src directory
-srcpath = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..",
- "src"))
+srcpath = os.path.abspath(os.path.join(testdir, "..", "src"))
# path to pylint rc file
-rcfile = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",
- "pylintrc.conf"))
+rcfile = os.path.join(testdir, "pylintrc.conf")
# test for pylint existence
try:
@@ -140,7 +142,7 @@ def blacklist_filter(filelist, blacklist):
class TestPylint(Bcfg2TestCase):
pylint_cmd = ["pylint", "--rcfile", rcfile, "--init-hook",
- "import sys;sys.path.append('%s')" %
+ "import sys;sys.path.extend('%s')" %
os.path.join(srcpath, "lib")]
# regex to find errors and fatal errors
@@ -156,6 +158,11 @@ class TestPylint(Bcfg2TestCase):
full_blacklist = expand_path_dict(error_checks) + contingent_blacklist + \
blacklist
+ def get_env(self):
+ env = copy.copy(os.environ)
+ env['PYTHONPATH'] = '%s:%s' % (env['PYTHONPATH'], testdir)
+ return env
+
@skipIf(not os.path.exists(srcpath), "%s does not exist" % srcpath)
@skipIf(not os.path.exists(rcfile), "%s does not exist" % rcfile)
@skipUnless(HAS_PYLINT, "pylint not found, skipping")
@@ -206,7 +213,7 @@ class TestPylint(Bcfg2TestCase):
extra_args = []
args = self.pylint_cmd + extra_args + \
[os.path.join(srcpath, p) for p in paths]
- pylint = Popen(args, stdout=PIPE, stderr=STDOUT)
+ pylint = Popen(args, stdout=PIPE, stderr=STDOUT, env=self.get_env())
print(pylint.communicate()[0])
self.assertEqual(pylint.wait(), 0)
@@ -260,7 +267,7 @@ class TestPylint(Bcfg2TestCase):
args = self.pylint_cmd + extra_args + \
["-f", "parseable", "-d", "R0801,E1103"] + \
[os.path.join(srcpath, p) for p in paths]
- pylint = Popen(args, stdout=PIPE, stderr=STDOUT)
+ pylint = Popen(args, stdout=PIPE, stderr=STDOUT, env=self.get_env())
output = pylint.communicate()[0]
rv = pylint.wait()
diff --git a/testsuite/ext/__init__.py b/testsuite/ext/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/testsuite/ext/__init__.py
diff --git a/testsuite/ext/exception_messages.py b/testsuite/ext/exception_messages.py
new file mode 100644
index 000000000..877ba42a1
--- /dev/null
+++ b/testsuite/ext/exception_messages.py
@@ -0,0 +1,35 @@
+from logilab import astng
+from pylint.interfaces import IASTNGChecker
+from pylint.checkers import BaseChecker
+from pylint.checkers.utils import safe_infer
+
+
+class ExceptionMessageChecker(BaseChecker):
+ __implements__ = IASTNGChecker
+
+ name = 'Exception Messages'
+ msgs = \
+ {'R9901': ('Exception raised without arguments',
+ 'Used when an exception is raised without any arguments')}
+ options = (
+ ('exceptions-without-args',
+ dict(default=('NotImplementedError',),
+ type='csv',
+ metavar='<exception names>',
+ help='List of exception names that may be raised without arguments')),)
+ # this is important so that your checker is executed before others
+ priority = -1
+
+ def visit_raise(self, node):
+ if node.exc is None:
+ return
+ if isinstance(node.exc, astng.Name):
+ raised = safe_infer(node.exc)
+ if (isinstance(raised, astng.Class) and
+ raised.name not in self.config.exceptions_without_args):
+ self.add_message('R9901', node=node.exc)
+
+
+def register(linter):
+ """required method to auto register this checker"""
+ linter.register_checker(ExceptionMessageChecker(linter))
diff --git a/testsuite/pylintrc.conf b/testsuite/pylintrc.conf
index 63c2873ee..14ccd1d23 100644
--- a/testsuite/pylintrc.conf
+++ b/testsuite/pylintrc.conf
@@ -19,7 +19,7 @@ persistent=no
# List of plugins (as comma separated values of python modules names) to load,
# usually to register additional checkers.
-load-plugins=
+load-plugins=ext.exception_messages
[MESSAGES CONTROL]