-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsetup_test.py
497 lines (429 loc) · 21.1 KB
/
setup_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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# coding: utf-8
#
# Copyright 2019 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/setup.py."""
from __future__ import annotations
import builtins
import collections
import os
import subprocess
import sys
import tarfile
from core.tests import test_utils
from typing import Final, List
from . import clean
from . import common
from . import setup
RELEASE_TEST_DIR: Final = os.path.join('core', 'tests', 'release_sources', '')
MOCK_TMP_UNTAR_PATH: Final = os.path.join(RELEASE_TEST_DIR, 'tmp_unzip.tar.gz')
TEST_DATA_DIR: Final = os.path.join('core', 'tests', 'data', '')
MOCK_YARN_PATH: Final = os.path.join(
TEST_DATA_DIR, 'yarn-v%s' % common.YARN_VERSION
)
class MockCD:
"""Mock for context manager for changing the current working directory."""
def __init__(self, unused_new_path: str) -> None:
pass
def __enter__(self) -> None:
pass
def __exit__(
self, unused_arg1: str, unused_arg2: str, unused_arg3: str
) -> None:
pass
class SetupTests(test_utils.GenericTestBase):
"""Test the methods for setup script."""
def setUp(self) -> None:
super().setUp()
self.check_function_calls = {
'create_directory_is_called': False,
'test_python_version_is_called': False,
'recursive_chown_is_called': False,
'recursive_chmod_is_called': False,
'rename_is_called': False,
'delete_file_is_called': False
}
self.urls: List[str] = []
def mock_create_directory(unused_path: str) -> None:
self.check_function_calls['create_directory_is_called'] = True
def mock_test_python_version() -> None:
self.check_function_calls['test_python_version_is_called'] = True
def mock_download_and_install_package(
url: str, unused_filename: str
) -> None:
self.urls.append(url)
def mock_recursive_chown(
unused_path: str, unused_uid: str, unused_gid: str
) -> None:
self.check_function_calls['recursive_chown_is_called'] = True
def mock_recursive_chmod(unused_path: str, unused_mode: str) -> None:
self.check_function_calls['recursive_chmod_is_called'] = True
def mock_uname() -> List[str]:
return ['Linux']
def mock_rename(unused_path1: str, unused_path2: str) -> None:
self.check_function_calls['rename_is_called'] = True
def mock_isfile(unused_path: str) -> bool:
return True
def mock_delete_file(unused_path: str) -> None:
self.check_function_calls['delete_file_is_called'] = True
def mock_get(unused_var: str) -> None:
return None
self.create_swap = self.swap(
setup, 'create_directory', mock_create_directory)
self.test_py_swap = self.swap(
setup, 'test_python_version', mock_test_python_version)
self.download_swap = self.swap(
setup, 'download_and_install_package',
mock_download_and_install_package)
self.exists_true_swap = self.swap_to_always_return(
os.path, 'exists', True)
self.exists_false_swap = self.swap_to_always_return(
os.path, 'exists', False)
self.is_x64_architecture_true_swap = self.swap_to_always_return(
common, 'is_x64_architecture', True)
self.is_x64_architecture_false_swap = self.swap_to_always_return(
common, 'is_x64_architecture', False)
self.chown_swap = self.swap(
common, 'recursive_chown', mock_recursive_chown)
self.chmod_swap = self.swap(
common, 'recursive_chmod', mock_recursive_chmod)
self.uname_swap = self.swap(os, 'uname', mock_uname)
self.rename_swap = self.swap(os, 'rename', mock_rename)
self.isfile_swap = self.swap(os.path, 'isfile', mock_isfile)
self.delete_swap = self.swap(clean, 'delete_file', mock_delete_file)
self.get_swap = self.swap(os.environ, 'get', mock_get)
self.cd_swap = self.swap(common, 'CD', MockCD)
version_info = collections.namedtuple(
'version_info', ['major', 'minor', 'micro'])
self.version_info_py38_swap = self.swap(
sys, 'version_info', version_info(major=3, minor=8, micro=15)
)
self.python2_print_swap = self.swap_with_checks(
builtins,
'print',
lambda *x: None,
expected_args=[(
'\033[91mThe Oppia server needs Python 2 to be installed. '
'Please follow the instructions at https://github.com/oppia/'
'oppia/wiki/Troubleshooting#python-2-is-not-available to fix '
'this.\033[0m',
)]
)
def test_create_directory_tree_with_missing_dir(self) -> None:
check_function_calls = {
'makedirs_is_called': False
}
def mock_makedirs(unused_path: str) -> None:
check_function_calls['makedirs_is_called'] = True
makedirs_swap = self.swap(os, 'makedirs', mock_makedirs)
with makedirs_swap, self.exists_false_swap:
setup.create_directory('dir')
self.assertTrue(check_function_calls['makedirs_is_called'])
def test_create_directory_tree_with_existing_dir(self) -> None:
check_function_calls = {
'makedirs_is_called': False
}
def mock_makedirs(unused_path: str) -> None:
check_function_calls['makedirs_is_called'] = True
makedirs_swap = self.swap(os, 'makedirs', mock_makedirs)
with makedirs_swap, self.exists_true_swap:
setup.create_directory('dir')
self.assertFalse(check_function_calls['makedirs_is_called'])
def test_python_version_testing_with_correct_version(self) -> None:
with self.version_info_py38_swap:
setup.test_python_version()
def test_python_version_testing_with_incorrect_version_and_linux_os(
self
) -> None:
print_arr: List[str] = []
def mock_print(msg_list: List[str]) -> None:
print_arr.extend(msg_list)
def mock_uname() -> List[str]:
return ['Linux']
print_swap = self.swap(
common, 'print_each_string_after_two_new_lines', mock_print)
uname_swap = self.swap(os, 'uname', mock_uname)
version_info = collections.namedtuple(
'version_info', ['major', 'minor', 'micro'])
version_swap = self.swap(
sys, 'version_info', version_info(major=3, minor=4, micro=12))
with print_swap, uname_swap, version_swap, self.assertRaisesRegex(
Exception, 'No suitable python version found.'
):
setup.test_python_version()
self.assertEqual(print_arr, [])
def test_python_version_testing_with_incorrect_version_and_windows_os(
self
) -> None:
print_arr: List[str] = []
def mock_print(msg_list: List[str]) -> None:
print_arr.extend(msg_list)
print_swap = self.swap(
common, 'print_each_string_after_two_new_lines', mock_print)
os_name_swap = self.swap(common, 'OS_NAME', 'Windows')
version_info = collections.namedtuple(
'version_info', ['major', 'minor', 'micro'])
version_swap = self.swap(
sys, 'version_info', version_info(major=3, minor=4, micro=12))
with print_swap, os_name_swap, version_swap:
with self.assertRaisesRegex(
Exception, 'No suitable python version found.'
):
setup.test_python_version()
self.assertEqual(
print_arr, [
'It looks like you are using Windows. If you have Python '
'installed,',
'make sure it is in your PATH and that PYTHONPATH is set.',
'If you have two versions of Python (ie, Python 2.7 and 3), '
'specify 2.7 before other versions of Python when setting the '
'PATH.',
'Here are some helpful articles:',
'http://docs.python-guide.org/en/latest/starting/install/win/',
'https://stackoverflow.com/questions/3701646/how-to-add-to-the-'
'pythonpath-in-windows-7'])
def test_python_version_testing_with_python2_wrong_code(self) -> None:
check_call_swap = self.swap_to_always_return(subprocess, 'call', 1)
with self.python2_print_swap, self.version_info_py38_swap:
with check_call_swap, self.assertRaisesRegex(SystemExit, '1'):
setup.test_python_version()
def test_download_and_install_package(self) -> None:
check_function_calls = {
'url_retrieve_is_called': False,
'open_is_called': False,
'extractall_is_called': False,
'close_is_called': False,
'remove_is_called': False
}
expected_check_function_calls = {
'url_retrieve_is_called': True,
'open_is_called': True,
'extractall_is_called': True,
'close_is_called': True,
'remove_is_called': True
}
def mock_url_retrieve(unused_url: str, filename: str) -> None: # pylint: disable=unused-argument
check_function_calls['url_retrieve_is_called'] = True
temp_file = tarfile.open(name=MOCK_TMP_UNTAR_PATH)
def mock_open(name: str) -> tarfile.TarFile: # pylint: disable=unused-argument
check_function_calls['open_is_called'] = True
return temp_file
def mock_extractall(unused_self: str, path: str) -> None: # pylint: disable=unused-argument
check_function_calls['extractall_is_called'] = True
def mock_close(unused_self: str) -> None:
check_function_calls['close_is_called'] = True
def mock_remove(unused_path: str) -> None:
check_function_calls['remove_is_called'] = True
url_retrieve_swap = self.swap(
common, 'url_retrieve', mock_url_retrieve)
open_swap = self.swap(tarfile, 'open', mock_open)
extract_swap = self.swap(tarfile.TarFile, 'extractall', mock_extractall)
close_swap = self.swap(tarfile.TarFile, 'close', mock_close)
remove_swap = self.swap(os, 'remove', mock_remove)
with url_retrieve_swap, open_swap, extract_swap, close_swap:
with remove_swap:
setup.download_and_install_package('url', 'filename')
self.assertEqual(check_function_calls, expected_check_function_calls)
def test_rename_yarn_folder(self) -> None:
# Creates a dummy yarn folder and then checks if `v` was removed
# upon function call.
os.mkdir(MOCK_YARN_PATH)
setup.rename_yarn_folder(
'yarn-v%s' % common.YARN_VERSION, TEST_DATA_DIR
)
target = os.path.join(
TEST_DATA_DIR, 'yarn-%s' % common.YARN_VERSION)
self.assertTrue(os.path.exists(target))
os.rmdir(target)
def test_invalid_dir(self) -> None:
def mock_getcwd() -> str:
return 'invalid'
print_arr: List[str] = []
def mock_print(msg: str) -> None:
print_arr.append(msg)
getcwd_swap = self.swap(os, 'getcwd', mock_getcwd)
print_swap = self.swap(builtins, 'print', mock_print)
with self.test_py_swap, getcwd_swap, print_swap:
with self.assertRaisesRegex(Exception, 'Invalid root directory.'):
setup.main(args=[])
self.assertFalse(
'WARNING This script should be run from the oppia/ '
'root folder.' in print_arr)
self.assertTrue(
self.check_function_calls['test_python_version_is_called'])
def test_package_install_with_darwin_x64(self) -> None:
os_name_swap = self.swap(common, 'OS_NAME', 'Darwin')
with self.test_py_swap, self.create_swap, os_name_swap:
with self.download_swap, self.rename_swap, self.exists_false_swap:
with self.chmod_swap, self.delete_swap, self.isfile_swap:
with self.is_x64_architecture_true_swap, self.chown_swap:
setup.main(args=[])
for item in self.check_function_calls.values():
self.assertTrue(item)
self.assertEqual(
self.urls, [
'https://nodejs.org/dist/v%s/node-v%s-darwin-x64.tar.gz' % (
common.NODE_VERSION, common.NODE_VERSION),
'https://github.com/yarnpkg/yarn/releases/download/'
'v%s/yarn-v%s.tar.gz' % (
common.YARN_VERSION, common.YARN_VERSION)])
def test_package_install_with_darwin_x86(self) -> None:
os_name_swap = self.swap(common, 'OS_NAME', 'Darwin')
all_cmd_tokens: List[str] = []
def mock_check_call(cmd_tokens: List[str]) -> None:
all_cmd_tokens.extend(cmd_tokens)
check_call_swap = self.swap(subprocess, 'check_call', mock_check_call)
with self.test_py_swap, self.create_swap, os_name_swap, self.chown_swap:
with self.download_swap, self.rename_swap, self.exists_false_swap:
with self.chmod_swap, self.delete_swap, self.isfile_swap:
with self.is_x64_architecture_false_swap, self.cd_swap:
with check_call_swap:
setup.main(args=[])
for _, item in self.check_function_calls.items():
self.assertTrue(item)
self.assertEqual(
self.urls, [
'https://nodejs.org/dist/v%s/node-v%s.tar.gz' % (
common.NODE_VERSION, common.NODE_VERSION),
'https://github.com/yarnpkg/yarn/releases/download/'
'v%s/yarn-v%s.tar.gz' % (
common.YARN_VERSION, common.YARN_VERSION)])
self.assertEqual(all_cmd_tokens, ['./configure', 'make'])
def test_package_install_with_linux_x64(self) -> None:
os_name_swap = self.swap(common, 'OS_NAME', 'Linux')
with self.test_py_swap, self.create_swap, os_name_swap, self.chown_swap:
with self.download_swap, self.rename_swap, self.exists_false_swap:
with self.chmod_swap, self.delete_swap, self.isfile_swap:
with self.is_x64_architecture_true_swap:
setup.main(args=[])
for item in self.check_function_calls.values():
self.assertTrue(item)
self.assertEqual(
self.urls, [
'https://nodejs.org/dist/v%s/node-v%s-linux-x64.tar.gz' % (
common.NODE_VERSION, common.NODE_VERSION),
'https://github.com/yarnpkg/yarn/releases/download/'
'v%s/yarn-v%s.tar.gz' % (
common.YARN_VERSION, common.YARN_VERSION)])
def test_package_install_with_linux_x86(self) -> None:
os_name_swap = self.swap(common, 'OS_NAME', 'Linux')
all_cmd_tokens: List[str] = []
def mock_check_call(cmd_tokens: List[str]) -> None:
all_cmd_tokens.extend(cmd_tokens)
check_call_swap = self.swap(subprocess, 'check_call', mock_check_call)
with self.test_py_swap, self.create_swap, os_name_swap, check_call_swap:
with self.download_swap, self.rename_swap, self.cd_swap:
with self.chmod_swap, self.delete_swap, self.isfile_swap:
with self.is_x64_architecture_false_swap, self.chown_swap:
with self.exists_false_swap:
setup.main(args=[])
for item in self.check_function_calls.values():
self.assertTrue(item)
self.assertEqual(
self.urls, [
'https://nodejs.org/dist/v%s/node-v%s.tar.gz' % (
common.NODE_VERSION, common.NODE_VERSION),
'https://github.com/yarnpkg/yarn/releases/download/'
'v%s/yarn-v%s.tar.gz' % (
common.YARN_VERSION, common.YARN_VERSION)])
self.assertEqual(all_cmd_tokens, ['./configure', 'make'])
def test_package_install_with_windows_x86(self) -> None:
def mock_url_retrieve(url: str, filename: str) -> None: # pylint: disable=unused-argument
self.urls.append(url)
check_call_commands: List[str] = []
def mock_check_call(commands: List[str]) -> None:
nonlocal check_call_commands
check_call_commands = commands
os_name_swap = self.swap(common, 'OS_NAME', 'Windows')
url_retrieve_swap = self.swap(
common, 'url_retrieve', mock_url_retrieve)
check_call_swap = self.swap(subprocess, 'check_call', mock_check_call)
with self.test_py_swap, self.create_swap, os_name_swap, check_call_swap:
with self.download_swap, self.rename_swap, self.delete_swap:
with self.isfile_swap, self.is_x64_architecture_false_swap:
with url_retrieve_swap, self.exists_false_swap:
setup.main(args=[])
check_function_calls = self.check_function_calls.copy()
del check_function_calls['recursive_chown_is_called']
del check_function_calls['recursive_chmod_is_called']
for item in check_function_calls.values():
self.assertTrue(item)
self.assertEqual(
check_call_commands,
['powershell.exe', '-c', 'expand-archive',
'node-download', '-DestinationPath',
common.OPPIA_TOOLS_DIR])
self.assertEqual(
self.urls, [
'https://nodejs.org/dist/v%s/node-v%s-win-x86.zip' % (
common.NODE_VERSION, common.NODE_VERSION),
'https://github.com/yarnpkg/yarn/releases/download/'
'v%s/yarn-v%s.tar.gz' % (
common.YARN_VERSION, common.YARN_VERSION)])
def test_package_install_with_windows_x64(self) -> None:
def mock_url_retrieve(url: str, filename: str) -> None: # pylint: disable=unused-argument
self.urls.append(url)
check_call_commands: List[str] = []
def mock_check_call(commands: List[str]) -> None:
nonlocal check_call_commands
check_call_commands = commands
os_name_swap = self.swap(common, 'OS_NAME', 'Windows')
url_retrieve_swap = self.swap(
common, 'url_retrieve', mock_url_retrieve)
check_call_swap = self.swap(subprocess, 'check_call', mock_check_call)
with self.test_py_swap, self.create_swap, os_name_swap, check_call_swap:
with self.download_swap, self.rename_swap, self.delete_swap:
with self.isfile_swap, self.is_x64_architecture_true_swap:
with url_retrieve_swap, self.exists_false_swap:
setup.main(args=[])
check_function_calls = self.check_function_calls.copy()
del check_function_calls['recursive_chown_is_called']
del check_function_calls['recursive_chmod_is_called']
for item in check_function_calls.values():
self.assertTrue(item)
self.assertEqual(
check_call_commands,
['powershell.exe', '-c', 'expand-archive',
'node-download', '-DestinationPath',
common.OPPIA_TOOLS_DIR])
self.assertEqual(
self.urls, [
'https://nodejs.org/dist/v%s/node-v%s-win-x64.zip' % (
common.NODE_VERSION, common.NODE_VERSION),
'https://github.com/yarnpkg/yarn/releases/download/'
'v%s/yarn-v%s.tar.gz' % (
common.YARN_VERSION, common.YARN_VERSION)])
def test_package_install_with_incompatible_system_raises_error(
self
) -> None:
os_name_swap = self.swap(common, 'OS_NAME', 'Solaris')
with self.test_py_swap, self.create_swap, os_name_swap, self.chown_swap:
with self.rename_swap, self.exists_false_swap:
with self.assertRaisesRegex(
Exception, 'System\'s Operating System is not compatible.'
), self.is_x64_architecture_true_swap:
setup.main(args=[])
def test_if_node_is_already_installed_then_skip_installation(self) -> None:
print_list = []
def mock_print(arg: str) -> None:
print_list.append(arg)
os_name_swap = self.swap(common, 'OS_NAME', 'Linux')
print_swap = self.swap(builtins, 'print', mock_print)
with self.test_py_swap, self.create_swap, self.chown_swap, print_swap:
with self.rename_swap, self.exists_true_swap, os_name_swap:
setup.main(args=[])
print(print_list)
self.assertIn('Environment setup completed.', print_list)
self.assertNotIn('Installing Node.js', print_list)
self.assertNotIn('Removing package-lock.json', print_list)