This is an automated email from the ASF dual-hosted git repository.

gcruz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git


The following commit(s) were added to refs/heads/master by this push:
     new 91d27e61a [#8610] Add pre-commit hooks and clean up syntax.py tests to 
use them
91d27e61a is described below

commit 91d27e61a7bc286b57bb9d92970460a071f1cd47
Author: Carlos Cruz <[email protected]>
AuthorDate: Wed Aug 16 19:06:25 2023 +0000

    [#8610] Add pre-commit hooks and clean up syntax.py tests to use them
---
 .pre-commit-config.yaml              | 109 +++++++++++++++++++++++++++++++++++
 AlluraTest/alluratest/test_syntax.py |  66 ++++++---------------
 requirements.in                      |   1 +
 requirements.txt                     |  20 ++++++-
 ruff.toml                            |   8 ++-
 5 files changed, 155 insertions(+), 49 deletions(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 000000000..9a5b3cc17
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,109 @@
+# See https://pre-commit.com for more information
+# See https://pre-commit.com/hooks.html for more hooks
+
+default_stages: [commit]
+repos:
+-   repo: https://github.com/pre-commit/pre-commit-hooks
+    rev: v4.1.0
+    hooks:
+    # general checks
+    -   id: check-xml
+    -   id: check-yaml
+    -   id: check-json
+        exclude: '.babelrc'
+    -   id: check-merge-conflict
+        fail_fast: true
+    # python checks
+    -   id: debug-statements
+        exclude: '^Allura/allura/lib/utils.py$'
+    # other checks
+    -   id: check-docstring-first
+        exclude: |
+            (?x)^(
+                Allura/allura/eventslistener.py|
+                Allura/allura/lib/.*|
+                scripts/teamforge-import.py
+            )$
+
+
+-   repo: https://github.com/pre-commit/pygrep-hooks
+    rev: v1.9.0
+    hooks:
+    -   id: python-check-blanket-noqa
+        exclude: '^Allura/setup.py$'
+    -   id: python-check-mock-methods
+        exclude: '^Allura/allura/tests/test_tasks.py$'
+    -   id: python-no-log-warn
+    -   id: rst-backticks
+        exclude:
+          (?x)^(
+                Allura/docs/.*|
+                ForgeImporters/docs/.*
+          )$
+    -   id: rst-directive-colons
+    -   id: rst-inline-touching-normal
+
+# https://pre-commit.com/#pygrep
+# mirrors test_syntax.py tests.  TODO have test_syntax.py run `pre-commit run 
--all-files utcnow` etc
+-   repo: local
+    hooks:
+      # Use
+      # .utcnow() rather than .now()
+      # .utcfromtimestamp() rather than .fromtimestamp()
+      # calendar.timegm() rather than mktime()
+      - id: tz-functions
+        name: don't use local tz functions
+        language: pygrep
+        types: [python]
+        entry: '\.now\(|\.fromtimestamp\(|\.mktime\('
+      - id: noprint
+        name: check for print statements
+        language: pygrep
+        types: [python]
+        entry: '\bprint\('
+        exclude: |
+            (?x)^(
+                /tests/.*|
+                Allura/allura/command/.*|
+                Allura/ldap-setup.py|
+                scripts/.*|
+                Allura/ldap-userconfig.py|
+                /scripts/.*|
+                ForgeSVN/setup.py|
+                Allura/allura/scripts/.*|
+                Allura/allura/tests/.*|
+                AlluraTest/alluratest/.*|
+                ForgeGit/forgegit/tests/.*|
+                fuse/.*|
+                run_tests|
+                ForgeSVN/forgesvn/tests/.*|
+                ForgeWiki/forgewiki/tests/.*|
+                ForgeImporters/forgeimporters/tests/.*
+            )$
+      - id: notab
+        name: check for tabs
+        language: pygrep
+        types: [python]
+        entry: '       '
+# SCSS Syntax: could use placeholders instead of extending a class or tag 
directly
+      - id: scss_extend_pattern
+        name: search for scss invalid extend patterns in class elements
+        language: pygrep
+        types: [file, scss]
+        entry: '@extend [^%]((?![\/\/]\stest_scss_extend_pattern allow).)*$'
+        exclude: 'node_modules'
+
+
+- repo:  https://github.com/milin/giticket
+  rev: v1.3
+  hooks:
+  - id:  giticket
+    args: ['--mode=regex_match', '--format=[#{ticket}] {commit_msg}', 
'--regex=[0-9]{4,}']
+
+
+- repo: https://github.com/astral-sh/ruff-pre-commit
+  # Ruff version.
+  rev: v0.0.283
+  hooks:
+    - id: ruff
+      types: [python]
diff --git a/AlluraTest/alluratest/test_syntax.py 
b/AlluraTest/alluratest/test_syntax.py
index c4feb0b9e..a05ba1281 100644
--- a/AlluraTest/alluratest/test_syntax.py
+++ b/AlluraTest/alluratest/test_syntax.py
@@ -16,57 +16,29 @@
 #       under the License.
 
 import os.path
+import re
 from subprocess import Popen, PIPE
 import sys
 
-from unittest import SkipTest
-from itertools import zip_longest
+dir = os.path.abspath(os.path.dirname(__file__) + "/..")
 
-toplevel_dir = os.path.abspath(os.path.dirname(__file__) + "/../..")
-BASE_PATH = (toplevel_dir,) #freeze main path
 
-def run(cmd):
-    proc = Popen(cmd, shell=True, cwd=toplevel_dir, stdout=PIPE, stderr=PIPE)
-    # must capture & reprint stdout, so that pytest can capture it
+def run(cmd, dir=dir):
+    proc = Popen(cmd, shell=True, cwd=dir, stdout=PIPE, stderr=PIPE)
+    # must capture & reprint stdount, so that test suite can capture it
     (stdout, stderr) = proc.communicate()
-    sys.stdout.write(stdout.decode('utf-8'))
-    sys.stderr.write(stderr.decode('utf-8'))
-    return proc.returncode
-
-
-find_py = r"find Allura Forge* -not -path '*/\.*' -name '*.py'"
-
-
-def test_no_local_tz_functions():
-    if run(find_py + r" | xargs grep '\.now(' ") not in [1, 123]:
-        raise Exception("These should use .utcnow()")
-    if run(find_py + r" | xargs grep '\.fromtimestamp(' ") not in [1, 123]:
-        raise Exception("These should use .utcfromtimestamp()")
-    if run(find_py + " | xargs grep 'mktime(' ") not in [1, 123]:
-        raise Exception("These should use calendar.timegm()")
-
-
-def test_no_prints():
-    skips = [
-        '/tests/',
-        'Allura/allura/command/',
-        'Allura/ldap-setup.py',
-        'Allura/ldap-userconfig.py',
-        '/scripts/',
-        'ForgeSVN/setup.py',
-    ]
-    if run(find_py + " | grep -v '" + "' | grep -v '".join(skips) + r"' | 
xargs grep -v '^ *#' | egrep -n '\bprint\(' | grep -E -v '(pprint|#pragma: 
?printok)' ") != 1:
-        raise Exception("These should use logging instead of print")
-
-
-def test_no_tabs():
-    if run(find_py + " | xargs grep '  ' ") not in [1, 123]:
-        raise Exception('These should not use tab chars')
-
-
-def test_ruff():
-    cmd = f"ruff check . --config {BASE_PATH[0]}/ruff.toml --show-source"
-    if run(cmd) != 0:
-        # print 'Command was: %s' % cmd
-        raise Exception('ruff failure, see stdout')
+    stdout = stdout.decode('utf-8')
+    stderr = stderr.decode('utf-8')
+    if re.match(r'xargs: .* No such file', stderr):
+        raise Exception(stderr)
+    print(stdout, end='')
+    print(stderr, end='', file=sys.stderr)
+    return (proc.returncode, proc.communicate())
+
+
+def test_run_precommit():
+    cmd = "pre-commit run --all-files"
+    code, outputs = run(cmd, dir='/src/allura')
+    if code != 0:
+        raise Exception(f'pre-commit failed to run: {outputs[0].decode()} 
{outputs[1].decode()}')
 
diff --git a/requirements.in b/requirements.in
index fd162d5c3..374a1a6df 100644
--- a/requirements.in
+++ b/requirements.in
@@ -59,3 +59,4 @@ pytest-sugar
 
 # deployment
 gunicorn
+pre-commit==2.21.0 # Supports Python 3.7 and 3.11
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index a0db57420..51c0f788a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,5 +1,5 @@
 #
-# This file is autogenerated by pip-compile with Python 3.7
+# This file is autogenerated by pip-compile with Python 3.11
 # by the following command:
 #
 #    pip-compile
@@ -18,6 +18,8 @@ certifi==2023.7.22
     # via requests
 cffi==1.15.1
     # via cryptography
+cfgv==3.4.0
+    # via pre-commit
 charset-normalizer==3.1.0
     # via requests
 colander==1.8.3
@@ -32,6 +34,8 @@ decorator==5.1.1
     # via -r requirements.in
 diff-match-patch==20230430
     # via sxsdiff
+distlib==0.3.7
+    # via virtualenv
 docutils==0.19
     # via pypeline
 easywidgets==0.4.1
@@ -48,6 +52,8 @@ feedgenerator==2.1.0
     # via -r requirements.in
 feedparser==6.0.10
     # via -r requirements.in
+filelock==3.12.2
+    # via virtualenv
 formencode==2.0.1
     # via
     #   -r requirements.in
@@ -65,6 +71,8 @@ html5lib==1.1
     #   -r requirements.in
     #   pypeline
     #   textile
+identify==2.5.26
+    # via pre-commit
 idna==3.4
     # via requests
 importlib-metadata==6.6.0
@@ -99,6 +107,8 @@ ming==0.13.0
     # via -r requirements.in
 mock==5.1.0
     # via -r requirements.in
+nodeenv==1.8.0
+    # via pre-commit
 oauthlib==3.2.2
     # via
     #   -r requirements.in
@@ -125,6 +135,10 @@ pillow==9.5.0
     # via -r requirements.in
 pluggy==1.2.0
     # via pytest
+platformdirs==3.10.0
+    # via virtualenv
+pre-commit==2.21.0
+    # via -r requirements.in
 profanityfilter==2.0.6
     # via -r requirements.in
 pycparser==2.21
@@ -164,6 +178,8 @@ pytz==2023.3
     #   -r requirements.in
     #   feedgenerator
     #   ming
+pyyaml==6.0.1
+    # via pre-commit
 qrcode==7.4.2
     # via -r requirements.in
 regex==2022.10.31
@@ -230,6 +246,8 @@ typing-extensions==4.5.0
     #   qrcode
 urllib3==1.26.15
     # via requests
+virtualenv==20.24.3
+    # via pre-commit
 waitress==2.1.2
     # via webtest
 webencodings==0.5.1
diff --git a/ruff.toml b/ruff.toml
index e35e1c2cb..88b6cea3f 100644
--- a/ruff.toml
+++ b/ruff.toml
@@ -24,7 +24,13 @@ ignore = [
     'E402', # Module level import not at top of file
     'E731', # Do not assign a lambda expression, use a def
     'E741', # Ambiguous variable name: I,
-    'E501' # Line too long
+    'E501', # Line too long
+    # REMOVE THESE AND FIX THE ISSUES
+    'F541', # f-string without any placeholders
+    'E401', # Multiple imports on one line
+    'E721', # Do not compare types, use `isinstance()`
+    'E713', # Test for membership should be `not in`
+    'E701'  # Multiple statements on one line (colon)
 ]
 line-length = 119
 

Reply via email to