Repository: pyenv-win/pyenv-win (language: python)

Issue:
Add tests
- Introduce helpers to create temp pyenv-win
- Add tests for global & versions commands


Discussion / hints:

Fix patch:
diff --git a/.gitignore b/.gitignore
index c7a05eaf..783f6500 100644
--- a/.gitignore
+++ b/.gitignore
@@ -109,3 +109,6 @@ version
 versions/
 exec.bat
 
+# PyCharm
+.idea/
+
diff --git a/pyenv-win/libexec/pyenv-global.bat b/pyenv-win/libexec/pyenv-global.bat
index 5d33701a..c17c6670 100644
--- a/pyenv-win/libexec/pyenv-global.bat
+++ b/pyenv-win/libexec/pyenv-global.bat
@@ -3,6 +3,7 @@ setlocal
 
 if "%1" == "--help" (
 echo Usage: pyenv global ^<version^>
+echo        pyenv global --unset
 echo.
 echo Sets the global Python version. You can override the global version at
 echo any time by setting a directory-specific version with `pyenv local'
diff --git a/pyenv-win/libexec/pyenv.vbs b/pyenv-win/libexec/pyenv.vbs
index d52b4383..0b71bfa1 100644
--- a/pyenv-win/libexec/pyenv.vbs
+++ b/pyenv-win/libexec/pyenv.vbs
@@ -383,8 +383,14 @@ Sub CommandGlobal(arg)
             WScript.Echo ver(0)
         End If
     Else
-        ver = Check32Bit(arg(1))
-        SetGlobalVersion ver
+        If arg(1) = "--unset" Then
+            ver = ""
+            objfs.DeleteFile strPyenvHome &"\version", True
+            Exit Sub
+        Else
+            ver = Check32Bit(arg(1))
+            SetGlobalVersion ver
+        End If
     End If
 End Sub
 


Test patch:
diff --git a/tests/test_pyenv_feature_commands.py b/tests/test_pyenv_feature_commands.py
index 786b3eaa..1b5abd79 100644
--- a/tests/test_pyenv_feature_commands.py
+++ b/tests/test_pyenv_feature_commands.py
@@ -3,7 +3,7 @@
 
 class TestPyenvFeatureCommands(TestPyenvBase):
     def test_check_pyenv_commands_list(self, setup):
-        # TODO: assert the list of commnds
+        # TODO: assert the list of commands
         result = subprocess.run(['pyenv'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         result = str(result.stdout, "utf-8")
         pass
diff --git a/tests/test_pyenv_feature_duplicate.py b/tests/test_pyenv_feature_duplicate.py
index fbc4162c..48760954 100644
--- a/tests/test_pyenv_feature_duplicate.py
+++ b/tests/test_pyenv_feature_duplicate.py
@@ -3,7 +3,7 @@
 
 class TestPyenvFeatureDuplicate(TestPyenvBase):
     def test_check_pyenv_duplicate(self, setup):
-        # TODO: assert the list of commnds
+        # TODO: assert the list of commands
         result = subprocess.run(['pyenv'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         result = str(result.stdout, "utf-8")
         pass
diff --git a/tests/test_pyenv_feature_global.py b/tests/test_pyenv_feature_global.py
index 7710f736..5fe27351 100644
--- a/tests/test_pyenv_feature_global.py
+++ b/tests/test_pyenv_feature_global.py
@@ -1,5 +1,55 @@
+import subprocess
+import tempfile
+from pathlib import Path
 from test_pyenv import TestPyenvBase
+from test_pyenv_helpers import install_pyenv, temp_pyenv, working_directory
+
 
 class TestPyenvFeatureGlobal(TestPyenvBase):
-    pass
-    
+    def test_no_global_version(self, setup):
+        with temp_pyenv("global") as output:
+            assert output == "no global version configured"
+
+    def test_global_version_defined(self, setup):
+        with temp_pyenv("global", global_ver="3.8.9") as output:
+            assert output == "3.8.9"
+
+    def test_global_set_installed_version(self, setup):
+        with tempfile.TemporaryDirectory() as tmp_path:
+            install_pyenv(tmp_path, ["3.7.7", "3.8.9"], "3.8.9")
+            with working_directory(tmp_path):
+                bat = Path(tmp_path, r'bin\pyenv.bat')
+
+                def pyenv_global(option=None):
+                    args = ['cmd', '/d', '/c', f'call {bat}', "global"]
+                    if option is not None:
+                        args.append(option)
+                    result = subprocess.run(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                    output = str(result.stdout, "utf-8").strip()
+                    return output
+
+                assert pyenv_global("3.7.7") == ""
+                assert pyenv_global() == "3.7.7"
+
+    def test_global_set_unknown_version(self, setup):
+        with temp_pyenv("global", "3.7.8", ["3.8.9"], "3.8.9") as output:
+            assert output == ("pyenv specific python requisite didn't meet. "
+                              "Project is using different version of python.\r\n"
+                              "Install python '3.7.8' by typing: 'pyenv install 3.7.8'")
+
+    def test_global_unset(self, setup):
+        with tempfile.TemporaryDirectory() as tmp_path:
+            install_pyenv(tmp_path, ["3.8.9"], "3.8.9")
+            with working_directory(tmp_path):
+                bat = Path(tmp_path, r'bin\pyenv.bat')
+
+                def pyenv_global(option=None):
+                    args = ['cmd', '/d', '/c', f'call {bat}', "global"]
+                    if option is not None:
+                        args.append(option)
+                    result = subprocess.run(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                    output = str(result.stdout, "utf-8").strip()
+                    return output
+
+                assert pyenv_global("--unset") == ""
+                assert pyenv_global() == "no global version configured"
diff --git a/tests/test_pyenv_feature_versions.py b/tests/test_pyenv_feature_versions.py
index 209f1f13..1a95ff3e 100644
--- a/tests/test_pyenv_feature_versions.py
+++ b/tests/test_pyenv_feature_versions.py
@@ -1,5 +1,14 @@
 from test_pyenv import TestPyenvBase
+from test_pyenv_helpers import temp_pyenv
+
 
 class TestPyenvFeatureVersions(TestPyenvBase):
-    pass
-    
+    def test_list_no_version(self, setup):
+        with temp_pyenv("versions") as output:
+            assert output == ""
+
+    def test_list_all_versions(self, setup):
+        versions = ['3.7.4', '3.8.5']
+        with temp_pyenv("versions", versions=versions) as output:
+            for v in versions:
+                assert v in output
diff --git a/tests/test_pyenv_helpers.py b/tests/test_pyenv_helpers.py
new file mode 100644
index 00000000..8ebd069d
--- /dev/null
+++ b/tests/test_pyenv_helpers.py
@@ -0,0 +1,53 @@
+import os
+import shutil
+import subprocess
+import tempfile
+from contextlib import contextmanager
+from pathlib import Path
+
+
+@contextmanager
+def working_directory(path):
+    prev_cwd = os.getcwd()
+    os.chdir(path)
+    try:
+        yield
+    finally:
+        os.chdir(prev_cwd)
+
+
+def install_pyenv(path, versions=None, global_ver=None):
+    if versions is None:
+        versions = []
+    src_path = Path(__file__).resolve().parents[1].joinpath('pyenv-win')
+    dirs = [r'bin', r'libexec\libs', r'shims', r'versions']
+    for d in dirs:
+        os.makedirs(Path(path, d))
+    files = [r'bin\pyenv.bat',
+             r'libexec\pyenv.vbs',
+             r'libexec\libs\pyenv-install-lib.vbs',
+             r'libexec\libs\pyenv-lib.vbs']
+    for f in files:
+        shutil.copy(src_path.joinpath(f), Path(path, f))
+    versions_dir = Path(path, r'versions')
+    for v in versions:
+        os.mkdir(versions_dir.joinpath(v))
+    if global_ver is not None:
+        with open(Path(path, "version"), "w") as f:
+            print(global_ver, file=f)
+
+
+@contextmanager
+def temp_pyenv(command, option=None, versions=None, global_ver=None):
+    if versions is None:
+        versions = []
+    with tempfile.TemporaryDirectory() as tmp_path:
+        install_pyenv(tmp_path, versions, global_ver)
+        with working_directory(tmp_path):
+            bat = Path(tmp_path, r'bin\pyenv.bat')
+            args = ['cmd', '/d', '/c', f'call {bat}', command]
+            if option is not None:
+                args.append(option)
+            result = subprocess.run(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+            result = str(result.stdout, "utf-8").strip()
+            yield result