summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2014-03-21 07:53:46 +0100
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2014-03-21 07:53:46 +0100
commitbd15fd5219e3453e7a2c1964d972806c4bac1508 (patch)
treef513040bf8f0c5400132e5fdc73be27446f4ac06
parentf49829d0867befb272c7115dac9c109aaa803760 (diff)
downloadpylint-bd15fd5219e3453e7a2c1964d972806c4bac1508.tar.gz
enhance option preprocessing error detection and related tests
-rw-r--r--lint.py19
-rw-r--r--test/unittest_lint.py18
2 files changed, 28 insertions, 9 deletions
diff --git a/lint.py b/lint.py
index 2d781b7..7697315 100644
--- a/lint.py
+++ b/lint.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE).
+# Copyright (c) 2003-2014 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
@@ -873,15 +873,20 @@ def preprocess_options(args, search_for):
option, val = arg[2:], None
try:
cb, takearg = search_for[option]
+ except KeyError:
+ i += 1
+ else:
del args[i]
if takearg and val is None:
if i >= len(args) or args[i].startswith('-'):
- raise ArgumentPreprocessingError(arg)
+ msg = 'Option %s expects a value' % option
+ raise ArgumentPreprocessingError(msg)
val = args[i]
del args[i]
+ elif not takearg and val is not None:
+ msg = "Option %s doesn't expects a value" % option
+ raise ArgumentPreprocessingError(msg)
cb(option, val)
- except KeyError:
- i += 1
else:
i += 1
@@ -906,7 +911,7 @@ group are mutually exclusive.'),
'load-plugins': (self.cb_add_plugins, True),
})
except ArgumentPreprocessingError, ex:
- print >> sys.stderr, 'Argument %s expects a value.' % (ex.args[0],)
+ print >> sys.stderr, ex
sys.exit(32)
self.linter = linter = self.LinterClass((
@@ -1043,11 +1048,11 @@ are done by default'''}),
sys.exit(self.linter.msg_status)
def cb_set_rcfile(self, name, value):
- """callback for option preprocessing (i.e. before optik parsing)"""
+ """callback for option preprocessing (i.e. before option parsing)"""
self._rcfile = value
def cb_add_plugins(self, name, value):
- """callback for option preprocessing (i.e. before optik parsing)"""
+ """callback for option preprocessing (i.e. before option parsing)"""
self._plugins.extend(splitstrip(value))
def cb_error_mode(self, *args, **kwargs):
diff --git a/test/unittest_lint.py b/test/unittest_lint.py
index 6c3569c..e211c15 100644
--- a/test/unittest_lint.py
+++ b/test/unittest_lint.py
@@ -491,7 +491,7 @@ class PreprocessOptionsTC(TestCase):
def _callback(self, name, value):
self.args.append((name, value))
- def test_preprocess(self):
+ def test_value_equal(self):
self.args = []
preprocess_options(['--foo', '--bar=baz', '--qu=ux'],
{'foo' : (self._callback, False),
@@ -499,7 +499,14 @@ class PreprocessOptionsTC(TestCase):
self.assertEqual(
[('foo', None), ('qu', 'ux')], self.args)
- def test_preprocessing_error(self):
+ def test_value_space(self):
+ self.args = []
+ preprocess_options(['--qu', 'ux'],
+ {'qu' : (self._callback, True)})
+ self.assertEqual(
+ [('qu', 'ux')], self.args)
+
+ def test_error_missing_expected_value(self):
self.assertRaises(
ArgumentPreprocessingError,
preprocess_options,
@@ -511,6 +518,13 @@ class PreprocessOptionsTC(TestCase):
['--foo', '--bar'],
{'bar' : (None, True)})
+ def test_error_unexpected_value(self):
+ self.assertRaises(
+ ArgumentPreprocessingError,
+ preprocess_options,
+ ['--foo', '--bar=spam', '--qu=ux'],
+ {'bar' : (None, False)})
+
if __name__ == '__main__':
unittest_main()