summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Nasrat <pnasrat@gmail.com>2011-04-21 11:10:11 +0100
committerPaul Nasrat <pnasrat@gmail.com>2011-04-21 15:31:17 +0100
commit5d57c4cb78fb9ba17027deadbafdda01efea12e1 (patch)
treec93b649f9bde07ec165e018bb26695824563f79e
parente48487561417f81ba232a3016572e7aaecc67248 (diff)
downloadvirtualenv-5d57c4cb78fb9ba17027deadbafdda01efea12e1.tar.gz
Prevent traceback with non-exec interpreter
Fixes #121 Add tests for virtualenv with nose and mock Add helper method to virtualenv for is_executable Raise SystemExit rather than sys.exit directly Tested on 2.4.4, 2.7.1 and 3.2
-rw-r--r--setup.py2
-rw-r--r--tests/test_virtualenv.py75
-rwxr-xr-xvirtualenv.py9
3 files changed, 85 insertions, 1 deletions
diff --git a/setup.py b/setup.py
index dad8605..3815afb 100644
--- a/setup.py
+++ b/setup.py
@@ -52,5 +52,7 @@ setup(name='virtualenv',
py_modules=['virtualenv'],
packages=['virtualenv_support'],
package_data={'virtualenv_support': ['*-py%s.egg' % sys.version[:3], '*.tar.gz']},
+ test_suite='nose.collector',
+ tests_require=['nose', 'Mock'],
**kw
)
diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py
new file mode 100644
index 0000000..9faad8a
--- /dev/null
+++ b/tests/test_virtualenv.py
@@ -0,0 +1,75 @@
+import virtualenv
+from mock import patch, Mock
+import os.path
+import sys
+
+def test_version():
+ """Should have a version string"""
+ assert virtualenv.virtualenv_version == "1.6", "Should have version"
+
+@patch('os.path.exists')
+@patch('os.path.abspath')
+def test_resolve_interpreter_with_absolute_path(mock_abspath, mock_exists):
+ """Should return absolute path if given and exists"""
+ mock_abspath.return_value = True
+ mock_exists.return_value = True
+ virtualenv.is_executable = Mock(return_value=True)
+
+ mock_abspath.start()
+ mock_exists.start()
+
+ exe = virtualenv.resolve_interpreter("/usr/bin/python42")
+
+ assert exe == "/usr/bin/python42", "Absolute path should return as is"
+ mock_abspath.assert_called_with("/usr/bin/python42")
+ mock_exists.assert_called_with("/usr/bin/python42")
+ virtualenv.is_executable.assert_called_with("/usr/bin/python42")
+
+ mock_abspath.stop()
+ mock_exists.stop()
+
+@patch('os.path.exists')
+@patch('os.path.abspath')
+def test_resolve_intepreter_with_nonexistant_interpreter(mock_abspath, mock_exists):
+ """Should exit when with absolute path if not exists"""
+ mock_abspath.return_value = True
+ mock_exists.return_value = False
+
+ mock_abspath.start()
+ mock_exists.start()
+
+ try:
+ exe = virtualenv.resolve_interpreter("/usr/bin/python42")
+ assert False, "Should raise exception"
+ except SystemExit:
+ pass
+
+ mock_abspath.assert_called_with("/usr/bin/python42")
+ mock_exists.assert_called_with("/usr/bin/python42")
+
+ mock_abspath.stop()
+ mock_exists.stop()
+
+@patch('os.path.exists')
+@patch('os.path.abspath')
+def test_resolve_intepreter_with_invalid_interpreter(mock_abspath, mock_exists):
+ """Should exit when with absolute path if not exists"""
+ mock_abspath.return_value = True
+ mock_exists.return_value = True
+ virtualenv.is_executable = Mock(return_value=False)
+
+ mock_abspath.start()
+ mock_exists.start()
+
+ try:
+ exe = virtualenv.resolve_interpreter("/usr/bin/python42")
+ assert False, "Should raise exception"
+ except SystemExit:
+ pass
+
+ mock_abspath.assert_called_with("/usr/bin/python42")
+ mock_exists.assert_called_with("/usr/bin/python42")
+ virtualenv.is_executable.assert_called_with("/usr/bin/python42")
+
+ mock_abspath.stop()
+ mock_exists.stop()
diff --git a/virtualenv.py b/virtualenv.py
index f49f3a3..5bd5534 100755
--- a/virtualenv.py
+++ b/virtualenv.py
@@ -1228,9 +1228,16 @@ def resolve_interpreter(exe):
break
if not os.path.exists(exe):
logger.fatal('The executable %s (from --python=%s) does not exist' % (exe, exe))
- sys.exit(3)
+ raise SystemExit(3)
+ if not is_executable(exe):
+ logger.fatal('The executable %s (from --python=%s) is not executable' % (exe, exe))
+ raise SystemExit(3)
return exe
+def is_executable(exe):
+ """Checks a file is executable"""
+ return os.access(exe, os.X_OK)
+
############################################################
## Relocating the environment: