OLD | NEW |
| (Empty) |
1 import virtualenv | |
2 import optparse | |
3 import os | |
4 import shutil | |
5 import sys | |
6 import tempfile | |
7 from mock import patch, Mock | |
8 | |
9 | |
10 def test_version(): | |
11 """Should have a version string""" | |
12 assert virtualenv.virtualenv_version, "Should have version" | |
13 | |
14 | |
15 @patch('os.path.exists') | |
16 def test_resolve_interpreter_with_absolute_path(mock_exists): | |
17 """Should return absolute path if given and exists""" | |
18 mock_exists.return_value = True | |
19 virtualenv.is_executable = Mock(return_value=True) | |
20 | |
21 exe = virtualenv.resolve_interpreter("/usr/bin/python42") | |
22 | |
23 assert exe == "/usr/bin/python42", "Absolute path should return as is" | |
24 mock_exists.assert_called_with("/usr/bin/python42") | |
25 virtualenv.is_executable.assert_called_with("/usr/bin/python42") | |
26 | |
27 | |
28 @patch('os.path.exists') | |
29 def test_resolve_interpreter_with_nonexistent_interpreter(mock_exists): | |
30 """Should exit when with absolute path if not exists""" | |
31 mock_exists.return_value = False | |
32 | |
33 try: | |
34 virtualenv.resolve_interpreter("/usr/bin/python42") | |
35 assert False, "Should raise exception" | |
36 except SystemExit: | |
37 pass | |
38 | |
39 mock_exists.assert_called_with("/usr/bin/python42") | |
40 | |
41 | |
42 @patch('os.path.exists') | |
43 def test_resolve_interpreter_with_invalid_interpreter(mock_exists): | |
44 """Should exit when with absolute path if not exists""" | |
45 mock_exists.return_value = True | |
46 virtualenv.is_executable = Mock(return_value=False) | |
47 | |
48 try: | |
49 virtualenv.resolve_interpreter("/usr/bin/python42") | |
50 assert False, "Should raise exception" | |
51 except SystemExit: | |
52 pass | |
53 | |
54 mock_exists.assert_called_with("/usr/bin/python42") | |
55 virtualenv.is_executable.assert_called_with("/usr/bin/python42") | |
56 | |
57 | |
58 def test_activate_after_future_statements(): | |
59 """Should insert activation line after last future statement""" | |
60 script = [ | |
61 '#!/usr/bin/env python', | |
62 'from __future__ import with_statement', | |
63 'from __future__ import print_function', | |
64 'print("Hello, world!")' | |
65 ] | |
66 assert virtualenv.relative_script(script) == [ | |
67 '#!/usr/bin/env python', | |
68 'from __future__ import with_statement', | |
69 'from __future__ import print_function', | |
70 '', | |
71 "import os; activate_this=os.path.join(os.path.dirname(os.path.realpath(
__file__)), 'activate_this.py'); exec(compile(open(activate_this).read(), activa
te_this, 'exec'), dict(__file__=activate_this)); del os, activate_this", | |
72 '', | |
73 'print("Hello, world!")' | |
74 ] | |
75 | |
76 | |
77 def test_cop_update_defaults_with_store_false(): | |
78 """store_false options need reverted logic""" | |
79 class MyConfigOptionParser(virtualenv.ConfigOptionParser): | |
80 def __init__(self, *args, **kwargs): | |
81 self.config = virtualenv.ConfigParser.RawConfigParser() | |
82 self.files = [] | |
83 optparse.OptionParser.__init__(self, *args, **kwargs) | |
84 | |
85 def get_environ_vars(self, prefix='VIRTUALENV_'): | |
86 yield ("no_site_packages", "1") | |
87 | |
88 cop = MyConfigOptionParser() | |
89 cop.add_option( | |
90 '--no-site-packages', | |
91 dest='system_site_packages', | |
92 action='store_false', | |
93 help="Don't give access to the global site-packages dir to the " | |
94 "virtual environment (default)") | |
95 | |
96 defaults = {} | |
97 cop.update_defaults(defaults) | |
98 assert defaults == {'system_site_packages': 0} | |
99 | |
100 def test_install_python_bin(): | |
101 """Should create the right python executables and links""" | |
102 tmp_virtualenv = tempfile.mkdtemp() | |
103 try: | |
104 home_dir, lib_dir, inc_dir, bin_dir = \ | |
105 virtualenv.path_locations(tmp_virtualenv) | |
106 virtualenv.install_python(home_dir, lib_dir, inc_dir, bin_dir, False, | |
107 False) | |
108 | |
109 if virtualenv.is_win: | |
110 required_executables = [ 'python.exe', 'pythonw.exe'] | |
111 else: | |
112 py_exe_no_version = 'python' | |
113 py_exe_version_major = 'python%s' % sys.version_info[0] | |
114 py_exe_version_major_minor = 'python%s.%s' % ( | |
115 sys.version_info[0], sys.version_info[1]) | |
116 required_executables = [ py_exe_no_version, py_exe_version_major, | |
117 py_exe_version_major_minor ] | |
118 | |
119 for pth in required_executables: | |
120 assert os.path.exists(os.path.join(bin_dir, pth)), ("%s should " | |
121 "exist in bin_dir" % pth) | |
122 finally: | |
123 shutil.rmtree(tmp_virtualenv) | |
124 | |
125 | |
126 def test_always_copy_option(): | |
127 """Should be no symlinks in directory tree""" | |
128 tmp_virtualenv = tempfile.mkdtemp() | |
129 ve_path = os.path.join(tmp_virtualenv, 'venv') | |
130 try: | |
131 virtualenv.create_environment(ve_path, symlink=False) | |
132 | |
133 for root, dirs, files in os.walk(tmp_virtualenv): | |
134 for f in files + dirs: | |
135 full_name = os.path.join(root, f) | |
136 assert not os.path.islink(full_name), "%s should not be a" \ | |
137 " symlink (to %s)" % (full_name, os.readlink(full_name)) | |
138 finally: | |
139 shutil.rmtree(tmp_virtualenv) | |
OLD | NEW |