-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_programs.py
302 lines (245 loc) · 9.92 KB
/
test_programs.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
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
"""Tests for programs.py"""
# Standard library imports
import os
import os.path as osp
import sys
# Third party impors
from flaky import flaky
import pytest
# Local imports
from spyder.config.base import running_in_ci
from spyder.utils.programs import (_clean_win_application_path, check_version,
find_program, get_application_icon,
get_installed_applications, get_temp_dir,
is_module_installed, is_python_interpreter,
is_python_interpreter_valid_name,
open_files_with_application,
parse_linux_desktop_entry,
run_python_script_in_terminal, shell_split,
get_package_version, get_module_version)
if os.name == 'nt':
python_dir = 'C:\\Miniconda\\'
VALID_INTERPRETER = os.path.join(python_dir, 'python.exe')
VALID_W_INTERPRETER = os.path.join(python_dir, 'pythonw.exe')
INVALID_INTERPRETER = os.path.join(python_dir, 'Scripts', 'ipython.exe')
else:
if sys.platform.startswith('linux'):
home_dir = '/usr/share/miniconda/'
else:
home_dir = '/usr/local/miniconda/'
VALID_INTERPRETER = os.path.join(home_dir, 'bin', 'python')
VALID_W_INTERPRETER = os.path.join(home_dir, 'bin', 'pythonw')
INVALID_INTERPRETER = os.path.join(home_dir, 'bin', 'ipython')
# =============================================================================
# ---- Fixtures
# =============================================================================
@pytest.fixture
def scriptpath(tmpdir):
"""Save a basic Python script in a file."""
script = ("with open('out.txt', 'w') as f:\n"
" f.write('done')\n")
scriptpath = tmpdir.join('write-done.py')
scriptpath.write(script)
return scriptpath
@pytest.fixture
def scriptpath_with_blanks(tmpdir):
"""Save a basic Python script in a file."""
name_dir = 'test dir'
if not osp.exists(name_dir):
os.mkdir(name_dir)
os.chdir(name_dir)
tmpdir.join(name_dir)
script = ("with open('out.txt', 'w') as f:\n"
" f.write('done')\n")
scriptpath = tmpdir.join('write-done.py')
scriptpath.write(script)
return scriptpath
# =============================================================================
# ---- Tests
# =============================================================================
@pytest.mark.skipif(sys.platform.startswith('linux') or not running_in_ci(),
reason='It only runs in CI services and '
'Linux does not have pythonw executables.')
def test_is_valid_w_interpreter():
assert is_python_interpreter(VALID_W_INTERPRETER)
@flaky(max_runs=3)
@pytest.mark.skipif(not running_in_ci(), reason='Only on CI!')
def test_run_python_script_in_terminal(scriptpath, qtbot):
"""
Test running a Python script in an external terminal when specifying
explicitly the working directory.
"""
# Run the script
outfilepath = osp.join(scriptpath.dirname, 'out.txt')
run_python_script_in_terminal(
scriptpath.strpath, scriptpath.dirname, '', False, False, '')
qtbot.waitUntil(lambda: osp.exists(outfilepath), timeout=10000)
# Assert the result.
with open(outfilepath, 'r') as txtfile:
res = txtfile.read()
assert res == 'done'
@flaky(max_runs=3)
@pytest.mark.order(1)
@pytest.mark.skipif(
not running_in_ci() or os.name == 'nt',
reason='Only on CI and not on windows!',
)
def test_run_python_script_in_terminal_blank_wdir(scriptpath_with_blanks,
qtbot):
"""
Test running a Python script in an external terminal when specifying
explicitly the working directory.
"""
# Run the script
outfilepath = osp.join(scriptpath_with_blanks.dirname, 'out.txt')
run_python_script_in_terminal(
scriptpath_with_blanks.strpath, scriptpath_with_blanks.dirname,
'', False, False, '')
qtbot.waitUntil(lambda: osp.exists(outfilepath), timeout=10000)
# Assert the result.
with open(outfilepath, 'r') as txtfile:
res = txtfile.read()
assert res == 'done'
@flaky(max_runs=3)
@pytest.mark.order(1)
@pytest.mark.skipif(
not running_in_ci() or os.name == 'nt',
reason='Only on CI and not on windows!',
)
def test_run_python_script_in_terminal_with_wdir_empty(scriptpath, qtbot):
"""
Test running a Python script in an external terminal without specifying
the working directory.
"""
# Run the script.
if sys.platform == 'darwin':
outfilepath = osp.join(osp.expanduser('~'), 'out.txt')
else:
outfilepath = osp.join(os.getcwd(), 'out.txt')
run_python_script_in_terminal(scriptpath.strpath, '', '', False, False, '')
qtbot.waitUntil(lambda: osp.exists(outfilepath), timeout=10000)
# Assert the result.
with open(outfilepath, 'r') as txtfile:
res = txtfile.read()
assert res == 'done'
@pytest.mark.order(1)
@pytest.mark.skipif(not running_in_ci(), reason='Only on CI!')
def test_is_valid_interpreter():
assert is_python_interpreter(VALID_INTERPRETER)
@pytest.mark.order(1)
@pytest.mark.skipif(not running_in_ci(), reason='Only on CI!')
def test_is_invalid_interpreter():
assert not is_python_interpreter(INVALID_INTERPRETER)
@pytest.mark.order(1)
@pytest.mark.skipif(not running_in_ci(), reason='Only on CI!')
def test_is_valid_interpreter_name():
names = ['python', 'pythonw', 'python2.7', 'python3.5', 'python.exe', 'pythonw.exe']
assert all([is_python_interpreter_valid_name(n) for n in names])
def test_find_program():
"""Test if can find the program."""
assert find_program('git')
def test_shell_split():
"""Test if the text can be split using shell-like syntax."""
assert shell_split('-q -o -a') == ['-q', '-o', '-a']
assert shell_split('-q "d:\\Python de xxxx\\t.txt" -o -a') == \
['-q', 'd:\\Python de xxxx\\t.txt', '-o', '-a']
def test_check_version():
"""Test the compare function for versions."""
assert check_version('0.9.4-1', '0.9.4', '>=')
assert check_version('3.0.0rc1', '3.0.0', '<')
assert check_version('1.0', '1.0b2', '>')
def test_is_module_installed():
"""Test if a module with the proper version is installed"""
assert is_module_installed('qtconsole', '>=4.5')
assert not is_module_installed('IPython', '>=1.0,<3.0')
assert is_module_installed('jedi', '>=0.7.0')
assert not is_module_installed('foo')
assert not is_module_installed('foo1', '>=1.2.0')
assert is_module_installed('IPython', '!=3.0')
def test_is_module_installed_with_custom_interpreter():
"""Test if a module with the proper version is installed"""
current = sys.executable
assert is_module_installed('qtconsole', '>=4.5', interpreter=current)
assert not is_module_installed('IPython', '>=1.0,<3.0', interpreter=current)
assert is_module_installed('cloudpickle', '>=0.5.0', interpreter=current)
def test_get_temp_dir_ensure_dir_exists():
"""Test that the call to get_temp_dir creates the dir when it doesn't exists
"""
temp_dir = get_temp_dir(suffix='test')
assert os.path.exists(temp_dir)
os.rmdir(temp_dir)
another_call = get_temp_dir(suffix='test')
assert os.path.exists(another_call)
assert another_call == temp_dir
def test_clean_win_application_path(qtbot):
data = (
('"c:\\some thing\\foo.exe"', 'c:/some thing/foo.exe'),
("c:\\some' thing\\foo.exe", "c:/some' thing/foo.exe"),
)
for path, expected_output in data:
assert _clean_win_application_path(path) == expected_output
def test_get_installed_apps_and_icons(qtbot):
apps = get_installed_applications()
assert apps
for app in apps:
fpath = apps[app]
icon = get_application_icon(fpath)
assert icon
assert osp.isdir(fpath) or osp.isfile(fpath)
@pytest.mark.skipif(not sys.platform.startswith('linux'),
reason="Test for linux only")
def test_parse_linux_desktop_entry():
apps = get_installed_applications()
for app in apps:
fpath = apps[app]
data = parse_linux_desktop_entry(fpath)
assert data
for key in ['name', 'icon_path', 'hidden', 'exec', 'type', 'fpath']:
assert key in data
assert fpath == data['fpath']
def test_open_files_with_application(tmp_path):
fpath = tmp_path / 'file space.txt'
fpath.write_text(u'Hello')
fpath_2 = tmp_path / 'file2.txt'
fpath_2.write_text(u'Hello 2')
if os.name == 'nt':
ext = '.exe'
path_obj = tmp_path / ("some-new app" + ext)
path_obj.write_bytes(b'\x00\x00')
app_path = str(path_obj)
elif sys.platform == 'darwin':
ext = '.app'
path_obj = tmp_path / ("some-new app" + ext)
path_obj.mkdir()
app_path = str(path_obj)
else:
ext = '.desktop'
path_obj = tmp_path / ("some-new app" + ext)
path_obj.write_text(u'''
[Desktop Entry]
Name=Suer app
Type=Application
Exec=/something/bleerp
Icon=/blah/blah.xpm
''')
app_path = str(path_obj)
fnames = [str(fpath), str(fpath_2)]
return_codes = open_files_with_application(app_path, fnames)
assert 0 not in return_codes.values()
# Test raises
with pytest.raises(ValueError):
return_codes = open_files_with_application('not-valid.ext', fnames)
def test_get_package_version():
# Primarily a test of pkg_resources/setuptools being installed properly
assert get_package_version('IPython')
assert get_package_version('python_lsp_black')
def test_get_module_version():
# intervaltree should not have a __version__ attribute, so test that the
# fallback mechanism to get_package_version is working
assert get_module_version('intervaltree')
if __name__ == '__main__':
pytest.main()