def test_shell_version(self, setup): def commands(ctx): assert ctx.pyenv("version") == "3.9.2 (set by %PYENV_VERSION%)" settings = { 'global_ver': "3.7.5", 'local_ver': "3.8.6", } with TemporaryEnvironment({"PYENV_VERSION": "3.9.2"}): run_pyenv_test(settings, commands)
def test_shell_version(command): def commands(ctx): assert ctx.pyenv(command) == ("3.9.2", "") settings = { 'global_ver': "3.7.5", 'local_ver': "3.8.6", } with TemporaryEnvironment({"PYENV_VERSION": "3.9.2"}): run_pyenv_test(settings, commands)
def test_which_exists_is_shell_not_installed(self, setup): def commands(ctx): for name in ['python', 'python3', 'python38', 'pip3', 'pip3.8']: assert ctx.pyenv([ "which", name ]) == "pyenv: version `3.8.5' is not installed (set by 3.8.5)" with TemporaryEnvironment({"PYENV_VERSION": "3.8.5"}): run_pyenv_test({}, commands)
def test_exec_arg(self, setup, command, arg): with TemporaryEnvironment({'World': 'Earth'}): stdout, stderr = self.__class__.ctx.exec([ *command(self.__class__.ctx.pyenv_path), "-c", "import sys; print(sys.argv[1])", arg ]) assert (stdout, stderr) == (arg.replace('%World%', 'Earth'), "")
def test_exec_set_var(self, setup): with TemporaryEnvironment({'VARIABLE': 'OLD'}): pyenv = str(self.__class__.ctx.pyenv_path / "bin" / "pyenv.bat") args = [ pyenv, "exec", "SET", "VARIABLE=NEW", "&&", "SET", "VARIABLE" ] result = subprocess.run(args, capture_output=True, encoding="utf8") # \x0c: generated by cls in cmd AutoRun stdout = result.stdout.strip("\r\n\x0c") assert stdout == "VARIABLE=NEW"
def test_which_exists_is_shell(self, setup): def commands(ctx): for name in ['python', 'python3', 'python38', 'pip3', 'pip3.8']: sub_dir = '' if 'python' in name else 'Scripts\\' assert_paths_equal( ctx.pyenv(["which", name]), rf'{ctx.pyenv_path}\versions\3.8.5\{sub_dir}{name}.exe') with TemporaryEnvironment({"PYENV_VERSION": "3.8.5"}): run_pyenv_test({'versions': ['3.8.5']}, commands)
def test_rehash_shell_version(self, setup): def commands(ctx): assert ctx.pyenv('rehash') == "" global_ver = '3.7.5' local_ver = '3.8.6' shell_ver = '3.9.1' settings = { 'versions': [global_ver, local_ver, shell_ver], 'global_ver': global_ver, 'local_ver': local_ver } with TemporaryEnvironment({"PYENV_VERSION": shell_ver}): run_pyenv_test(settings, commands)
def test_list_uninstalled_shell_version(self, setup): def commands(ctx): assert ctx.pyenv("versions") == ((f" 3.6.5\r\n" f" 3.7.7\r\n" f" 3.9.1"), "") settings = { 'versions': ["3.6.5", "3.7.7", "3.9.1"], 'global_ver': "3.7.7", 'local_ver': "3.6.5" } with TemporaryEnvironment({"PYENV_VERSION": "3.9.2"}): run_pyenv_test(settings, commands)
def test_shell_set_installed_version(self, setup): def commands(ctx): pyenv_bat = Path(ctx.pyenv_path, r'bin\pyenv.bat') tmp_bat = str(Path(ctx.local_path, "tmp.bat")) with open(tmp_bat, "w") as f: # must chain commands because env var is lost when cmd ends print(f'@call "{pyenv_bat}" shell 3.7.7 && call "{pyenv_bat}" shell', file=f) args = ['cmd', '/d', '/c', 'call', tmp_bat] result = subprocess.run(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = str(result.stdout, "utf-8").strip() assert output == "3.7.7" with TemporaryEnvironment({"PYENV_VERSION": "3.8.9"}): run_pyenv_test({'versions': ["3.7.7", "3.8.9"]}, commands)
def setup_class(cls): settings = {'versions': ['3.7.7'], 'global_ver': '3.7.7'} cls._temp_env = TempPyEnv(settings) cls.ctx = cls._temp_env.context cls._temp_path = TemporaryEnvironment({ "PATH": f"{os.path.dirname(sys.executable)};" f"{str(Path(cls.ctx.pyenv_path, 'bin'))};" f"{str(Path(cls.ctx.pyenv_path, 'shims'))};" f"{os.environ['PATH']}" }) cls._temp_path.__enter__() cls.ctx.pyenv('rehash') os.unlink(str(Path(cls.ctx.pyenv_path, r'versions\3.7.7\python.exe')))
def test_exec_arg(self, setup, command, arg): with TemporaryEnvironment({'World': 'Earth'}): args = [ *command(self.__class__.ctx.pyenv_path), "-c", "import sys; print(sys.argv[1])", arg ] result = subprocess.run(args, capture_output=True, encoding="utf8") # \x0c: generated by cls in cmd AutoRun stdout = result.stdout.strip("\r\n\x0c") # " is escaped as \" by python, so we unescape it here stdout = stdout.replace('\\', '"') stderr = result.stderr.strip() if stderr != "": print(f'stderr: {stderr}') assert stdout == arg.replace('%World%', 'Earth')
def commands(ctx): touch(Path(ctx.local_path, 'python.exe')) with TemporaryEnvironment( {"PATH": f"{ctx.local_path};{os.environ['PATH']}"}): touch(Path(ctx.pyenv_path, r'shims\python.bat')) stdout, stderr = ctx.pyenv("version") expected = ( f'\x1b[91mFATAL: Found \x1b[95m{ctx.local_path}\\python.exe\x1b[91m version ' f'before pyenv in PATH.\x1b[0m\r\n' f'\x1b[91mPlease remove \x1b[95m{ctx.local_path}\\\x1b[91m from ' f'PATH for pyenv to work properly.\x1b[0m\r\n' f'3.7.2 (set by {ctx.pyenv_path}\\version)') # Fix 8.3 mismatch in GitHub actions stdout = stdout.replace('RUNNER~1', 'runneradmin') expected = expected.replace('RUNNER~1', 'runneradmin') assert (stdout, stderr) == (expected, "")
def setup_class(cls): settings = { 'versions': ['3.7.7', '3.8.9', '3.10.0'], 'global_ver': '3.7.7', 'local_ver': "3.7.7\n3.8.9\n", } cls._temp_env = TempPyEnv(settings) cls.ctx = cls._temp_env.context cls._temp_path = TemporaryEnvironment({"PATH": f"{os.path.dirname(sys.executable)};" f"{str(Path(cls.ctx.pyenv_path, 'bin'))};" f"{str(Path(cls.ctx.pyenv_path, 'shims'))};" f"{os.environ['PATH']}"}) cls._temp_path.__enter__() cls.ctx.pyenv('rehash') for v in settings['versions']: os.unlink(str(Path(cls.ctx.pyenv_path, fr'versions\{v}\python.exe')))
def test_which_exists_is_shell_other_version(self, setup): def commands(ctx): for name in ['python38', 'python3.8', 'pip3.8']: assert ctx.pyenv(["which", name]) == ( f"pyenv: {name}: command not found\r\n" f"\r\n" f"The '{name}' command exists in these Python versions:\r\n" f" 3.8.2\r\n" f" 3.8.6\r\n" f" ") settings = { 'versions': ['3.8.2', '3.8.6', '3.9.1'], } with TemporaryEnvironment({"PYENV_VERSION": "3.9.1"}): run_pyenv_test(settings, commands)
def test_shell_version_defined(self, setup): def commands(ctx): assert ctx.pyenv("shell") == ("3.9.2", "") with TemporaryEnvironment({"PYENV_VERSION": "3.9.2"}): run_pyenv_test({}, commands)
def test_no_shell_version(self, setup): def commands(ctx): assert ctx.pyenv("shell") == ("no shell-specific version configured", "") with TemporaryEnvironment({"PYENV_VERSION": ""}): run_pyenv_test({}, commands)