Skip to content

Commit 14c29f5

Browse files
author
im-vedant
committed
Added disable check scripts
1 parent 11e1121 commit 14c29f5

File tree

3 files changed

+358
-0
lines changed

3 files changed

+358
-0
lines changed

.github/workflows/pull-request.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,48 @@ jobs:
3030
run: docker buildx build --file ./docker/api.Containerfile --tag talawa_api --target non_production ./
3131
- name: Check type errors
3232
run: docker container run talawa_api pnpm check_code_quality
33+
34+
35+
check_eslint_disable:
36+
name: Check for eslint-disable
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: Checkout code
40+
uses: actions/checkout@v4
41+
42+
- name: Get changed files
43+
id: changed-files
44+
uses: tj-actions/changed-files@v45
45+
46+
- name: Set up Python
47+
uses: actions/setup-python@v5
48+
with:
49+
python-version: 3.9
50+
51+
- name: Run Python script
52+
run: |
53+
python .github/workflows/scripts/eslint_disable_check.py --files ${{ steps.changed-files.outputs.all_changed_files }}
54+
55+
check_code_coverage_disable:
56+
name: Check for code coverage disable
57+
runs-on: ubuntu-latest
58+
steps:
59+
- name: Checkout code
60+
uses: actions/checkout@v4
61+
62+
- name: Get changed files
63+
id: changed-files
64+
uses: tj-actions/changed-files@v45
65+
66+
- name: Set up Python
67+
uses: actions/setup-python@v5
68+
with:
69+
python-version: 3.9
70+
71+
- name: Run Python script
72+
run: |
73+
python .github/workflows/scripts/code_coverage_disable_check.py --files ${{ steps.changed-files.outputs.all_changed_files }}
74+
3375
check_gql_tada:
3476
name: Check gql tada files and configuration
3577
runs-on: ubuntu-latest
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
"""Code Coverage Disable Checker Script.
2+
3+
Methodology:
4+
5+
Recursively analyzes TypeScript files in the specified directories or
6+
checks specific files
7+
to ensure they do not contain code coverage disable statements.
8+
9+
This script enforces proper code coverage practices in the project.
10+
11+
NOTE:
12+
This script complies with our python3 coding and documentation standards.
13+
It complies with:
14+
15+
1) Pylint
16+
2) Pydocstyle
17+
3) Pycodestyle
18+
4) Flake8
19+
5) Python Black
20+
21+
"""
22+
23+
import os
24+
import re
25+
import argparse
26+
import sys
27+
28+
29+
def has_code_coverage_disable(file_path):
30+
"""
31+
Check if a TypeScript file contains code coverage disable statements.
32+
33+
Args:
34+
file_path (str): Path to the TypeScript file.
35+
36+
Returns:
37+
bool: True if code coverage disable statement is found, False
38+
otherwise.
39+
"""
40+
code_coverage_disable_pattern = re.compile(
41+
r"""//?\s*istanbul\s+ignore(?:\s+(?:next|-line))?[^\n]*|
42+
/\*\s*istanbul\s+ignore\s+(?:next|-line)\s*\*/""",
43+
re.IGNORECASE,
44+
)
45+
try:
46+
with open(file_path, "r", encoding="utf-8") as file:
47+
content = file.read()
48+
return bool(code_coverage_disable_pattern.search(content))
49+
except FileNotFoundError:
50+
print(f"File not found: {file_path}")
51+
return False
52+
except PermissionError:
53+
print(f"Permission denied: {file_path}")
54+
return False
55+
except (IOError, OSError) as e:
56+
print(f"Error reading file {file_path}: {e}")
57+
return False
58+
59+
60+
def check_code_coverage(files_or_dirs):
61+
"""
62+
Check TypeScript files for code coverage disable statements.
63+
64+
Args:
65+
files_or_dirs (list): List of files or directories to check.
66+
67+
Returns:
68+
bool: True if code coverage disable statement is found, False
69+
otherwise.
70+
"""
71+
code_coverage_found = False
72+
73+
for item in files_or_dirs:
74+
if os.path.isdir(item):
75+
# If it's a directory, recursively walk through the files in it
76+
for root, _, files in os.walk(item):
77+
if "node_modules" in root:
78+
continue
79+
for file_name in files:
80+
if (
81+
file_name.endswith(".ts")
82+
and not file_name.endswith(".test.ts")
83+
and not file_name.endswith(".spec.ts")
84+
):
85+
file_path = os.path.join(root, file_name)
86+
if has_code_coverage_disable(file_path):
87+
print(
88+
f"""File {file_path} contains code coverage disable statement."""
89+
)
90+
code_coverage_found = True
91+
elif os.path.isfile(item):
92+
# If it's a file, check it directly
93+
if (
94+
item.endswith(".ts")
95+
and not item.endswith(".test.ts")
96+
and not item.endswith(".spec.ts")
97+
):
98+
if has_code_coverage_disable(item):
99+
print(
100+
f"""File {item} contains code coverage disable statement. Please remove it and add the appropriate tests."""
101+
)
102+
code_coverage_found = True
103+
104+
return code_coverage_found
105+
106+
107+
def arg_parser_resolver():
108+
"""Resolve the CLI arguments provided by the user.
109+
110+
Returns:
111+
result: Parsed argument object
112+
"""
113+
parser = argparse.ArgumentParser()
114+
parser.add_argument(
115+
"--directory",
116+
type=str,
117+
nargs="+",
118+
default=[os.getcwd()],
119+
help="""One or more directories to check for code coverage disable
120+
statements (default: current directory).""",
121+
)
122+
parser.add_argument(
123+
"--files",
124+
type=str,
125+
nargs="+",
126+
default=[],
127+
help="""One or more files to check directly for code coverage disable
128+
statements (default: check directories).""",
129+
)
130+
return parser.parse_args()
131+
132+
133+
def main():
134+
"""
135+
Execute the script's main functionality.
136+
137+
This function serves as the entry point for the script. It performs
138+
the following tasks:
139+
1. Validates and retrieves the files or directories to check from
140+
command line arguments.
141+
2. Checks files or directories for code coverage disable statements.
142+
3. Provides informative messages based on the analysis.
143+
4. Exits with an error if code coverage disable statements are found.
144+
145+
Raises:
146+
SystemExit: If an error occurs during execution.
147+
"""
148+
args = arg_parser_resolver()
149+
files_or_dirs = args.files if args.files else args.directory
150+
# Check code coverage in the specified files or directories
151+
code_coverage_found = check_code_coverage(files_or_dirs)
152+
153+
if code_coverage_found:
154+
print("Code coverage disable check failed. Exiting with error.")
155+
sys.exit(1)
156+
157+
print("Code coverage disable check completed successfully.")
158+
159+
160+
if __name__ == "__main__":
161+
main()
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: UTF-8 -*-
3+
"""ESLint Checker Script.
4+
5+
Methodology:
6+
7+
Recursively analyzes TypeScript files in the specified directory
8+
or checks specific files directly to ensure they do not contain
9+
eslint-disable statements.
10+
11+
This script enforces code quality practices in the project.
12+
13+
NOTE:
14+
This script complies with our python3 coding and documentation standards.
15+
It complies with:
16+
17+
1) Pylint
18+
2) Pydocstyle
19+
3) Pycodestyle
20+
4) Flake8
21+
5) Python Black
22+
23+
"""
24+
import os
25+
import re
26+
import argparse
27+
import sys
28+
29+
30+
def has_eslint_disable(file_path):
31+
"""
32+
Check if a TypeScript file contains eslint-disable statements.
33+
34+
Args:
35+
file_path (str): Path to the TypeScript file.
36+
37+
Returns:
38+
bool: True if eslint-disable statement is found, False otherwise.
39+
"""
40+
eslint_disable_pattern = re.compile(
41+
r"""\/\/\s*eslint-disable(?:-next-line
42+
|-line)?[^\n]*|\/\*\s*eslint-disable[^\*]*\*\/""",
43+
re.IGNORECASE,
44+
)
45+
46+
try:
47+
with open(file_path, "r", encoding="utf-8") as file:
48+
content = file.read()
49+
return bool(eslint_disable_pattern.search(content))
50+
except FileNotFoundError:
51+
print(f"File not found: {file_path}")
52+
return False
53+
except PermissionError:
54+
print(f"Permission denied: {file_path}")
55+
return False
56+
except (IOError, OSError) as e:
57+
print(f"Error reading file {file_path}: {e}")
58+
return False
59+
60+
61+
def check_eslint(files_or_directories):
62+
"""
63+
Check TypeScript files for eslint-disable statements.
64+
65+
Args:
66+
files_or_directories (list): List of files or directories to check.
67+
68+
Returns:
69+
bool: True if eslint-disable statement is found, False otherwise.
70+
"""
71+
eslint_found = False
72+
73+
for item in files_or_directories:
74+
if os.path.isfile(item):
75+
# If it's a file, directly check it
76+
if item.endswith(".ts") :
77+
if has_eslint_disable(item):
78+
print(f"File {item} contains eslint-disable statement. Please remove them and ensure the code adheres to the specified ESLint rules.")
79+
eslint_found = True
80+
elif os.path.isdir(item):
81+
# If it's a directory, walk through it and check all
82+
# .ts and .tsx files
83+
for root, _, files in os.walk(item):
84+
if "node_modules" in root:
85+
continue
86+
for file_name in files:
87+
if file_name.endswith(".ts") :
88+
file_path = os.path.join(root, file_name)
89+
if has_eslint_disable(file_path):
90+
print(
91+
f"""File {file_path} contains eslint-disable
92+
statement."""
93+
)
94+
eslint_found = True
95+
96+
return eslint_found
97+
98+
99+
def arg_parser_resolver():
100+
"""Resolve the CLI arguments provided by the user.
101+
102+
Returns:
103+
result: Parsed argument object
104+
"""
105+
parser = argparse.ArgumentParser()
106+
parser.add_argument(
107+
"--files",
108+
type=str,
109+
nargs="+",
110+
default=[],
111+
help="""List of files to check for eslint disable
112+
statements (default: None).""",
113+
)
114+
parser.add_argument(
115+
"--directory",
116+
type=str,
117+
nargs="+",
118+
default=[os.getcwd()],
119+
help="""One or more directories to check for eslint disable
120+
statements (default: current directory).""",
121+
)
122+
return parser.parse_args()
123+
124+
125+
def main():
126+
"""
127+
Execute the script's main functionality.
128+
129+
This function serves as the entry point for the script. It performs
130+
the following tasks:
131+
1. Validates and retrieves the files and directories to check from
132+
command line arguments.
133+
2. Recursively checks TypeScript files for eslint-disable statements.
134+
3. Provides informative messages based on the analysis.
135+
4. Exits with an error if eslint-disable statements are found.
136+
137+
Raises:
138+
SystemExit: If an error occurs during execution.
139+
"""
140+
args = arg_parser_resolver()
141+
142+
# Determine whether to check files or directories based on the arguments
143+
files_or_directories = args.files if args.files else args.directory
144+
# Check eslint in the specified files or directories
145+
eslint_found = check_eslint(files_or_directories)
146+
147+
if eslint_found:
148+
print("ESLint-disable check failed. Exiting with error.")
149+
sys.exit(1)
150+
151+
print("ESLint-disable check completed successfully.")
152+
153+
154+
if __name__ == "__main__":
155+
main()

0 commit comments

Comments
 (0)