From 0320066c8a738e8bffaab8edba47b4cbb445b8e2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 29 May 2024 22:03:21 +0800 Subject: [PATCH 01/11] Add a test to make sure PyGMT works with paths that contain non-ASCII characters --- pygmt/tests/test_which.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 121d6cf4a27..4f1dfac6cc9 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -2,7 +2,9 @@ Test pygmt.which. """ +import os from pathlib import Path +from tempfile import TemporaryDirectory import pytest from pygmt import which @@ -40,3 +42,29 @@ def test_which_fails(): which(bogus_file) with pytest.raises(FileNotFoundError): which(fname=[f"{bogus_file}.nc", f"{bogus_file}.txt"]) + + +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. + 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 + assert str(Path.home()) == fakehome + + # GMT should download the remote file under the new home directory. + fname = which(fname="@static_earth_relief.nc", download="c") + assert fname.startswith(fakehome) + assert fname.endswith("static_earth_relief.nc") + + # Make sure HOME is reverted correctly. + assert os.getenv("HOME") != fakehome + assert os.environ["HOME"] != fakehome + assert str(Path.home()) != fakehome From e74ebad4dfb8ee156da4a96916ce0bbff8481e51 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 29 May 2024 22:20:34 +0800 Subject: [PATCH 02/11] Try using Path.home().resolve() --- pygmt/tests/test_which.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 4f1dfac6cc9..4a79679dd47 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -57,7 +57,7 @@ def test_which_nonascii_path(monkeypatch): # Check if HOME is set correctly assert os.getenv("HOME") == fakehome assert os.environ["HOME"] == fakehome - assert str(Path.home()) == fakehome + assert str(Path.home().resolve()) == fakehome # GMT should download the remote file under the new home directory. fname = which(fname="@static_earth_relief.nc", download="c") @@ -67,4 +67,4 @@ def test_which_nonascii_path(monkeypatch): # Make sure HOME is reverted correctly. assert os.getenv("HOME") != fakehome assert os.environ["HOME"] != fakehome - assert str(Path.home()) != fakehome + assert str(Path.home().resolve()) != fakehome From d24b33221df03404dd4aa301ab278214995d17b1 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 29 May 2024 22:33:00 +0800 Subject: [PATCH 03/11] Comment out the one that doesn't work --- pygmt/tests/test_which.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 4a79679dd47..058d1d444a7 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -57,7 +57,7 @@ def test_which_nonascii_path(monkeypatch): # Check if HOME is set correctly assert os.getenv("HOME") == fakehome assert os.environ["HOME"] == fakehome - assert str(Path.home().resolve()) == fakehome + # assert str(Path.home().resolve()) == fakehome # GMT should download the remote file under the new home directory. fname = which(fname="@static_earth_relief.nc", download="c") @@ -67,4 +67,4 @@ def test_which_nonascii_path(monkeypatch): # Make sure HOME is reverted correctly. assert os.getenv("HOME") != fakehome assert os.environ["HOME"] != fakehome - assert str(Path.home().resolve()) != fakehome + # assert str(Path.home().resolve()) != fakehome From cd5e3d95eca4fb6b87eefdeabe855f6eaabcc402 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 29 May 2024 22:49:13 +0800 Subject: [PATCH 04/11] Check HOME and fname --- pygmt/tests/test_which.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 058d1d444a7..c2c43fb46b8 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -61,6 +61,8 @@ def test_which_nonascii_path(monkeypatch): # GMT should download the remote file under the new home directory. fname = which(fname="@static_earth_relief.nc", download="c") + print(os.environ["HOME"]) + print(fname) assert fname.startswith(fakehome) assert fname.endswith("static_earth_relief.nc") From 3bb6e12cf0080b7fbc1bfd426fcdd658d27a7dce Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 29 May 2024 23:12:56 +0800 Subject: [PATCH 05/11] verbose mode --- pygmt/tests/test_which.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index c2c43fb46b8..8e4cf518aae 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -60,7 +60,7 @@ def test_which_nonascii_path(monkeypatch): # assert str(Path.home().resolve()) == fakehome # GMT should download the remote file under the new home directory. - fname = which(fname="@static_earth_relief.nc", download="c") + fname = which(fname="@static_earth_relief.nc", download="c", verbose="d") print(os.environ["HOME"]) print(fname) assert fname.startswith(fakehome) From 915b6371c86ba95d5da1e9fc9d58f2bbf80eabe9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 7 Jun 2024 21:10:18 +0800 Subject: [PATCH 06/11] Create the .gmt directory --- pygmt/tests/test_which.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 8e4cf518aae..4a3242af900 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -50,6 +50,7 @@ def test_which_nonascii_path(monkeypatch): """ # Create a temporary directory with a Chinese suffix as a fake home directory. with TemporaryDirectory(suffix="中文") as fakehome: + (Path(fakehome) / ".gmt").mkdir() # Create the ~/.gmt directory. assert fakehome.endswith("中文") # Make sure fakename contains Chinese. with monkeypatch.context() as mpatch: # Set HOME to the fake home directory and GMT will use it. From 0838b4461893c5cc0cf6896d6b949f3f7ee01438 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 7 Jun 2024 21:24:03 +0800 Subject: [PATCH 07/11] End global session and begin --- pygmt/tests/test_which.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 4a3242af900..2452cde9e6d 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -9,6 +9,7 @@ import pytest from pygmt import which from pygmt.helpers import unique_name +from pygmt.session_management import begin, end def test_which(): @@ -59,6 +60,7 @@ def test_which_nonascii_path(monkeypatch): assert os.getenv("HOME") == fakehome assert os.environ["HOME"] == fakehome # assert str(Path.home().resolve()) == fakehome + end() # GMT should download the remote file under the new home directory. fname = which(fname="@static_earth_relief.nc", download="c", verbose="d") @@ -66,6 +68,7 @@ def test_which_nonascii_path(monkeypatch): print(fname) assert fname.startswith(fakehome) assert fname.endswith("static_earth_relief.nc") + begin() # Make sure HOME is reverted correctly. assert os.getenv("HOME") != fakehome From 8deba66253a49718abc44a812253a9700109a164 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 7 Jun 2024 22:01:59 +0800 Subject: [PATCH 08/11] Fix --- pygmt/tests/test_which.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 2452cde9e6d..99f1df81958 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -60,15 +60,14 @@ def test_which_nonascii_path(monkeypatch): assert os.getenv("HOME") == fakehome assert os.environ["HOME"] == fakehome # assert str(Path.home().resolve()) == fakehome - end() - + begin() # GMT should download the remote file under the new home directory. fname = which(fname="@static_earth_relief.nc", download="c", verbose="d") print(os.environ["HOME"]) print(fname) assert fname.startswith(fakehome) assert fname.endswith("static_earth_relief.nc") - begin() + end() # Make sure HOME is reverted correctly. assert os.getenv("HOME") != fakehome From 1399fbc47ef8403c74c9fa32cb50e2f8a8a45b69 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jul 2024 17:55:48 +0800 Subject: [PATCH 09/11] Set GMT_VERBOSE to debug globally --- pygmt/tests/test_which.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 99f1df81958..ab13cd60153 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -7,7 +7,7 @@ from tempfile import TemporaryDirectory import pytest -from pygmt import which +from pygmt import which, config from pygmt.helpers import unique_name from pygmt.session_management import begin, end @@ -49,6 +49,7 @@ def test_which_nonascii_path(monkeypatch): """ Make sure PyGMT works with paths that contain non-ascii characters (e.g., Chinese). """ + config(GMT_VERBOSE="d") # Create a temporary directory with a Chinese suffix as a fake home directory. with TemporaryDirectory(suffix="中文") as fakehome: (Path(fakehome) / ".gmt").mkdir() # Create the ~/.gmt directory. From 1e48b2e1035f2a1460e7493f34777d930322e6ab Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jul 2024 18:32:17 +0800 Subject: [PATCH 10/11] Make the test as xfail on Windows --- pygmt/tests/test_which.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index ab13cd60153..f2b3ec8cf6f 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -3,11 +3,12 @@ """ import os +import sys from pathlib import Path from tempfile import TemporaryDirectory import pytest -from pygmt import which, config +from pygmt import which from pygmt.helpers import unique_name from pygmt.session_management import begin, end @@ -45,27 +46,29 @@ def test_which_fails(): which(fname=[f"{bogus_file}.nc", f"{bogus_file}.txt"]) +@pytest.mark.xfail( + condition=sys.platform == "win32", + reason="The Windows mkdir() function doesn't support non-ASCII characters", +) def test_which_nonascii_path(monkeypatch): """ Make sure PyGMT works with paths that contain non-ascii characters (e.g., Chinese). """ - config(GMT_VERBOSE="d") # Create a temporary directory with a Chinese suffix as a fake home directory. with TemporaryDirectory(suffix="中文") as fakehome: - (Path(fakehome) / ".gmt").mkdir() # Create the ~/.gmt directory. 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 - # assert str(Path.home().resolve()) == 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") - print(os.environ["HOME"]) - print(fname) assert fname.startswith(fakehome) assert fname.endswith("static_earth_relief.nc") end() @@ -73,4 +76,3 @@ def test_which_nonascii_path(monkeypatch): # Make sure HOME is reverted correctly. assert os.getenv("HOME") != fakehome assert os.environ["HOME"] != fakehome - # assert str(Path.home().resolve()) != fakehome From 58f86040040a5537f404fe6661b0089862ef4692 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jul 2024 18:46:59 +0800 Subject: [PATCH 11/11] Use skipif marker --- pygmt/tests/test_which.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index f2b3ec8cf6f..642706fba2e 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -46,9 +46,9 @@ def test_which_fails(): which(fname=[f"{bogus_file}.nc", f"{bogus_file}.txt"]) -@pytest.mark.xfail( - condition=sys.platform == "win32", - reason="The Windows mkdir() function doesn't support non-ASCII characters", +@pytest.mark.skipif( + sys.platform == "win32", + reason="The Windows mkdir() function doesn't support multi-byte characters", ) def test_which_nonascii_path(monkeypatch): """