Skip to content

Commit b617e85

Browse files
jurko-gospodneticgaborbernat
authored andcommitted
touch up & improve resolve_interpreter() tests
- tests no longer permanently modify the `virtualenv` module under test (`virtualenv.is_executable` changes were leaking from some tests) - tests now check that `get_installed_python()` results unrelated to the given version tag do not affect the result - simple patch function's return values now given in `@patch` decorators to make the test code more compact
1 parent 5fd620c commit b617e85

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

tests/test_virtualenv.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,45 @@ def test_version():
1515
assert virtualenv.virtualenv_version, "Should have version"
1616

1717

18-
@patch('os.path.exists')
19-
def test_resolve_interpreter_with_absolute_path(mock_exists):
18+
@patch('virtualenv.is_executable', return_value=True)
19+
@patch('virtualenv.get_installed_pythons', return_value={'foo': 'bar'})
20+
@patch('os.path.exists', return_value=True)
21+
def test_resolve_interpreter_with_absolute_path(
22+
mock_exists, mock_get_installed_pythons, mock_is_executable):
2023
"""Should return absolute path if given and exists"""
21-
mock_exists.return_value = True
22-
virtualenv.is_executable = Mock(return_value=True)
2324
test_abs_path = os.path.abspath("/usr/bin/python53")
2425

2526
exe = virtualenv.resolve_interpreter(test_abs_path)
2627

2728
assert exe == test_abs_path, "Absolute path should return as is"
2829

2930
mock_exists.assert_called_with(test_abs_path)
30-
virtualenv.is_executable.assert_called_with(test_abs_path)
31+
mock_is_executable.assert_called_with(test_abs_path)
3132

3233

33-
@patch('os.path.exists')
34-
def test_resolve_interpreter_with_nonexistent_interpreter(mock_exists):
34+
@patch('virtualenv.get_installed_pythons', return_value={'foo': 'bar'})
35+
@patch('os.path.exists', return_value=False)
36+
def test_resolve_interpreter_with_nonexistent_interpreter(
37+
mock_exists, mock_get_installed_pythons):
3538
"""Should SystemExit with an nonexistent python interpreter path"""
36-
mock_exists.return_value = False
37-
3839
with pytest.raises(SystemExit):
3940
virtualenv.resolve_interpreter("/usr/bin/python53")
4041

4142
mock_exists.assert_called_with("/usr/bin/python53")
4243

4344

44-
@patch('os.path.exists')
45-
def test_resolve_interpreter_with_invalid_interpreter(mock_exists):
45+
@patch('virtualenv.is_executable', return_value=False)
46+
@patch('os.path.exists', return_value=True)
47+
def test_resolve_interpreter_with_invalid_interpreter(mock_exists,
48+
mock_is_executable):
4649
"""Should exit when with absolute path if not exists"""
47-
mock_exists.return_value = True
48-
virtualenv.is_executable = Mock(return_value=False)
4950
invalid = os.path.abspath("/usr/bin/pyt_hon53")
5051

5152
with pytest.raises(SystemExit):
5253
virtualenv.resolve_interpreter(invalid)
5354

5455
mock_exists.assert_called_with(invalid)
55-
virtualenv.is_executable.assert_called_with(invalid)
56+
mock_is_executable.assert_called_with(invalid)
5657

5758

5859
def test_activate_after_future_statements():

0 commit comments

Comments
 (0)