From 895e6792259214ab42e398425e219ba0b83a20e6 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 18 Jul 2024 11:48:09 +0800 Subject: [PATCH 1/3] build_arg_list: Raise an exception if an invalid output file name is given --- pygmt/helpers/utils.py | 8 +++++++- pygmt/tests/test_helpers.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 8997a2b0df1..dd202d2e840 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -350,7 +350,13 @@ def build_arg_list( gmt_args = [str(infile), *gmt_args] else: gmt_args = [str(_file) for _file in infile] + gmt_args - if outfile: + if outfile is not None: + if ( + not isinstance(outfile, str | pathlib.PurePath) + or str(outfile) in {"", ".", ".."} + or str(outfile).endswith(("/", "\\")) + ): + raise GMTInvalidInput(f"Invalid output file name '{outfile}'.") gmt_args.append(f"->{outfile}") return gmt_args diff --git a/pygmt/tests/test_helpers.py b/pygmt/tests/test_helpers.py index ea9e4c87225..274dcd31dbb 100644 --- a/pygmt/tests/test_helpers.py +++ b/pygmt/tests/test_helpers.py @@ -12,6 +12,7 @@ from pygmt.helpers import ( GMTTempFile, args_in_kwargs, + build_arg_list, data_kind, kwargs_to_strings, unique_name, @@ -137,6 +138,17 @@ def test_gmttempfile_read(): assert tmpfile.read(keep_tabs=True) == "in.dat: N = 2\t<1/3>\t<2/4>\n" +@pytest.mark.parametrize( + "outfile", [123, "", ".", "..", "path/to/dir/", Path(), Path("..")] +) +def test_build_arg_list_invalid_output(outfile): + """ + Test that build_arg_list raises an exception when output file name is invalid. + """ + with pytest.raises(GMTInvalidInput): + build_arg_list({}, outfile=outfile) + + def test_args_in_kwargs(): """ Test that args_in_kwargs function returns correct Boolean responses. From 14eac5d1f9e1a9bd97d294ae2ada633c96bdaba8 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 19 Jul 2024 08:59:50 +0800 Subject: [PATCH 2/3] Check windows path Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/tests/test_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_helpers.py b/pygmt/tests/test_helpers.py index 274dcd31dbb..8e2ff5a6fc9 100644 --- a/pygmt/tests/test_helpers.py +++ b/pygmt/tests/test_helpers.py @@ -139,7 +139,7 @@ def test_gmttempfile_read(): @pytest.mark.parametrize( - "outfile", [123, "", ".", "..", "path/to/dir/", Path(), Path("..")] + "outfile", [123, "", ".", "..", "path/to/dir/", r"path\to\dir\", Path(), Path(".."), ] ) def test_build_arg_list_invalid_output(outfile): """ From 070adb318b4f560831718ee0fb6b77a2aa9e8d76 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 19 Jul 2024 09:16:21 +0800 Subject: [PATCH 3/3] Fix the Windows path --- pygmt/tests/test_helpers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_helpers.py b/pygmt/tests/test_helpers.py index 8e2ff5a6fc9..ea966753535 100644 --- a/pygmt/tests/test_helpers.py +++ b/pygmt/tests/test_helpers.py @@ -139,7 +139,8 @@ def test_gmttempfile_read(): @pytest.mark.parametrize( - "outfile", [123, "", ".", "..", "path/to/dir/", r"path\to\dir\", Path(), Path(".."), ] + "outfile", + [123, "", ".", "..", "path/to/dir/", "path\\to\\dir\\", Path(), Path("..")], ) def test_build_arg_list_invalid_output(outfile): """