Skip to content

Commit b338abc

Browse files
jurko-gospodneticgaborbernat
authored andcommitted
test get_installed_pythons() on and off Windows platform
1 parent a2d3b89 commit b338abc

File tree

1 file changed

+143
-1
lines changed

1 file changed

+143
-1
lines changed

tests/test_virtualenv.py

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,156 @@
77
import pytest
88
import platform # noqa
99

10-
from mock import patch, Mock
10+
from mock import call, Mock, NonCallableMock, patch
1111

1212

1313
def test_version():
1414
"""Should have a version string"""
1515
assert virtualenv.virtualenv_version, "Should have version"
1616

1717

18+
class TestGetInstalledPythons:
19+
key_local_machine = 'key-local-machine'
20+
key_current_user = 'key-current-user'
21+
22+
@classmethod
23+
def mock_virtualenv_winreg(cls, monkeypatch, data):
24+
def enum_key(key, index):
25+
try:
26+
return data.get(key, [])[index]
27+
except IndexError:
28+
raise WindowsError
29+
30+
def query_value(key, path):
31+
installed_version_tags = data.get(key, [])
32+
suffix = '\\InstallPath'
33+
if path.endswith(suffix):
34+
version_tag = path[:-len(suffix)]
35+
if version_tag in installed_version_tags:
36+
return '{}-{}-path'.format(key, version_tag)
37+
raise WindowsError
38+
39+
mock_winreg = NonCallableMock(spec_set=[
40+
'HKEY_LOCAL_MACHINE',
41+
'HKEY_CURRENT_USER',
42+
'CreateKey',
43+
'EnumKey',
44+
'QueryValue',
45+
'CloseKey'])
46+
mock_winreg.HKEY_LOCAL_MACHINE = 'HKEY_LOCAL_MACHINE'
47+
mock_winreg.HKEY_CURRENT_USER = 'HKEY_CURRENT_USER'
48+
mock_winreg.CreateKey.side_effect = [cls.key_local_machine,
49+
cls.key_current_user]
50+
mock_winreg.EnumKey.side_effect = enum_key
51+
mock_winreg.QueryValue.side_effect = query_value
52+
mock_winreg.CloseKey.return_value = None
53+
monkeypatch.setattr(virtualenv, 'winreg', mock_winreg)
54+
return mock_winreg
55+
56+
@pytest.mark.skipif(sys.platform == 'win32',
57+
reason='non-windows specific test')
58+
def test_on_non_windows(self, monkeypatch):
59+
assert not virtualenv.is_win
60+
assert not hasattr(virtualenv, 'winreg')
61+
assert virtualenv.get_installed_pythons() == {}
62+
63+
@pytest.mark.skipif(sys.platform != 'win32',
64+
reason='non-windows specific test')
65+
def test_on_windows(self, monkeypatch):
66+
assert virtualenv.is_win
67+
mock_winreg = self.mock_virtualenv_winreg(monkeypatch, {
68+
self.key_local_machine: (
69+
'2.4',
70+
'2.7',
71+
'3.2',
72+
'3.4',
73+
'3.5', # 64-bit only
74+
'3.6-32', # 32-bit only
75+
'3.7', '3.7-32', # both 32 & 64-bit with a 64-bit user install
76+
'3.8'), # 64-bit with a 32-bit user install
77+
self.key_current_user: (
78+
'2.5',
79+
'2.7',
80+
'3.7',
81+
'3.8-32')})
82+
monkeypatch.setattr(virtualenv, 'join', '{}\\{}'.format)
83+
84+
installed_pythons = virtualenv.get_installed_pythons()
85+
86+
assert installed_pythons == {
87+
'2': self.key_current_user + '-2.7-path\\python.exe',
88+
'2.4': self.key_local_machine + '-2.4-path\\python.exe',
89+
'2.5': self.key_current_user + '-2.5-path\\python.exe',
90+
'2.7': self.key_current_user + '-2.7-path\\python.exe',
91+
'3': self.key_local_machine + '-3.8-path\\python.exe',
92+
'3.2': self.key_local_machine + '-3.2-path\\python.exe',
93+
'3.4': self.key_local_machine + '-3.4-path\\python.exe',
94+
'3.5': self.key_local_machine + '-3.5-path\\python.exe',
95+
'3.5-64': self.key_local_machine + '-3.5-path\\python.exe',
96+
'3.6': self.key_local_machine + '-3.6-32-path\\python.exe',
97+
'3.6-32': self.key_local_machine + '-3.6-32-path\\python.exe',
98+
'3.7': self.key_current_user + '-3.7-path\\python.exe',
99+
'3.7-32': self.key_local_machine + '-3.7-32-path\\python.exe',
100+
'3.7-64': self.key_current_user + '-3.7-path\\python.exe',
101+
'3.8': self.key_local_machine + '-3.8-path\\python.exe',
102+
'3.8-32': self.key_current_user + '-3.8-32-path\\python.exe',
103+
'3.8-64': self.key_local_machine + '-3.8-path\\python.exe'}
104+
assert mock_winreg.mock_calls == [
105+
call.CreateKey(mock_winreg.HKEY_LOCAL_MACHINE,
106+
'Software\\Python\\PythonCore'),
107+
call.EnumKey(self.key_local_machine, 0),
108+
call.QueryValue(self.key_local_machine, '2.4\\InstallPath'),
109+
call.EnumKey(self.key_local_machine, 1),
110+
call.QueryValue(self.key_local_machine, '2.7\\InstallPath'),
111+
call.EnumKey(self.key_local_machine, 2),
112+
call.QueryValue(self.key_local_machine, '3.2\\InstallPath'),
113+
call.EnumKey(self.key_local_machine, 3),
114+
call.QueryValue(self.key_local_machine, '3.4\\InstallPath'),
115+
call.EnumKey(self.key_local_machine, 4),
116+
call.QueryValue(self.key_local_machine, '3.5\\InstallPath'),
117+
call.EnumKey(self.key_local_machine, 5),
118+
call.QueryValue(self.key_local_machine, '3.6-32\\InstallPath'),
119+
call.EnumKey(self.key_local_machine, 6),
120+
call.QueryValue(self.key_local_machine, '3.7\\InstallPath'),
121+
call.EnumKey(self.key_local_machine, 7),
122+
call.QueryValue(self.key_local_machine, '3.7-32\\InstallPath'),
123+
call.EnumKey(self.key_local_machine, 8),
124+
call.QueryValue(self.key_local_machine, '3.8\\InstallPath'),
125+
call.EnumKey(self.key_local_machine, 9),
126+
call.CloseKey(self.key_local_machine),
127+
call.CreateKey(mock_winreg.HKEY_CURRENT_USER,
128+
'Software\\Python\\PythonCore'),
129+
call.EnumKey(self.key_current_user, 0),
130+
call.QueryValue(self.key_current_user, '2.5\\InstallPath'),
131+
call.EnumKey(self.key_current_user, 1),
132+
call.QueryValue(self.key_current_user, '2.7\\InstallPath'),
133+
call.EnumKey(self.key_current_user, 2),
134+
call.QueryValue(self.key_current_user, '3.7\\InstallPath'),
135+
call.EnumKey(self.key_current_user, 3),
136+
call.QueryValue(self.key_current_user, '3.8-32\\InstallPath'),
137+
call.EnumKey(self.key_current_user, 4),
138+
call.CloseKey(self.key_current_user)]
139+
140+
@pytest.mark.skipif(sys.platform != 'win32',
141+
reason='windows specific test')
142+
def test_on_windows_with_no_installations(self, monkeypatch):
143+
assert virtualenv.is_win
144+
mock_winreg = self.mock_virtualenv_winreg(monkeypatch, {})
145+
146+
installed_pythons = virtualenv.get_installed_pythons()
147+
148+
assert installed_pythons == {}
149+
assert mock_winreg.mock_calls == [
150+
call.CreateKey(mock_winreg.HKEY_LOCAL_MACHINE,
151+
'Software\\Python\\PythonCore'),
152+
call.EnumKey(self.key_local_machine, 0),
153+
call.CloseKey(self.key_local_machine),
154+
call.CreateKey(mock_winreg.HKEY_CURRENT_USER,
155+
'Software\\Python\\PythonCore'),
156+
call.EnumKey(self.key_current_user, 0),
157+
call.CloseKey(self.key_current_user)]
158+
159+
18160
@patch('distutils.spawn.find_executable')
19161
@patch('virtualenv.is_executable', return_value=True)
20162
@patch('virtualenv.get_installed_pythons')

0 commit comments

Comments
 (0)