diff --git a/README.rst b/README.rst index 6fc1b3f..979becf 100644 --- a/README.rst +++ b/README.rst @@ -129,3 +129,13 @@ register. Such as: [hacking] local-check-factory = nova.tests.hacking.factory + +In addition, hacking provides a flake8 option to specify the +``local-check-factory`` on the CLI. When specified via CLI with +the ``--local-check-factory`` option to flake8, the specified +factory takes precedence over ``tox.ini`` and the local checks +from ``tox.ini`` are not used. + +For example, via CLI (any check specified in ``tox.ini`` are ignored):: + + flake8 --local-check-factory mypkg.warn_checks --exit-zero diff --git a/hacking/core.py b/hacking/core.py index 338f81f..a5bacb6 100644 --- a/hacking/core.py +++ b/hacking/core.py @@ -145,20 +145,29 @@ class ProxyChecks(GlobalCheck): name = 'ProxyChecker' @classmethod - def add_options(cls, parser): + def parse_options(cls, options): # We're looking for local checks, so we need to include the local # dir in the search path sys.path.append('.') + local_check_fact = options.factory or CONF.get('local-check-factory') - local_check = CONF.get_multiple('local-check', default=[]) - for check_path in set(local_check): - if check_path.strip(): - checker = pbr.util.resolve_name(check_path) - pep8.register_check(checker) + if not options.factory: + # local factory on CLI trumps everything else + local_check = CONF.get_multiple('local-check', default=[]) + for check_path in set(local_check): + if check_path.strip(): + checker = pbr.util.resolve_name(check_path) + pep8.register_check(checker) - local_check_fact = CONF.get('local-check-factory') if local_check_fact: factory = pbr.util.resolve_name(local_check_fact) factory(pep8.register_check) sys.path.pop() + + @classmethod + def add_options(cls, parser): + # allow local check factory to be specified as CLI option to flake8 + # in which case its used as the sole source of checks + parser.add_option('--local-check-factory', action='store', + type='string', dest='factory') diff --git a/hacking/tests/test_local.py b/hacking/tests/test_local.py index e4dcc23..6be7d9a 100644 --- a/hacking/tests/test_local.py +++ b/hacking/tests/test_local.py @@ -14,8 +14,10 @@ # limitations under the License. from flake8 import engine +import mock import pep8 +from hacking import core import hacking.tests @@ -34,3 +36,11 @@ class HackingTestCase(hacking.tests.TestCase): report=report) checker.check_all() self.assertIn("L100", report.counters) + + @mock.patch.object(core, 'CONF') + def test_local_check_factory_cli_option(self, mock_conf): + mock_opts = mock.Mock() + mock_opts.factory = 'mypkg.factory' + self.assertRaises( + ImportError, core.ProxyChecks.parse_options, mock_opts) + self.assertFalse(mock_conf.called)