Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test to make sure PyGMT works with paths that contain non-ASCII characters #3280

Merged
merged 14 commits into from
Jul 29, 2024
36 changes: 36 additions & 0 deletions pygmt/tests/test_which.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
Test pygmt.which.
"""

import os
import sys
from pathlib import Path
from tempfile import TemporaryDirectory

import pytest
from pygmt import which
from pygmt.helpers import unique_name
from pygmt.session_management import begin, end


def test_which():
Expand Down Expand Up @@ -40,3 +44,35 @@ def test_which_fails():
which(bogus_file)
with pytest.raises(FileNotFoundError):
which(fname=[f"{bogus_file}.nc", f"{bogus_file}.txt"])


@pytest.mark.skipif(
sys.platform == "win32",
reason="The Windows mkdir() function doesn't support multi-byte characters",
)
def test_which_nonascii_path(monkeypatch):
"""
Make sure PyGMT works with paths that contain non-ascii characters (e.g., Chinese).
"""
# Create a temporary directory with a Chinese suffix as a fake home directory.
with TemporaryDirectory(suffix="中文") as fakehome:
assert fakehome.endswith("中文") # Make sure fakename contains Chinese.
(Path(fakehome) / ".gmt").mkdir() # Create the ~/.gmt directory.
with monkeypatch.context() as mpatch:
# Set HOME to the fake home directory and GMT will use it.
mpatch.setenv("HOME", fakehome)
# Check if HOME is set correctly
assert os.getenv("HOME") == fakehome
assert os.environ["HOME"] == fakehome

# Start a new session
begin()
# GMT should download the remote file under the new home directory.
fname = which(fname="@static_earth_relief.nc", download="c", verbose="d")
assert fname.startswith(fakehome)
assert fname.endswith("static_earth_relief.nc")
end()

# Make sure HOME is reverted correctly.
assert os.getenv("HOME") != fakehome
assert os.environ["HOME"] != fakehome