Best Python code snippet using molecule_python
test_virtualenv.py
Source:test_virtualenv.py
1import virtualenv2import optparse3import os4import shutil5import sys6import tempfile7import pytest8import platform # noqa9from mock import patch, Mock10def test_version():11 """Should have a version string"""12 assert virtualenv.virtualenv_version, "Should have version"13@patch('os.path.exists')14def test_resolve_interpreter_with_absolute_path(mock_exists):15 """Should return absolute path if given and exists"""16 mock_exists.return_value = True17 virtualenv.is_executable = Mock(return_value=True)18 test_abs_path = os.path.abspath("/usr/bin/python53")19 exe = virtualenv.resolve_interpreter(test_abs_path)20 assert exe == test_abs_path, "Absolute path should return as is"21 mock_exists.assert_called_with(test_abs_path)22 virtualenv.is_executable.assert_called_with(test_abs_path)23@patch('os.path.exists')24def test_resolve_interpreter_with_nonexistent_interpreter(mock_exists):25 """Should SystemExit with an nonexistent python interpreter path"""26 mock_exists.return_value = False27 with pytest.raises(SystemExit):28 virtualenv.resolve_interpreter("/usr/bin/python53")29 mock_exists.assert_called_with("/usr/bin/python53")30@patch('os.path.exists')31def test_resolve_interpreter_with_invalid_interpreter(mock_exists):32 """Should exit when with absolute path if not exists"""33 mock_exists.return_value = True34 virtualenv.is_executable = Mock(return_value=False)35 invalid = os.path.abspath("/usr/bin/pyt_hon53")36 with pytest.raises(SystemExit):37 virtualenv.resolve_interpreter(invalid)38 mock_exists.assert_called_with(invalid)39 virtualenv.is_executable.assert_called_with(invalid)40def test_activate_after_future_statements():41 """Should insert activation line after last future statement"""42 script = [43 '#!/usr/bin/env python',44 'from __future__ import with_statement',45 'from __future__ import print_function',46 'print("Hello, world!")'47 ]48 assert virtualenv.relative_script(script) == [49 '#!/usr/bin/env python',50 'from __future__ import with_statement',51 'from __future__ import print_function',52 '',53 "import os; activate_this=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'activate_this.py'); exec(compile(open(activate_this).read(), activate_this, 'exec'), dict(__file__=activate_this)); del os, activate_this",54 '',55 'print("Hello, world!")'56 ]57def test_cop_update_defaults_with_store_false():58 """store_false options need reverted logic"""59 class MyConfigOptionParser(virtualenv.ConfigOptionParser):60 def __init__(self, *args, **kwargs):61 self.config = virtualenv.ConfigParser.RawConfigParser()62 self.files = []63 optparse.OptionParser.__init__(self, *args, **kwargs)64 def get_environ_vars(self, prefix='VIRTUALENV_'):65 yield ("no_site_packages", "1")66 cop = MyConfigOptionParser()67 cop.add_option(68 '--no-site-packages',69 dest='system_site_packages',70 action='store_false',71 help="Don't give access to the global site-packages dir to the "72 "virtual environment (default)")73 defaults = {}74 cop.update_defaults(defaults)75 assert defaults == {'system_site_packages': 0}76def test_install_python_bin():77 """Should create the right python executables and links"""78 tmp_virtualenv = tempfile.mkdtemp()79 try:80 home_dir, lib_dir, inc_dir, bin_dir = \81 virtualenv.path_locations(tmp_virtualenv)82 virtualenv.install_python(home_dir, lib_dir, inc_dir, bin_dir, False,83 False)84 if virtualenv.is_win:85 required_executables = [ 'python.exe', 'pythonw.exe']86 else:87 py_exe_no_version = 'python'88 py_exe_version_major = 'python%s' % sys.version_info[0]89 py_exe_version_major_minor = 'python%s.%s' % (90 sys.version_info[0], sys.version_info[1])91 required_executables = [ py_exe_no_version, py_exe_version_major,92 py_exe_version_major_minor ]93 for pth in required_executables:94 assert os.path.exists(os.path.join(bin_dir, pth)), ("%s should "95 "exist in bin_dir" % pth)96 finally:97 shutil.rmtree(tmp_virtualenv)98@pytest.mark.skipif("platform.python_implementation() == 'PyPy'")99def test_always_copy_option():100 """Should be no symlinks in directory tree"""101 tmp_virtualenv = tempfile.mkdtemp()102 ve_path = os.path.join(tmp_virtualenv, 'venv')103 try:104 virtualenv.create_environment(ve_path, symlink=False)105 for root, dirs, files in os.walk(tmp_virtualenv):106 for f in files + dirs:107 full_name = os.path.join(root, f)108 assert not os.path.islink(full_name), "%s should not be a" \109 " symlink (to %s)" % (full_name, os.readlink(full_name))110 finally:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!