Skip to content

Commit 59f25a2

Browse files
committed
make path resolve strict for windows
1 parent 5ca5ec2 commit 59f25a2

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

PyStemmusScope/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from pathlib import Path
23
import numpy as np
34

@@ -25,10 +26,13 @@ def convert_to_lsm_coordinates(lat, lon):
2526
return np.round(lat).astype(int), np.round(lon).astype(int)
2627

2728

29+
def os_name():
30+
return os.name
31+
32+
2833
def to_absolute_path(
2934
input_path: str,
3035
parent: Path = None,
31-
must_exist: bool = False,
3236
must_be_in_parent=True,
3337
) -> Path:
3438
"""Parse input string as :py:class:`pathlib.Path` object.
@@ -43,6 +47,9 @@ def to_absolute_path(
4347
Returns:
4448
The input path that is an absolute path and a :py:class:`pathlib.Path` object.
4549
"""
50+
# care for windows, see issue 22
51+
must_exist = os_name() == 'nt'
52+
4653
pathlike = Path(input_path)
4754
if parent:
4855
pathlike = parent.joinpath(pathlike)

tests/test_utils.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11

22
from pathlib import Path
3+
from unittest.mock import patch
34

45
import pytest
56
from PyStemmusScope import utils
67

78
# TODO add test for convert_to_lsm_coordinates
89

10+
911
def test_to_absolute_path():
1012
input_path = "~/nonexistent_file.txt"
1113
parsed = utils.to_absolute_path(input_path)
1214
expected = Path.home() / "nonexistent_file.txt"
1315
assert parsed == expected
1416

15-
16-
def test_to_absolute_path_must_exist():
17+
@patch("PyStemmusScope.utils.os_name")
18+
def test_to_absolute_path_must_exist(mocked_osname):
1719
input_path = "~/nonexistent_file.txt"
20+
mocked_osname.return_value = "nt"
1821
with pytest.raises(FileNotFoundError):
19-
utils.to_absolute_path(input_path, must_exist=True)
22+
utils.to_absolute_path(input_path)
2023

2124

2225
def test_to_absolute_path_with_absolute_input_and_parent(tmp_path):
@@ -33,16 +36,24 @@ def test_to_absolute_path_with_relative_input_and_parent(tmp_path):
3336

3437

3538
def test_to_absolute_path_with_relative_input_and_no_parent():
36-
input_path = "nonexistent_file.txt"
39+
input_path = "./input_dir"
40+
41+
# care for windows, see issue 22
42+
Path(input_path).mkdir(exist_ok=True)
43+
3744
parsed = utils.to_absolute_path(input_path)
38-
expected = Path.cwd() / "nonexistent_file.txt"
45+
expected = Path.cwd() / "input_dir"
3946
assert parsed == expected
4047

4148

4249
def test_to_absolute_path_with_relative_input_and_relative_parent():
43-
input_path = "nonexistent_file.txt"
50+
input_path = "./input_dir"
51+
52+
# care for windows, see issue 22
53+
Path(input_path).mkdir(exist_ok=True)
54+
4455
parsed = utils.to_absolute_path(input_path, parent=Path("."))
45-
expected = Path.cwd() / "nonexistent_file.txt"
56+
expected = Path.cwd() / "input_dir"
4657
assert parsed == expected
4758

4859

0 commit comments

Comments
 (0)