forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_frontend_test_coverage.py
254 lines (220 loc) · 9.06 KB
/
check_frontend_test_coverage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Copyright 2020 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Check for decrease in coverage from 100% of frontend files."""
from __future__ import annotations
import fnmatch
import logging
import os
import re
import sys
from core import python_utils
LCOV_FILE_PATH = os.path.join(os.pardir, 'karma_coverage_reports', 'lcov.info')
RELEVANT_LCOV_LINE_PREFIXES = ['SF', 'LH', 'LF']
EXCLUDED_DIRECTORIES = [
'node_modules/*',
'extensions/classifiers/proto/*'
]
# Contains the name of all files that is not 100% coverage.
# This list must be kept up-to-date; the changes (only remove) should be done
# manually.
# Please keep the list in alphabetical order.
# NOTE TO DEVELOPERS: do not add any new files to this list without asking
# @nithusha21 first.
NOT_FULLY_COVERED_FILENAMES = [
'angular-html-bind.directive.ts',
'answer-classification.service.ts',
'App.ts',
'audio-preloader.service.ts',
'Base.ts',
'ck-editor-4-rte.component.ts',
'ck-editor-4-widgets.initializer.ts',
'collection-player-page.directive.ts',
'collection.model.ts',
'contribution-and-review.service.ts',
'conversation-skin.directive.ts',
'current-interaction.service.ts',
'exploration-states.service.ts',
'expression-evaluator.service.ts',
'expression-interpolation.service.ts',
'fatigue-detection.service.ts',
'google-analytics.initializer.ts',
'language-util.service.ts',
'learner-answer-info.service.ts',
'mathjax-bind.directive.ts',
'normalize-whitespace-punctuation-and-case.pipe.ts',
'object-editor.directive.ts',
'oppia-footer.component.ts',
'oppia-interactive-music-notes-input.directive.ts',
'oppia-interactive-pencil-code-editor.directive.ts',
'oppia-root.directive.ts',
'parameterize-rule-description.filter.ts',
'player-correctness-feedback-enabled.service.ts',
'player-transcript.service.ts',
'python-program.tokenizer.ts',
'question-update.service.ts',
'refresher-exploration-confirmation-modal.service.ts',
'release-coordinator-page.component.ts',
'rule-type-selector.directive.ts',
'schema-based-custom-viewer.directive.ts',
'schema-based-html-viewer.directive.ts',
'schema-based-list-viewer.directive.ts',
'select2-dropdown.directive.ts',
'state-card.model.ts',
'state-content-editor.directive.ts',
'state-interaction-editor.directive.ts',
'story-node.model.ts',
'subtopic.model.ts',
'translation-file-hash-loader-backend-api.service.ts',
'truncate-and-capitalize.filter.ts',
'truncate-and-capitalize.pipe.ts',
'truncate-input-based-on-interaction-answer-type.filter.ts',
'truncate.filter.ts',
# Please don't try to cover `unit-test-utils.ajs.ts` file.
'unit-test-utils.ajs.ts',
'voiceover-recording.service.ts',
]
class LcovStanzaRelevantLines:
"""Gets the relevant lines from a lcov stanza."""
def __init__(self, stanza):
"""Initialize the object which provides relevant data of a lcov
stanza in order to calculate any decrease in frontend test coverage.
Args:
stanza: list(str). Contains all the lines from a lcov stanza.
Raises:
Exception. The file_path is empty.
Exception. Total lines number is not found.
Exception. Covered lines number is not found.
"""
match = re.search('SF:(.+)\n', stanza)
if match is None:
raise Exception(
'The test path is empty or null. '
'It\'s not possible to diff the test coverage correctly.')
_, file_name = os.path.split(match.group(1))
self.file_name = file_name
self.file_path = match.group(1)
match = re.search(r'LF:(\d+)\n', stanza)
if match is None:
raise Exception(
'It wasn\'t possible to get the total lines of {} file.'
'It\'s not possible to diff the test coverage correctly.'
.format(file_name))
self.total_lines = int(match.group(1))
match = re.search(r'LH:(\d+)\n', stanza)
if match is None:
raise Exception(
'It wasn\'t possible to get the covered lines of {} file.'
'It\'s not possible to diff the test coverage correctly.'
.format(file_name))
self.covered_lines = int(match.group(1))
def get_stanzas_from_lcov_file():
"""Get all stanzas from a lcov file. The lcov file gather all the frontend
files that has tests and each one has the following structure:
TN: test name
SF: file path
FNF: total functions
FNH: functions covered
LF: total lines
LH: lines covered
BRF: total branches
BRH: branches covered
end_of_record
Returns:
list(LcovStanzaRelevantLines). A list with all stanzas.
"""
f = python_utils.open_file(LCOV_FILE_PATH, 'r')
lcov_items_list = f.read().split('end_of_record')
stanzas_list = []
for item in lcov_items_list:
if item.strip('\n'):
stanza = LcovStanzaRelevantLines(item)
stanzas_list.append(stanza)
return stanzas_list
def check_not_fully_covered_filenames_list_is_sorted():
"""Check if NOT_FULLY_COVERED_FILENAMES list is in alphabetical order."""
if NOT_FULLY_COVERED_FILENAMES != sorted(
NOT_FULLY_COVERED_FILENAMES, key=lambda s: s.lower()):
logging.error(
'The \033[1mNOT_FULLY_COVERED_FILENAMES\033[0m list must be'
' kept in alphabetical order.')
sys.exit(1)
def check_coverage_changes():
"""Checks if the denylist for not fully covered files needs to be changed
by:
- File renaming
- File deletion
Raises:
Exception. LCOV_FILE_PATH doesn't exist.
"""
if not os.path.exists(LCOV_FILE_PATH):
raise Exception(
'Expected lcov file to be available at {}, but the'
' file does not exist.'.format(LCOV_FILE_PATH))
stanzas = get_stanzas_from_lcov_file()
remaining_denylisted_files = list(NOT_FULLY_COVERED_FILENAMES)
errors = ''
for stanza in stanzas:
file_name = stanza.file_name
total_lines = stanza.total_lines
covered_lines = stanza.covered_lines
if any(fnmatch.fnmatch(
stanza.file_path, pattern) for pattern in EXCLUDED_DIRECTORIES):
continue
if file_name not in remaining_denylisted_files:
if total_lines != covered_lines:
errors += (
'\033[1m{}\033[0m seems to be not completely tested.'
' Make sure it\'s fully covered.\n'.format(file_name))
else:
if total_lines == covered_lines:
errors += (
'\033[1m{}\033[0m seems to be fully covered!'
' Before removing it manually from the denylist'
' in the file'
' scripts/check_frontend_test_coverage.py, please'
' make sure you\'ve followed the unit tests rules'
' correctly on:'
' https://github.com/oppia/oppia/wiki/Frontend'
'-unit-tests-guide#rules\n'.format(file_name))
remaining_denylisted_files.remove(file_name)
if remaining_denylisted_files:
for test_name in remaining_denylisted_files:
errors += (
'\033[1m{}\033[0m is in the frontend test coverage'
' denylist but it doesn\'t exist anymore. If you have'
' renamed it, please make sure to remove the old file'
' name and add the new file name in the denylist in'
' the file scripts/check_frontend_test_coverage.py.\n'
.format(test_name))
if errors:
python_utils.PRINT('------------------------------------')
python_utils.PRINT('Frontend Coverage Checks Not Passed.')
python_utils.PRINT('------------------------------------')
logging.error(errors)
sys.exit(1)
else:
python_utils.PRINT('------------------------------------')
python_utils.PRINT('All Frontend Coverage Checks Passed.')
python_utils.PRINT('------------------------------------')
check_not_fully_covered_filenames_list_is_sorted()
def main():
"""Runs all the steps for checking if there is any decrease of 100% covered
files in the frontend.
"""
check_coverage_changes()
# The 'no coverage' pragma is used as this line is un-testable. This is because
# it will only be called when check_frontend_test_coverage.py
# is used as a script.
if __name__ == '__main__': # pragma: no cover
main()