Skip to content

Commit 0a5c32b

Browse files
authored
Merge pull request #146 from cosine0/gitbash
Fix #145
2 parents 60f1c8e + dc7592a commit 0a5c32b

5 files changed

Lines changed: 46 additions & 35 deletions

File tree

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ testpaths = tests/
7676
norecursedirs = .* build dist news tasks docs
7777
markers =
7878
parse
79-
skip_nt
8079
is_python
8180
filterwarnings =
8281
ignore::DeprecationWarning

src/pythonfinder/environment.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import os
44
import platform
55
import sys
6-
import posixpath
7-
import ntpath
86
import re
97
import shutil
108

@@ -21,28 +19,30 @@ def possibly_convert_to_windows_style_path(path):
2119
if not isinstance(path, str):
2220
path = str(path)
2321
# Check if the path is in Unix-style (Git Bash)
24-
if os.name == 'nt':
25-
if path.startswith('/'):
26-
drive, tail = re.match(r"^/([a-zA-Z])/(.*)", path).groups()
27-
revised_path = drive.upper() + ":\\" + tail.replace('/', '\\')
28-
return revised_path
29-
elif path.startswith('\\'):
30-
drive, tail = re.match(r"^\\([a-zA-Z])\\(.*)", path).groups()
31-
revised_path = drive.upper() + ":\\" + tail.replace('\\', '\\')
32-
return revised_path
33-
22+
if os.name != 'nt':
23+
return path
24+
if os.path.exists(path):
25+
return path
26+
match = re.match(r"[/\\]([a-zA-Z])[/\\](.*)", path)
27+
if match is None:
28+
return path
29+
drive, rest_of_path = match.groups()
30+
rest_of_path = rest_of_path.replace("/", "\\")
31+
revised_path = f"{drive.upper()}:\\{rest_of_path}"
32+
if os.path.exists(revised_path):
33+
return revised_path
3434
return path
3535

3636

3737
PYENV_ROOT = os.path.expanduser(
3838
os.path.expandvars(os.environ.get("PYENV_ROOT", "~/.pyenv"))
3939
)
4040
PYENV_ROOT = possibly_convert_to_windows_style_path(PYENV_ROOT)
41-
PYENV_INSTALLED = shutil.which("pyenv") != None
41+
PYENV_INSTALLED = shutil.which("pyenv") is not None
4242
ASDF_DATA_DIR = os.path.expanduser(
4343
os.path.expandvars(os.environ.get("ASDF_DATA_DIR", "~/.asdf"))
4444
)
45-
ASDF_INSTALLED = shutil.which("asdf") != None
45+
ASDF_INSTALLED = shutil.which("asdf") is not None
4646
IS_64BIT_OS = None
4747
SYSTEM_ARCH = platform.architecture()[0]
4848

@@ -61,20 +61,9 @@ def possibly_convert_to_windows_style_path(path):
6161
"""
6262

6363

64-
def join_path_for_platform(path, path_parts):
65-
# If we're on Unix or Unix-like system
66-
if os.name == 'posix' or sys.platform == 'linux':
67-
return posixpath.join(path, *path_parts)
68-
# If we're on Windows
69-
elif os.name == 'nt' or sys.platform == 'win32':
70-
return ntpath.join(path, *path_parts)
71-
else:
72-
raise Exception("Unknown environment")
73-
74-
7564
def set_asdf_paths():
7665
if ASDF_INSTALLED:
77-
python_versions = join_path_for_platform(ASDF_DATA_DIR, ["installs", "python"])
66+
python_versions = os.path.join(ASDF_DATA_DIR, "installs", "python")
7867
try:
7968
# Get a list of all files and directories in the given path
8069
all_files_and_dirs = os.listdir(python_versions)
@@ -92,10 +81,10 @@ def set_pyenv_paths():
9281
if PYENV_INSTALLED:
9382
is_windows = False
9483
if os.name == "nt":
95-
python_versions = join_path_for_platform(PYENV_ROOT, ["pyenv-win", "versions"])
84+
python_versions = os.path.join(PYENV_ROOT, "pyenv-win", "versions")
9685
is_windows = True
9786
else:
98-
python_versions = join_path_for_platform(PYENV_ROOT, ["versions"])
87+
python_versions = os.path.join(PYENV_ROOT, "versions")
9988
try:
10089
# Get a list of all files and directories in the given path
10190
all_files_and_dirs = os.listdir(python_versions)

tests/conftest.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
pythoninfo = namedtuple("PythonVersion", ["name", "version", "path", "arch"])
2525

2626

27-
def pytest_runtest_setup(item):
28-
if item.get_closest_marker("skip_nt") is not None and os.name == "nt":
29-
pytest.skip("does not run on windows")
30-
31-
3227
@pytest.fixture
3328
def pathlib_tmpdir(request, tmpdir):
3429
yield Path(str(tmpdir))

tests/test_environment.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import re
5+
import tempfile
6+
7+
import pytest
8+
9+
from pythonfinder.environment import possibly_convert_to_windows_style_path
10+
11+
12+
@pytest.mark.skipif(os.name != 'nt', reason="Only run on Windows")
13+
def test_possibly_convert_to_windows_style_path():
14+
# Create a temporary directory
15+
with tempfile.TemporaryDirectory() as tmpdirname:
16+
# Get an input path in the form "\path\to\tempdir"
17+
drive, tail = os.path.splitdrive(tmpdirname)
18+
input_path = tail.replace('/', '\\')
19+
assert re.match(r"(\\[^/\\]+)+", input_path)
20+
revised_path = possibly_convert_to_windows_style_path(input_path)
21+
assert input_path == revised_path
22+
23+
# Get an input path in the form "/c/path/to/tempdir"
24+
input_path = '/' + drive[0].lower() + tail.replace('\\', '/')
25+
assert re.match(r"/[a-z](/[^/\\]+)+", input_path)
26+
expected = drive.upper() + tail.replace('/', '\\')
27+
revised_path = possibly_convert_to_windows_style_path(input_path)
28+
assert expected == revised_path

tests/test_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ def get_python_version(path, orig_fn=None):
9595
assert isinstance(parsed.version, Version)
9696

9797

98-
@pytest.mark.skip_nt
98+
@pytest.mark.skipif(os.name == "nt", reason="Does not run on Windows")
9999
def test_pythonfinder(expected_python_versions, all_python_versions):
100100
assert sorted(expected_python_versions) == sorted(all_python_versions)

0 commit comments

Comments
 (0)