forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_frontend_test_coverage_test.py
367 lines (330 loc) · 13.6 KB
/
check_frontend_test_coverage_test.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# 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.
"""Unit tests for scripts/check_frontend_test_coverage.py."""
from __future__ import annotations
import os
import subprocess
import sys
from core import python_utils
from core.tests import test_utils
from . import check_frontend_test_coverage
class CheckFrontendCoverageTests(test_utils.GenericTestBase):
def setUp(self):
super(CheckFrontendCoverageTests, self).setUp()
self.lcov_items_list = None
self.check_function_calls = {
'open_file_is_called': False,
'exists_is_called': False,
}
self.expected_check_function_calls = {
'open_file_is_called': True,
'exists_is_called': True,
}
self.printed_messages = []
class MockFile:
def __init__(self, lcov_items_list):
self.lcov_items_list = lcov_items_list
def read(self): # pylint: disable=missing-docstring
return self.lcov_items_list
def mock_open_file(
file_name, option
): # pylint: disable=unused-argument
self.check_function_calls['open_file_is_called'] = True
return MockFile(self.lcov_items_list)
def mock_exists(unused_path):
self.check_function_calls['exists_is_called'] = True
return True
def mock_print(message):
self.printed_messages.append(message)
def mock_check_call(command): # pylint: disable=unused-argument
self.check_function_calls['check_call_is_called'] = True
self.open_file_swap = self.swap(
python_utils, 'open_file', mock_open_file
)
self.exists_swap = self.swap(os.path, 'exists', mock_exists)
self.print_swap = self.swap(python_utils, 'PRINT', mock_print)
self.check_call_swap = self.swap(
subprocess, 'check_call', mock_check_call
)
def test_get_stanzas_from_lcov_file(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:5\n'
'end_of_record\n'
'SF:/opensource/oppia/file2.ts\n'
'LF:10\n'
'LH:5\n'
'end_of_record\n'
'SF:/opensource/oppia/file3.ts\n'
'LF:10\n'
'LH:5\n'
'end_of_record\n'
)
with self.open_file_swap:
stanzas = check_frontend_test_coverage.get_stanzas_from_lcov_file()
self.assertEqual(stanzas[0].file_name, 'file.ts')
self.assertEqual(stanzas[0].total_lines, 10)
self.assertEqual(stanzas[0].covered_lines, 5)
self.assertEqual(stanzas[1].file_name, 'file2.ts')
self.assertEqual(stanzas[1].total_lines, 10)
self.assertEqual(stanzas[1].covered_lines, 5)
self.assertEqual(stanzas[2].file_name, 'file3.ts')
self.assertEqual(stanzas[2].total_lines, 10)
self.assertEqual(stanzas[2].covered_lines, 5)
def test_get_stanzas_from_lcov_file_file_name_exception(self):
self.lcov_items_list = (
'SF:\n'
'LF:10\n'
'LH:5\n'
'end_of_record\n'
)
with self.open_file_swap:
with self.assertRaisesRegexp(
Exception,
'The test path is empty or null. '
'It\'s not possible to diff the test coverage correctly.',
):
check_frontend_test_coverage.get_stanzas_from_lcov_file()
def test_get_stanzas_from_lcov_file_total_lines_exception(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:\n'
'LH:5\n'
'end_of_record\n'
)
with self.open_file_swap:
with self.assertRaisesRegexp(
Exception,
'It wasn\'t possible to get the total lines of file.ts file.'
'It\'s not possible to diff the test coverage correctly.',
):
check_frontend_test_coverage.get_stanzas_from_lcov_file()
def test_get_stanzas_from_lcov_file_covered_lines_exception(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:\n'
'end_of_record\n'
)
with self.open_file_swap:
with self.assertRaisesRegexp(
Exception,
'It wasn\'t possible to get the covered lines of file.ts file.'
'It\'s not possible to diff the test coverage correctly.',
):
check_frontend_test_coverage.get_stanzas_from_lcov_file()
def test_check_coverage_changes(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
'SF:/opensource/oppia/file2.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
)
not_fully_covered_files_swap = self.swap(
check_frontend_test_coverage,
'NOT_FULLY_COVERED_FILENAMES',
['file.ts', 'file2.ts'],
)
check_function_calls = {'sys_exit_is_called': False}
expected_check_function_calls = {'sys_exit_is_called': False}
def mock_sys_exit(error_message): # pylint: disable=unused-argument
check_function_calls['sys_exit_is_called'] = True
sys_exit_swap = self.swap(sys, 'exit', mock_sys_exit)
with sys_exit_swap, self.exists_swap, self.open_file_swap, self.print_swap: # pylint: disable=line-too-long
with not_fully_covered_files_swap:
check_frontend_test_coverage.check_coverage_changes()
self.assertEqual(
check_function_calls, expected_check_function_calls
)
def test_check_coverage_changes_error(self):
def mock_exists(unused_path):
return False
exists_swap = self.swap(os.path, 'exists', mock_exists)
with exists_swap:
with self.assertRaisesRegexp(
Exception,
'Expected lcov file to be'
r' available at [A-Za-z\._/]+, but the file does not exist.',
):
check_frontend_test_coverage.check_coverage_changes()
def test_check_coverage_changes_for_covered_files(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
'SF:node_modules/oppia/anotherfile.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
)
not_fully_covered_files_swap = self.swap(
check_frontend_test_coverage, 'NOT_FULLY_COVERED_FILENAMES', []
)
with self.exists_swap, self.open_file_swap, self.print_swap:
with not_fully_covered_files_swap, self.capture_logging() as logs:
with self.assertRaisesRegexp(SystemExit, '1'):
check_frontend_test_coverage.check_coverage_changes()
self.assertEqual(
logs,
[
'\033[1mfile.ts\033[0m seems to be not completely '
'tested. Make sure it\'s fully covered.'
],
)
def test_check_coverage_changes_remove_file(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:10\n'
'end_of_record\n'
)
not_fully_covered_files_swap = self.swap(
check_frontend_test_coverage,
'NOT_FULLY_COVERED_FILENAMES',
['file.ts'],
)
with self.exists_swap, self.open_file_swap, self.print_swap:
with not_fully_covered_files_swap, self.capture_logging() as logs:
with self.assertRaisesRegexp(SystemExit, '1'):
check_frontend_test_coverage.check_coverage_changes()
self.assertEqual(
logs,
[
'\033[1mfile.ts\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'
],
)
def test_check_coverage_changes_when_renaming_file(self):
self.lcov_items_list = (
'SF:/opensource/oppia/newfilename.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
)
not_fully_covered_files_swap = self.swap(
check_frontend_test_coverage,
'NOT_FULLY_COVERED_FILENAMES',
['file.ts'],
)
with self.exists_swap, self.open_file_swap, self.print_swap:
with not_fully_covered_files_swap, self.capture_logging() as logs:
with self.assertRaisesRegexp(SystemExit, '1'):
check_frontend_test_coverage.check_coverage_changes()
self.assertEqual(
logs,
[
'\033[1mnewfilename.ts\033[0m seems to be not '
'completely tested. Make sure it\'s fully covered.\n'
'\033[1mfile.ts\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.'
],
)
def test_fully_covered_filenames_is_sorted(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
'SF:/opensource/oppia/anotherfile.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
'SF:node_modules/oppia/thirdfile.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
)
not_fully_covered_files_swap = self.swap(
check_frontend_test_coverage,
'NOT_FULLY_COVERED_FILENAMES',
['anotherfile.tsfile.ts'],
)
check_function_calls = {'sys_exit_is_called': False}
expected_check_function_calls = {'sys_exit_is_called': False}
def mock_sys_exit(error_message): # pylint: disable=unused-argument
check_function_calls['sys_exit_is_called'] = True
sys_exit_swap = self.swap(sys, 'exit', mock_sys_exit)
with sys_exit_swap, self.exists_swap, self.open_file_swap:
with self.print_swap, not_fully_covered_files_swap:
(
check_frontend_test_coverage
.check_not_fully_covered_filenames_list_is_sorted()
)
self.assertEqual(
check_function_calls, expected_check_function_calls
)
def test_fully_covered_filenames_is_not_sorted(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
'SF:/opensource/oppia/anotherfile.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
)
not_fully_covered_files_swap = self.swap(
check_frontend_test_coverage,
'NOT_FULLY_COVERED_FILENAMES',
['file.ts', 'anotherfile.ts'],
)
with self.exists_swap, self.open_file_swap, self.print_swap:
with not_fully_covered_files_swap, self.capture_logging() as logs:
with self.assertRaisesRegexp(SystemExit, '1'):
(
check_frontend_test_coverage
.check_not_fully_covered_filenames_list_is_sorted()
)
self.assertEqual(
logs,
[
'The \033[1mNOT_FULLY_COVERED_FILENAMES\033[0m list '
'must be kept in alphabetical order.'
],
)
def test_function_calls(self):
self.lcov_items_list = (
'SF:/opensource/oppia/file.ts\n'
'LF:10\n'
'LH:9\n'
'end_of_record\n'
)
not_fully_covered_files_swap = self.swap(
check_frontend_test_coverage,
'NOT_FULLY_COVERED_FILENAMES',
['file.ts'],
)
with self.check_call_swap, self.exists_swap, self.open_file_swap:
with not_fully_covered_files_swap:
check_frontend_test_coverage.main()
self.assertEqual(
self.check_function_calls, self.expected_check_function_calls
)