|
2 | 2 | Test pygmt.which.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import os |
| 6 | +import sys |
5 | 7 | from pathlib import Path
|
| 8 | +from tempfile import TemporaryDirectory |
6 | 9 |
|
7 | 10 | import pytest
|
8 | 11 | from pygmt import which
|
9 | 12 | from pygmt.helpers import unique_name
|
| 13 | +from pygmt.session_management import begin, end |
10 | 14 |
|
11 | 15 |
|
12 | 16 | def test_which():
|
@@ -40,3 +44,35 @@ def test_which_fails():
|
40 | 44 | which(bogus_file)
|
41 | 45 | with pytest.raises(FileNotFoundError):
|
42 | 46 | 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