Skip to content

Commit 6e68380

Browse files
author
im-vedant
committed
Formatted code_coverage_disable_check.py to comply with all coding and documentation standards.
1 parent 70fc259 commit 6e68380

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

.github/workflows/code_coverage_disable_check.py

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
33
Methodology:
44
5-
Recursively analyzes TypeScript files in the specified directories to ensure
6-
they do not contain code coverage disable statements.
5+
Recursively analyzes TypeScript files in the specified directories to
6+
ensure they do not contain code coverage disable statements.
77
88
This script enforces proper code coverage practices in the project.
99
1010
NOTE:
11-
1211
This script complies with our python3 coding and documentation standards.
1312
It complies with:
1413
1514
1) Pylint
1615
2) Pydocstyle
1716
3) Pycodestyle
1817
4) Flake8
18+
5) Python Black
1919
2020
"""
2121

@@ -24,6 +24,7 @@
2424
import argparse
2525
import sys
2626

27+
2728
def has_code_coverage_disable(file_path):
2829
"""
2930
Check if a TypeScript file contains code coverage disable statements.
@@ -32,18 +33,30 @@ def has_code_coverage_disable(file_path):
3233
file_path (str): Path to the TypeScript file.
3334
3435
Returns:
35-
bool: True if code coverage disable statement is found, False otherwise.
36+
bool: True if code coverage disable statement is found, False
37+
otherwise.
3638
"""
37-
code_coverage_disable_pattern = re.compile(r'(?://\s*istanbul\s+ignore(?:-next-line|-line)?|/\*\s*istanbul\s+ignore\s*(?:next|-line)\s*\*/)', re.IGNORECASE)
38-
39+
code_coverage_disable_pattern = re.compile(
40+
r"""(?://\s*istanbul\s+ignore(?:-next-line|-line)?
41+
|/\*\s*istanbul\s+ignore\s*(?:next|-line)\s*\*/)""",
42+
re.IGNORECASE,
43+
)
44+
3945
try:
40-
with open(file_path, 'r', encoding='utf-8') as file:
46+
with open(file_path, "r", encoding="utf-8") as file:
4147
content = file.read()
4248
return bool(code_coverage_disable_pattern.search(content))
43-
except Exception as e:
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:
4456
print(f"Error reading file {file_path}: {e}")
4557
return False
4658

59+
4760
def check_code_coverage(directories):
4861
"""
4962
Recursively check TypeScript files for code coverage disable statements.
@@ -52,29 +65,46 @@ def check_code_coverage(directories):
5265
directories (list) : List of directories.
5366
5467
Returns:
55-
bool: True if code coverage disable statement is found, False otherwise.
68+
bool: True if code coverage disable statement is found, False
69+
otherwise.
5670
"""
5771
code_coverage_found = False
5872

5973
for directory in directories:
6074
if not os.path.exists(directory):
61-
print(f"Error: The specified directory '{directory}' does not exist.")
75+
print(
76+
f"""Error: The specified directory '{directory}' does
77+
not exist."""
78+
)
6279
sys.exit(1)
63-
for root, dirs, files in os.walk(directory):
80+
for root, _, files in os.walk(directory):
6481
for file_name in files:
65-
if file_name.endswith('.tsx') and not file_name.endswith('.test.tsx'):
82+
if (
83+
file_name.endswith(".tsx")
84+
and not file_name.endswith(".test.tsx")
85+
):
6686
file_path = os.path.join(root, file_name)
6787
if has_code_coverage_disable(file_path):
68-
print(f'File {file_path} contains code coverage disable statement.')
88+
print(
89+
f"""File {file_path} contains code coverage disable
90+
statement."""
91+
)
6992
code_coverage_found = True
7093

71-
setup_path = os.path.join(directory, 'setup.ts')
72-
if os.path.exists(setup_path) and has_code_coverage_disable(setup_path):
73-
print(f'Setup file {setup_path} contains code coverage disable statement.')
94+
setup_path = os.path.join(directory, "setup.ts")
95+
if (
96+
os.path.exists(setup_path)
97+
and has_code_coverage_disable(setup_path)
98+
):
99+
print(
100+
f"""Setup file {setup_path} contains code coverage disable
101+
statement."""
102+
)
74103
code_coverage_found = True
75104

76105
return code_coverage_found
77106

107+
78108
def arg_parser_resolver():
79109
"""Resolve the CLI arguments provided by the user.
80110
@@ -85,12 +115,14 @@ def arg_parser_resolver():
85115
parser.add_argument(
86116
"--directory",
87117
type=str,
88-
nargs='+',
118+
nargs="+",
89119
default=[os.getcwd()],
90-
help="One or more directories to check for code coverage disable statements (default: current directory)."
120+
help="""One or more directories to check for code coverage disable
121+
statements (default: current directory).""",
91122
)
92123
return parser.parse_args()
93124

125+
94126
def main():
95127
"""
96128
Execute the script's main functionality.
@@ -99,16 +131,16 @@ def main():
99131
the following tasks:
100132
1. Validates and retrieves the directory to check from
101133
command line arguments.
102-
2. Recursively checks TypeScript files for code coverage disable statements.
134+
2. Recursively checks TypeScript files for code coverage disable
135+
statements.
103136
3. Provides informative messages based on the analysis.
104137
4. Exits with an error if code coverage disable statements are found.
105138
106139
Raises:
107140
SystemExit: If an error occurs during execution.
108141
"""
109142
args = arg_parser_resolver()
110-
print("Directories to check: ", args.directory)
111-
143+
112144
# Check code coverage in the specified directory
113145
code_coverage_found = check_code_coverage(args.directory)
114146

@@ -118,5 +150,6 @@ def main():
118150

119151
print("Code coverage disable check completed successfully.")
120152

153+
121154
if __name__ == "__main__":
122155
main()

0 commit comments

Comments
 (0)