Skip to content

Commit

Permalink
Add conditional mark sort check to pre-commit hook (#8162)
Browse files Browse the repository at this point in the history
What is the motivation for this PR?
In conditional mark, testcases should be sorted from A to Z, and there is no support to check it in PR test.

How did you do it?
Add a pre-commit hook under sonic-mgmt repo to check conditional mark sort, if testcases are not sorted, pre-commit will fail, need to fix it before PR get merged.
  • Loading branch information
xwjiang-ms authored May 4, 2023
1 parent f8cdd33 commit b886f21
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 47 deletions.
7 changes: 0 additions & 7 deletions .hooks/README.md

This file was deleted.

13 changes: 0 additions & 13 deletions .hooks/pre-commit

This file was deleted.

24 changes: 0 additions & 24 deletions .hooks/pre-commit.py

This file was deleted.

Empty file.
28 changes: 28 additions & 0 deletions .hooks/pre_commit_hooks/check_conditional_mark_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import re
import sys


def main():
stage_files = sys.argv[1:]
retval = 0
for stage_file in stage_files:
if "tests_mark_conditions" in stage_file:
conditions = []
with open(stage_file, 'r') as f:
file_contents = f.readlines()
if not file_contents:
continue
for line in file_contents:
if re.match('^[a-zA-Z]', line):
conditions.append(line.strip().rstrip(":"))
sorted_conditions = conditions[:]
sorted_conditions.sort()
if conditions != sorted_conditions:
print("The entries in tests/common/plugins/conditional_mark/tests_mark_conditions*.yaml "
"are not sorted in alphabetic order.")
retval = 1
return retval


if __name__ == "__main__":
raise SystemExit(main())
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ repos:
hooks:
- id: flake8
args: ["--max-line-length=120"]

- repo: https://github.com/sonic-net/sonic-mgmt
rev: 1.0.0+pre_commit
hooks:
- id: check-conditional-mark-sort
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- id: check-conditional-mark-sort
name: check conditional mark sort
description: check conditional mark yaml file sort from A to Z.
entry: check-conditional-mark-sort
language: python
types: [yaml]
3 changes: 0 additions & 3 deletions init_hooks.sh

This file was deleted.

32 changes: 32 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Local pre-commit hook config
[project]
name = "sonic-mgmt-pre-commit-hooks"
version = "1.0.0+pre_commit"
description = "Some hooks for pre-commit in sonic-mgmt repo."
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
requires-python = ">=3.7"
dependencies = ["tomli>=2.0.1;python_version<'4.0'"]

[project.urls]
Homepage = "https://github.com/sonic-net/sonic-mgmt"

[tool.setuptools]
package-dir = {"" = ".hooks"}

[tool.setuptools.packages.find]
where = [".hooks"]

[project.scripts]
check-conditional-mark-sort = 'pre_commit_hooks.check_conditional_mark_sort:main'

[tool.black]
exclude = '''
/(
spytest/
)/
'''

0 comments on commit b886f21

Please sign in to comment.