Skip to content

Commit

Permalink
Add a test to make sure PyGMT works with paths that contain non-ASCII…
Browse files Browse the repository at this point in the history
… characters (#3280)
  • Loading branch information
seisman authored Jul 29, 2024
1 parent 537d684 commit 1df8f19
Showing 1 changed file with 36 additions and 0 deletions.
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

0 comments on commit 1df8f19

Please sign in to comment.