diff mbox series

[next,v3,1/3] utils/check-package: factor-out check_disabled()

Message ID 20230903163509.614740-2-ricardo.martincoski@gmail.com
State New
Headers show
Series check-package: support inline flags v3 | expand

Commit Message

Ricardo Martincoski Sept. 3, 2023, 4:35 p.m. UTC
Currently source files analysed by check-package can use a special
comment to hint the script to completely ignore a CheckFunction for the
next line of the source file:

    # check-package Indent

Currently the logic that implements this is at the main check-package
script. But the main package script is not covered by unit tests.

In preparation to support not only
'ignore this CheckFunction for the next source line'
but also
'use special rules for this CheckFunction when testing this file'
move the logic that parses such special comments into the base class
_CheckFunction.

At same time, create unit tests that exercise such code.

Cc: James Knight <james.d.knight@live.com>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com>
---
Changes v1 -> v3:
  - new patch, implementing a code improvement detected while reviewing
    v1 of this series

https://gitlab.com/RicardoMartincoski/buildroot/-/pipelines/990306862
---
 utils/check-package                |  2 +-
 utils/checkpackagelib/base.py      |  5 +++
 utils/checkpackagelib/test_base.py | 49 ++++++++++++++++++++++++++++++
 utils/checkpackagelib/test_util.py |  5 ++-
 4 files changed, 59 insertions(+), 2 deletions(-)
 create mode 100644 utils/checkpackagelib/test_base.py
diff mbox series

Patch

diff --git a/utils/check-package b/utils/check-package
index 105902303e..23d250d4a1 100755
--- a/utils/check-package
+++ b/utils/check-package
@@ -237,7 +237,7 @@  def check_file_using_lib(fname):
         for lineno, text in enumerate(f):
             nlines += 1
             for name, cf in objects:
-                if cf.disable.search(lastline):
+                if cf.check_disabled(lastline):
                     continue
                 line_sts = cf.check_line(lineno + 1, text)
                 warn, fail = print_warnings(line_sts, name in xfail)
diff --git a/utils/checkpackagelib/base.py b/utils/checkpackagelib/base.py
index f666e4110b..15a304acf3 100644
--- a/utils/checkpackagelib/base.py
+++ b/utils/checkpackagelib/base.py
@@ -8,6 +8,11 @@  class _CheckFunction(object):
         self.url_to_manual = url_to_manual
         self.disable = re.compile(r"^\s*# check-package .*\b{}\b".format(self.__class__.__name__))
 
+    def check_disabled(self, line_before):
+        if self.disable.search(line_before):
+            return True
+        return False
+
     def before(self):
         pass
 
diff --git a/utils/checkpackagelib/test_base.py b/utils/checkpackagelib/test_base.py
new file mode 100644
index 0000000000..ef266d542c
--- /dev/null
+++ b/utils/checkpackagelib/test_base.py
@@ -0,0 +1,49 @@ 
+import pytest
+import checkpackagelib.test_util as util
+import checkpackagelib.base as m
+
+
+IgnoreNextLine = [
+    ('without comment to ignore a given CheckFunction',
+     'any',
+     'content to test\n'
+     '# commented content to test\n'
+     'content to test\n'
+     'content to test\n'
+     'content to test\n',
+     [1, 2, 3, 4, 5]),  # set of line numbers for all lines not ignored while testing
+    ('ignore next line',
+     'any',
+     'content to test\n'
+     '# check-package CheckFunctionUnderTest\n'
+     'content to ignore\n'
+     'content to test\n'
+     'content to test\n',
+     [1, 2, 4, 5]),
+    ('comment to ignore one CheckFunction does not affect another one',
+     'any',
+     'content to test\n'
+     '# check-package AnotherCheckFunction\n'
+     'content to test\n'
+     'content to test\n'
+     'content to test\n',
+     [1, 2, 3, 4, 5]),
+    ('ignore 2 CheckFunctions for next line',
+     'any',
+     '# check-package CheckFunctionUnderTest, AnotherCheckFunction\n'
+     'content to ignore\n'
+     '# check-package AnotherCheckFunction, CheckFunctionUnderTest\n'
+     'content to ignore\n'
+     'content to test\n',
+     [1, 3, 5]),
+    ]
+
+
+@pytest.mark.parametrize('testname,filename,string,expected', IgnoreNextLine)
+def test_IgnoreNextLine(testname, filename, string, expected):
+    class CheckFunctionUnderTest(m._CheckFunction):
+        def check_line(self, lineno, text):
+            return lineno
+
+    warnings = util.check_file(CheckFunctionUnderTest, filename, string)
+    assert warnings == expected
diff --git a/utils/checkpackagelib/test_util.py b/utils/checkpackagelib/test_util.py
index 23f2995e27..b06e218498 100644
--- a/utils/checkpackagelib/test_util.py
+++ b/utils/checkpackagelib/test_util.py
@@ -2,7 +2,10 @@  def check_file(check_function, filename, string):
     obj = check_function(filename, 'url')
     result = []
     result.append(obj.before())
+    lastline = ""
     for i, line in enumerate(string.splitlines(True)):
-        result.append(obj.check_line(i + 1, line))
+        if not obj.check_disabled(lastline):
+            result.append(obj.check_line(i + 1, line))
+        lastline = line
     result.append(obj.after())
     return [r for r in result if r is not None]