Skip to content

Commit 1df8f19

Browse files
authored
Add a test to make sure PyGMT works with paths that contain non-ASCII characters (#3280)
1 parent 537d684 commit 1df8f19

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pygmt/tests/test_which.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
Test pygmt.which.
33
"""
44

5+
import os
6+
import sys
57
from pathlib import Path
8+
from tempfile import TemporaryDirectory
69

710
import pytest
811
from pygmt import which
912
from pygmt.helpers import unique_name
13+
from pygmt.session_management import begin, end
1014

1115

1216
def test_which():
@@ -40,3 +44,35 @@ def test_which_fails():
4044
which(bogus_file)
4145
with pytest.raises(FileNotFoundError):
4246
which(fname=[f"{bogus_file}.nc", f"{bogus_file}.txt"])
47+
48+
49+
@pytest.mark.skipif(
50+
sys.platform == "win32",
51+
reason="The Windows mkdir() function doesn't support multi-byte characters",
52+
)
53+
def test_which_nonascii_path(monkeypatch):
54+
"""
55+
Make sure PyGMT works with paths that contain non-ascii characters (e.g., Chinese).
56+
"""
57+
# Create a temporary directory with a Chinese suffix as a fake home directory.
58+
with TemporaryDirectory(suffix="中文") as fakehome:
59+
assert fakehome.endswith("中文") # Make sure fakename contains Chinese.
60+
(Path(fakehome) / ".gmt").mkdir() # Create the ~/.gmt directory.
61+
with monkeypatch.context() as mpatch:
62+
# Set HOME to the fake home directory and GMT will use it.
63+
mpatch.setenv("HOME", fakehome)
64+
# Check if HOME is set correctly
65+
assert os.getenv("HOME") == fakehome
66+
assert os.environ["HOME"] == fakehome
67+
68+
# Start a new session
69+
begin()
70+
# GMT should download the remote file under the new home directory.
71+
fname = which(fname="@static_earth_relief.nc", download="c", verbose="d")
72+
assert fname.startswith(fakehome)
73+
assert fname.endswith("static_earth_relief.nc")
74+
end()
75+
76+
# Make sure HOME is reverted correctly.
77+
assert os.getenv("HOME") != fakehome
78+
assert os.environ["HOME"] != fakehome

0 commit comments

Comments
 (0)