Skip to content

Commit

Permalink
Add conditional mark sort check to pre-commit hook
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 conditional mark sort check to pre-commit hook, if testcases are not
sorted, pre-commit will fail, need to fix it before PR get merged.
  • Loading branch information
xwjiang-ms committed Apr 26, 2023
1 parent 502afe7 commit c219273
Show file tree
Hide file tree
Showing 10 changed files with 95 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.

51 changes: 51 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[metadata]
name = Some pre commit hooks
version = 1.0.0+pre_commit
description = Some hooks for pre-commit.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/sonic-net/sonic-mgmt
classifiers =
License :: OSI Approved :: MIT License
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: Implementation :: CPython
Programming Language :: Python :: Implementation :: PyPy

[options]
packages = find:
package_dir =
=.hooks
install_requires =
ruamel.yaml>=0.15
tomli>=1.1.0;python_version<"3.11"
python_requires = >=3.7

[options.packages.find]
where = .hooks
exclude =
spytest*

[options.entry_points]
console_scripts =
check-conditional-mark-sort = pre_commit_hooks.check_conditional_mark_sort:main

[bdist_wheel]
universal = True

[coverage:run]
plugins = covdefaults

[mypy]
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
warn_redundant_casts = true
warn_unused_ignores = true

[mypy-testing.*]
disallow_untyped_defs = false

[mypy-tests.*]
disallow_untyped_defs = false
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Local pre-commit hook setup
from __future__ import annotations

from setuptools import setup
setup()

0 comments on commit c219273

Please sign in to comment.