Today hacking only provides consumers specifying the local check
factory function via tox.ini (in the '[hacking]' section). While this works
fine in a number of cases, others need a way to specify multiple
check factories without using multiple tox.ini files.
For example, we'd like a library project to define a common set of hacking
check functions for consumers. Some of those factory functions
may register checks we want consumers to run in 'warn only' mode.
From a tox.ini perspective, we'd like to be able to do this:
[testenv:pep8]
commands =
# run mandatory checks in fail mode
flake8 --factory a.b.c.mandatory
# run incubating checks in warn-only mode
flake8 --factory a.b.c.incubating --exit-zero
Today the above isn't possible without 2 separate tox.ini files;
each to specify the local-check-factory. While we can use our
own flake8 extension [1], it would be better to have this
support built right into hacking enabling a broader set of use
cases.
This patch adds a CLI option called --local-check-factory that
allows consumers to pass a local-check-factory via CLI. If
specified on CLI, it overrides anything in tox.ini. Using this
patch consumers can achieve the use case noted above and this
should also be backwards compatible.
[1] https://review.openstack.org/#/c/350723/14/neutron_lib/hacking/checks.py@286
Change-Id: I26f68e11d21938a13974c4e7684eea604dfc2e69
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from flake8 import engine
|
|
import mock
|
|
import pep8
|
|
|
|
from hacking import core
|
|
import hacking.tests
|
|
|
|
|
|
def check(physical_line):
|
|
"""Test check to make sure local-checks are working."""
|
|
if physical_line.strip() == "#this-is-the-test-phrase":
|
|
return (0, "L100: Found local-check test case")
|
|
|
|
|
|
class HackingTestCase(hacking.tests.TestCase):
|
|
def test_local_check(self):
|
|
flake8_style = engine.get_style_guide(parse_argv=False, ignore='F')
|
|
report = pep8.BaseReport(flake8_style.options)
|
|
line = ["#this-is-the-test-phrase"]
|
|
checker = pep8.Checker(lines=line, options=flake8_style.options,
|
|
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)
|