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_which_command_not_found(self, setup): def commands(ctx): for name in ['unknown3.8']: assert ctx.pyenv(["which", name ]) == (f"pyenv: {name}: command not found", "") settings = {'versions': ['3.8.6'], 'global_ver': '3.8.6'} run_pyenv_test(settings, commands)
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_which_exists_is_local(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') settings = {'versions': ['3.8.5'], 'local_ver': '3.8.5'} run_pyenv_test(settings, commands)
def test_which_no_version_defined(self, setup): def commands(ctx): for name in ['python']: assert ctx.pyenv( ["which", name]) == ("No global python version has been set yet. " "Please set the global version by typing:\r\n" "pyenv global 3.7.2") run_pyenv_test({'versions': ['3.8.6']}, commands)
def test_global_unset(self, setup): def commands(ctx): assert ctx.pyenv(["global", "--unset"]) == "" assert ctx.pyenv("global") == "no global version configured" settings = { 'versions': ["3.8.9"], 'global_ver': "3.8.9", } run_pyenv_test(settings, commands)
def test_version_name_help(): def commands(ctx): for args in [ ["--help", "version-name"], ["help", "version-name"], ["version-name", "--help"], ]: stdout = "\r\n".join(ctx.pyenv(args).splitlines()[:2]).strip() assert stdout == "Usage: pyenv version-name" run_pyenv_test({}, commands)
def test_global_set_unknown_version(self, setup): def commands(ctx): assert ctx.pyenv(["global", "3.7.8"]) == not_installed_output("3.7.8") settings = { 'versions': ["3.8.9"], 'global_ver': "3.8.9", } run_pyenv_test(settings, commands)
def test_shell_help(self, setup): def commands(ctx): for args in [ ["--help", "shell"], ["help", "shell"], ["shell", "--help"], ]: stdout, stderr = ctx.pyenv(args) assert ("\r\n".join(stdout.splitlines()[:2]), stderr) == (pyenv_shell_help(), "") run_pyenv_test({}, commands)
def test_local_set_installed_version(self, setup): def commands(ctx): assert ctx.pyenv(["local", "3.7.7"]) == ("", "") assert ctx.pyenv("local") == ("3.7.7", "") settings = { 'versions': ["3.7.7", "3.8.9"], 'local_ver': "3.8.9", } run_pyenv_test(settings, commands)
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_whence_no_arg(self, setup): def commands(ctx): assert ctx.pyenv("whence") == (pyenv_whence_usage(), "") assert ctx.pyenv(["whence", "--help"]) == (pyenv_whence_usage(), "") assert ctx.pyenv(["--help", "whence"]) == (pyenv_whence_usage(), "") assert ctx.pyenv(["help", "whence"]) == (pyenv_whence_usage(), "") run_pyenv_test({'versions': ['3.7.7']}, commands)
def test_rehash_global_version(self, setup): def commands(ctx): assert ctx.pyenv('rehash') == "" assert_shims(ctx.pyenv_path, '3.8.6') assert_shims(ctx.pyenv_path, '3.8.7') settings = { 'versions': ['3.8.6', '3.8.7'], 'global_ver': '3.8.6', } run_pyenv_test(settings, commands)
def test_vname_help(): def commands(ctx): for args in [ ["--help", "vname"], ["help", "vname"], ["vname", "--help"], ]: stdout, stderr = ctx.pyenv(args) stdout = "\r\n".join(stdout.splitlines()[:2]).strip() assert (stdout, stderr) == ("Usage: pyenv vname", "") run_pyenv_test({}, commands)
def test_shell_help(self, setup): def commands(ctx): for args in [ ["--help", "shell"], ["help", "shell"], ["shell", "--help"], ]: assert "\r\n".join( ctx.pyenv(args).splitlines()[:2]) == pyenv_shell_help() run_pyenv_test({}, commands)
def test_no_version(command): def commands(ctx): assert ctx.pyenv(command) == ( ( "No global python version has been set yet. " "Please set the global version by typing:\r\n" "pyenv global 3.7.2" ), "" ) run_pyenv_test({}, commands)
def test_list_uninstalled_current_global_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.5" } run_pyenv_test(settings, commands)
def test_list_current_global_version(self, setup): def commands(ctx): assert ctx.pyenv("versions") == (( f" 3.6.5\r\n" f"* 3.7.7 (set by {ctx.pyenv_path}\\version)\r\n" f" 3.9.1"), "") settings = { 'versions': ["3.6.5", "3.7.7", "3.9.1"], 'global_ver': "3.7.7" } run_pyenv_test(settings, commands)
def test_list_current_local_many_versions(self, setup): def commands(ctx): assert ctx.pyenv("versions") == (( f"* 3.6.5 (set by {ctx.local_path}\\.python-version)\r\n" f"* 3.7.7 (set by {ctx.local_path}\\.python-version)\r\n" f" 3.9.1"), "") settings = { 'versions': ["3.6.5", "3.7.7", "3.9.1"], 'global_ver': "3.9.1", 'local_ver': "3.6.5\n3.7.7\n" } run_pyenv_test(settings, 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 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_which_exists_is_global_other_version(self, setup): def commands(ctx): for name in ['python38', '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'], 'global_ver': '3.9.1' } run_pyenv_test(settings, commands)
def test_which_many_local_versions(self, setup): def commands(ctx): cases = [ ('python37', r'3.7.7\python37.exe'), ('python38', r'3.8.2\python38.exe'), ('pip3.7', r'3.7.7\Scripts\pip3.7.exe'), ('pip3.8', r'3.8.2\Scripts\pip3.8.exe'), ] for (name, path) in cases: assert_paths_equal(ctx.pyenv(["which", name]), rf'{ctx.pyenv_path}\versions\{path}') settings = { 'versions': ['3.7.7', '3.8.2', '3.9.1'], 'local_ver': '3.7.7\n3.8.2\n' } run_pyenv_test(settings, commands)
def test_bad_path(self, setup): 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, "") run_pyenv_test({'global_ver': "3.7.2"}, commands)
def test_global_version(self, setup): def commands(ctx): assert ctx.pyenv("version") == rf'3.7.2 (set by {ctx.pyenv_path}\version)' run_pyenv_test({'global_ver': "3.7.2"}, commands)
def test_shell_set_unknown_version(self, setup): def commands(ctx): assert ctx.pyenv(["shell", "3.7.8"]) == (not_installed_output("3.7.8"), "") run_pyenv_test({'versions': ["3.8.9"]}, 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)