2
2
3
3
Methodology:
4
4
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.
7
7
8
8
This script enforces proper code coverage practices in the project.
9
9
10
10
NOTE:
11
-
12
11
This script complies with our python3 coding and documentation standards.
13
12
It complies with:
14
13
15
14
1) Pylint
16
15
2) Pydocstyle
17
16
3) Pycodestyle
18
17
4) Flake8
18
+ 5) Python Black
19
19
20
20
"""
21
21
24
24
import argparse
25
25
import sys
26
26
27
+
27
28
def has_code_coverage_disable (file_path ):
28
29
"""
29
30
Check if a TypeScript file contains code coverage disable statements.
@@ -32,18 +33,30 @@ def has_code_coverage_disable(file_path):
32
33
file_path (str): Path to the TypeScript file.
33
34
34
35
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.
36
38
"""
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
+
39
45
try :
40
- with open (file_path , 'r' , encoding = ' utf-8' ) as file :
46
+ with open (file_path , "r" , encoding = " utf-8" ) as file :
41
47
content = file .read ()
42
48
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 :
44
56
print (f"Error reading file { file_path } : { e } " )
45
57
return False
46
58
59
+
47
60
def check_code_coverage (directories ):
48
61
"""
49
62
Recursively check TypeScript files for code coverage disable statements.
@@ -52,29 +65,46 @@ def check_code_coverage(directories):
52
65
directories (list) : List of directories.
53
66
54
67
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.
56
70
"""
57
71
code_coverage_found = False
58
72
59
73
for directory in directories :
60
74
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
+ )
62
79
sys .exit (1 )
63
- for root , dirs , files in os .walk (directory ):
80
+ for root , _ , files in os .walk (directory ):
64
81
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
+ ):
66
86
file_path = os .path .join (root , file_name )
67
87
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
+ )
69
92
code_coverage_found = True
70
93
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
+ )
74
103
code_coverage_found = True
75
104
76
105
return code_coverage_found
77
106
107
+
78
108
def arg_parser_resolver ():
79
109
"""Resolve the CLI arguments provided by the user.
80
110
@@ -85,12 +115,14 @@ def arg_parser_resolver():
85
115
parser .add_argument (
86
116
"--directory" ,
87
117
type = str ,
88
- nargs = '+' ,
118
+ nargs = "+" ,
89
119
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).""" ,
91
122
)
92
123
return parser .parse_args ()
93
124
125
+
94
126
def main ():
95
127
"""
96
128
Execute the script's main functionality.
@@ -99,16 +131,16 @@ def main():
99
131
the following tasks:
100
132
1. Validates and retrieves the directory to check from
101
133
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.
103
136
3. Provides informative messages based on the analysis.
104
137
4. Exits with an error if code coverage disable statements are found.
105
138
106
139
Raises:
107
140
SystemExit: If an error occurs during execution.
108
141
"""
109
142
args = arg_parser_resolver ()
110
- print ("Directories to check: " , args .directory )
111
-
143
+
112
144
# Check code coverage in the specified directory
113
145
code_coverage_found = check_code_coverage (args .directory )
114
146
@@ -118,5 +150,6 @@ def main():
118
150
119
151
print ("Code coverage disable check completed successfully." )
120
152
153
+
121
154
if __name__ == "__main__" :
122
155
main ()
0 commit comments