瀏覽代碼

utils/checkpackagelib: warn about executable files

Currently there are no .mk, Config.in, .patch or .hash files with
executable permissions in the tree.
But we don't want to have that.

So warn when a file checked by check-package has executable permission.

This check will be reused when testing SysV init scripts in the tree.

Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com>
[Arnout: use context manager for temp dir so it gets deleted]
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
Ricardo Martincoski 3 年之前
父節點
當前提交
7947328de4

+ 1 - 0
utils/checkpackagelib/lib_config.py

@@ -10,6 +10,7 @@ from checkpackagelib.lib import ConsecutiveEmptyLines  # noqa: F401
 from checkpackagelib.lib import EmptyLastLine          # noqa: F401
 from checkpackagelib.lib import NewlineAtEof           # noqa: F401
 from checkpackagelib.lib import TrailingSpace          # noqa: F401
+from checkpackagelib.tool import NotExecutable         # noqa: F401
 
 
 def _empty_or_comment(text):

+ 1 - 0
utils/checkpackagelib/lib_hash.py

@@ -10,6 +10,7 @@ from checkpackagelib.lib import ConsecutiveEmptyLines  # noqa: F401
 from checkpackagelib.lib import EmptyLastLine          # noqa: F401
 from checkpackagelib.lib import NewlineAtEof           # noqa: F401
 from checkpackagelib.lib import TrailingSpace          # noqa: F401
+from checkpackagelib.tool import NotExecutable         # noqa: F401
 
 
 def _empty_line_or_comment(text):

+ 1 - 0
utils/checkpackagelib/lib_mk.py

@@ -13,6 +13,7 @@ from checkpackagelib.lib import EmptyLastLine          # noqa: F401
 from checkpackagelib.lib import NewlineAtEof           # noqa: F401
 from checkpackagelib.lib import TrailingSpace          # noqa: F401
 from checkpackagelib.lib import Utf8Characters         # noqa: F401
+from checkpackagelib.tool import NotExecutable         # noqa: F401
 
 # used in more than one check
 start_conditional = ["ifdef", "ifeq", "ifndef", "ifneq"]

+ 1 - 0
utils/checkpackagelib/lib_patch.py

@@ -8,6 +8,7 @@ import re
 
 from checkpackagelib.base import _CheckFunction
 from checkpackagelib.lib import NewlineAtEof           # noqa: F401
+from checkpackagelib.tool import NotExecutable         # noqa: F401
 
 
 class ApplyOrder(_CheckFunction):

+ 41 - 0
utils/checkpackagelib/test_tool.py

@@ -0,0 +1,41 @@
+import os
+import pytest
+import re
+import tempfile
+import checkpackagelib.tool as m
+
+workdir_regex = re.compile(r'/tmp/tmp[^/]*-checkpackagelib-test-tool')
+
+
+def check_file(tool, filename, string, permissions=None):
+    with tempfile.TemporaryDirectory(suffix='-checkpackagelib-test-tool') as workdir:
+        script = os.path.join(workdir, filename)
+        with open(script, 'wb') as f:
+            f.write(string.encode())
+        if permissions:
+            os.chmod(script, permissions)
+        obj = tool(script)
+        result = obj.run()
+        if result is None:
+            return []
+        return [workdir_regex.sub('dir', r) for r in result]
+
+
+NotExecutable = [
+    ('664',
+     'package.mk',
+     0o664,
+     '',
+     []),
+    ('775',
+     'package.mk',
+     0o775,
+     '',
+     ["dir/package.mk:0: This file does not need to be executable"]),
+    ]
+
+
+@pytest.mark.parametrize('testname,filename,permissions,string,expected', NotExecutable)
+def test_NotExecutable(testname, filename, permissions, string, expected):
+    warnings = check_file(m.NotExecutable, filename, string, permissions)
+    assert warnings == expected

+ 8 - 0
utils/checkpackagelib/tool.py

@@ -0,0 +1,8 @@
+import os
+from checkpackagelib.base import _Tool
+
+
+class NotExecutable(_Tool):
+    def run(self):
+        if os.access(self.filename, os.X_OK):
+            return ["{}:0: This file does not need to be executable".format(self.filename)]