def test_ve_bin_path_does_not_exist(self): yay = 'yay' yay_bin = os.path.join(yay, 'bin') def fake_exists(path): if path in (yay, yay_bin): return True return False with patch('os.path.exists', wraps=fake_exists): with raises(exceptions.BadConfig): run.get_environ({}, {}, 'yak') run.get_environ({}, {}, 'yay')
def test_get_environ(): path = "thing" defaults = {"from_defaults": "b"} original = { "PATH": os.path.pathsep.join(["crap", "bad_old_ve/bin", "junk"]), "from_passed": "x", "PYTHONHOME": "removeme", "VIRTUAL_ENV": "bad_old_ve", } passed_environ = original.copy() with FakeEnviron() as os_environ, PatchedModule(os.path, exists=lambda path: True): result = run.get_environ(passed_environ, defaults, path) # os.environ should not be changed in any way. assert len(os_environ) == 0 # nor should the passed environ be changed in any way. assert sorted(original.items()) == sorted(passed_environ.items()) # but result should inherit from passed assert result["from_passed"] == "x" # and should update from defaults assert result["from_defaults"] == "b" # except with no PYTHONHOME assert "PYTHONHOME" not in result # and PATH is prepended to. but without bad old ve's bin. assert result["PATH"] == os.path.pathsep.join(["thing/bin", "crap", "junk"]) assert result["VIRTUAL_ENV"] == path
def test_ve_path(self): original = {"foo": "bar"} defaults = {} ve_path = "blah" with patch("os.path.exists"): environ = run.get_environ(original, defaults, ve_path) assert environ.get("VIRTUAL_ENV") == ve_path
def test_get_environ(): path = 'thing' defaults = {'from_defaults': 'b'} original = { 'PATH': os.path.pathsep.join(['crap', 'bad_old_ve/bin', 'junk']), 'from_passed': 'x', 'PYTHONHOME': 'removeme', 'VIRTUAL_ENV': 'bad_old_ve', } passed_environ = original.copy() with FakeEnviron() as os_environ, \ PatchedModule(os.path, exists=lambda path: True): result = run.get_environ(passed_environ, defaults, path) # os.environ should not be changed in any way. assert len(os_environ) == 0 # nor should the passed environ be changed in any way. assert sorted(original.items()) == sorted(passed_environ.items()) # but result should inherit from passed assert result['from_passed'] == 'x' # and should update from defaults assert result['from_defaults'] == 'b' # except with no PYTHONHOME assert 'PYTHONHOME' not in result # and PATH is prepended to. but without bad old ve's bin. assert result['PATH'] == os.path.pathsep.join( ['thing/bin', 'crap', 'junk'] ) assert result['VIRTUAL_ENV'] == path
def test_ve_path(self): original = {'foo': 'bar'} defaults = {} ve_path = 'blah' with patch('os.path.exists'): environ = run.get_environ(original, defaults, ve_path) assert environ.get('VIRTUAL_ENV') == ve_path
def test_get_environ(): path = "thing" defaults = {"from_defaults": "b"} original = { "PATH": os.path.pathsep.join(["crap", "bad_old_ve/bin", "junk"]), "from_passed": "x", "PYTHONHOME": "removeme", "VIRTUAL_ENV": "bad_old_ve", } passed_environ = original.copy() with FakeEnviron() as os_environ, \ PatchedModule(os.path, exists=lambda path: True): result = run.get_environ(passed_environ, defaults, path) # os.environ should not be changed in any way. assert len(os_environ) == 0 # nor should the passed environ be changed in any way. assert sorted(original.items()) == sorted(passed_environ.items()) # but result should inherit from passed assert result["from_passed"] == "x" # and should update from defaults assert result["from_defaults"] == "b" # except with no PYTHONHOME assert "PYTHONHOME" not in result # and PATH is prepended to. but without bad old ve's bin. assert result["PATH"] == os.path.pathsep.join( ["thing/bin", "crap", "junk"]) assert result["VIRTUAL_ENV"] == path
def test_copies_original(self): original = {"foo": "bar"} defaults = {} ve_path = "blah" with patch("os.path.exists", return_value=True): environ = run.get_environ(original, defaults, ve_path) assert environ is not original assert environ.get("foo") == "bar"
def test_copies_original(self): original = {'foo': 'bar'} defaults = {} ve_path = 'blah' with patch('os.path.exists', return_value=True): environ = run.get_environ(original, defaults, ve_path) assert environ is not original assert environ.get('foo') == 'bar'
def test_updates_with_defaults(self): original = {"foo": "bar", "yak": "nope"} defaults = {"bam": "pow", "yak": "fur"} ve_path = "blah" with patch("os.path.exists", return_value=True): environ = run.get_environ(original, defaults, ve_path) assert environ.get("bam") == "pow" assert environ.get("yak") == "fur"
def test_updates_with_defaults(self): original = {'foo': 'bar', 'yak': 'nope'} defaults = {'bam': 'pow', 'yak': 'fur'} ve_path = 'blah' with patch('os.path.exists', return_value=True): environ = run.get_environ(original, defaults, ve_path) assert environ.get('bam') == 'pow' assert environ.get('yak') == 'fur'
def test_prefixes_PATH(self): original = {"foo": "bar"} defaults = {} ve_path = "fnood" bin_path = os.path.join(ve_path, "bin") with patch("os.path.exists", return_value=True): environ = run.get_environ(original, defaults, ve_path) PATH = environ.get("PATH", "") paths = PATH.split(os.pathsep) assert paths[0] == bin_path
def test_prefixes_PATH(self): original = {'foo': 'bar'} defaults = {} ve_path = 'fnood' bin_path = os.path.join(ve_path, 'bin') with patch('os.path.exists', return_value=True): environ = run.get_environ(original, defaults, ve_path) PATH = environ.get('PATH', '') paths = PATH.split(os.pathsep) assert paths[0] == bin_path
def _main(environ, argv): """Logic for main(), with less direct system interaction. Routines called here raise InvalidArgument with messages that should be delivered on stderr, to be caught by main. """ options = get_options(argv) if options.version: return handle_version() vexrc = get_vexrc(options, environ) # Handle --shell-config as soon as its arguments are available. if options.shell_to_configure: return handle_shell_config(options.shell_to_configure, vexrc, environ) if options.list is not None: return handle_list(vexrc.get_ve_base(environ), options.list) # Do as much as possible before a possible make, so errors can raise # without leaving behind an unused virtualenv. # get_virtualenv_name is destructive and must happen before get_command cwd = get_cwd(options) ve_base = vexrc.get_ve_base(environ) ve_name = get_virtualenv_name(options) command = get_command(options, vexrc, environ) # Either we create ve_path, get it from options.path or find it # in ve_base. if options.make: if options.path: make_path = os.path.abspath(options.path) else: make_path = os.path.abspath(os.path.join(ve_base, ve_name)) handle_make(environ, options, make_path) ve_path = make_path elif options.path: ve_path = os.path.abspath(options.path) if not os.path.exists(ve_path) or not os.path.isdir(ve_path): raise exceptions.InvalidVirtualenv( "argument for --path is not a directory") else: try: ve_path = get_virtualenv_path(ve_base, ve_name) except exceptions.NoVirtualenvName: options.print_help() raise # get_environ has to wait until ve_path is defined, which might # be after a make; of course we can't run until we have env. env = get_environ(environ, vexrc['env'], ve_path) returncode = run(command, env=env, cwd=cwd) if options.remove: handle_remove(ve_path) if returncode is None: raise exceptions.InvalidCommand( "command not found: {0!r}".format(command[0])) return returncode
def _main(environ, argv): """Logic for main(), with less direct system interaction. Routines called here raise InvalidArgument with messages that should be delivered on stderr, to be caught by main. """ options = get_options(argv) if options.version: return handle_version() vexrc = get_vexrc(options, environ) # Handle --shell-config as soon as its arguments are available. if options.shell_to_configure: return handle_shell_config(options.shell_to_configure, vexrc, environ) if options.list is not None: return handle_list(vexrc.get_ve_base(environ), options.list) # Do as much as possible before a possible make, so errors can raise # without leaving behind an unused virtualenv. # get_virtualenv_name is destructive and must happen before get_command cwd = get_cwd(options) ve_base = vexrc.get_ve_base(environ) ve_name = get_virtualenv_name(options) command = get_command(options, vexrc, environ) # Either we create ve_path, get it from options.path or find it # in ve_base. if options.make: if options.path: make_path = os.path.abspath(options.path) else: make_path = os.path.abspath(os.path.join(ve_base, ve_name)) handle_make(environ, options, make_path) ve_path = make_path elif options.path: ve_path = os.path.abspath(options.path) if not os.path.exists(ve_path) or not os.path.isdir(ve_path): raise exceptions.InvalidVirtualenv( "argument for --path is not a directory") else: try: ve_path = get_virtualenv_path(ve_base, ve_name) except exceptions.NoVirtualenvName: options.print_help() raise # get_environ has to wait until ve_path is defined, which might # be after a make; of course we can't run until we have env. env = get_environ(environ, vexrc['env'], ve_path) returncode = run(command, env=env, cwd=cwd) if options.remove: handle_remove(ve_path) if returncode is None: raise exceptions.InvalidCommand("command not found: {0!r}".format( command[0])) return returncode
def test_removes_old_virtualenv_bin_path(self): new = "new" old = "old" original = {"foo": "bar", "VIRTUALENV": old} defaults = {} new_bin = os.path.join(new, "bin") old_bin = os.path.join(old, "bin") with patch("os.path.exists", return_value=True): environ = run.get_environ(original, defaults, new) PATH = environ.get("PATH", "") paths = PATH.split(os.pathsep) assert paths[0] == new_bin assert old_bin not in paths
def test_removes_old_virtualenv_bin_path(self): new = 'new' old = 'old' original = {'foo': 'bar', 'VIRTUALENV': old} defaults = {} new_bin = os.path.join(new, 'bin') old_bin = os.path.join(old, 'bin') with patch('os.path.exists', return_value=True): environ = run.get_environ(original, defaults, new) PATH = environ.get('PATH', '') paths = PATH.split(os.pathsep) assert paths[0] == new_bin assert old_bin not in paths
def test_fake_windows_env(self): # does not simulate different os.pathsep, etc. # just tests using Script instead of bin, for coverage. original = {"foo": "bar"} defaults = {} ve_path = "fnard" bin_path = os.path.join(ve_path, "Scripts") with patch("platform.system", return_value="Windows"), patch("os.path.exists", return_value=True): assert platform.system() == "Windows" environ = run.get_environ(original, defaults, ve_path) assert environ.get("VIRTUAL_ENV") == ve_path PATH = environ.get("PATH", "") paths = PATH.split(os.pathsep) assert paths[0] == bin_path
def test_fake_windows_env(self): # does not simulate different os.pathsep, etc. # just tests using Script instead of bin, for coverage. original = {'foo': 'bar'} defaults = {} ve_path = 'fnard' bin_path = os.path.join(ve_path, 'Scripts') with patch('platform.system', return_value='Windows'), \ patch('os.path.exists', return_value=True): assert platform.system() == 'Windows' environ = run.get_environ(original, defaults, ve_path) assert environ.get('VIRTUAL_ENV') == ve_path PATH = environ.get('PATH', '') paths = PATH.split(os.pathsep) assert paths[0] == bin_path
def test_fake_windows_env(self): # does not simulate different os.pathsep, etc. # just tests using Script instead of bin, for coverage. original = {"foo": "bar"} defaults = {} ve_path = "fnard" bin_path = os.path.join(ve_path, "Scripts") with patch("platform.system", return_value="Windows"), \ patch("os.path.exists", return_value=True): assert platform.system() == "Windows" environ = run.get_environ(original, defaults, ve_path) assert environ.get("VIRTUAL_ENV") == ve_path PATH = environ.get("PATH", "") paths = PATH.split(os.pathsep) assert paths[0] == bin_path
def test_ve_path_empty_string(self): with raises(exceptions.BadConfig): run.get_environ({}, {}, "")
def test_ve_path_None(self): with raises(exceptions.BadConfig): run.get_environ({}, {}, None)