Skip to content

Commit 419e931

Browse files
seismanweiji14
andauthored
build_arg_list: Raise an exception if an invalid output file name is given (#3336)
Co-authored-by: Wei Ji <[email protected]>
1 parent 318a8c4 commit 419e931

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pygmt/helpers/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,13 @@ def build_arg_list(
350350
gmt_args = [str(infile), *gmt_args]
351351
else:
352352
gmt_args = [str(_file) for _file in infile] + gmt_args
353-
if outfile:
353+
if outfile is not None:
354+
if (
355+
not isinstance(outfile, str | pathlib.PurePath)
356+
or str(outfile) in {"", ".", ".."}
357+
or str(outfile).endswith(("/", "\\"))
358+
):
359+
raise GMTInvalidInput(f"Invalid output file name '{outfile}'.")
354360
gmt_args.append(f"->{outfile}")
355361
return gmt_args
356362

pygmt/tests/test_helpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pygmt.helpers import (
1313
GMTTempFile,
1414
args_in_kwargs,
15+
build_arg_list,
1516
data_kind,
1617
kwargs_to_strings,
1718
unique_name,
@@ -137,6 +138,18 @@ def test_gmttempfile_read():
137138
assert tmpfile.read(keep_tabs=True) == "in.dat: N = 2\t<1/3>\t<2/4>\n"
138139

139140

141+
@pytest.mark.parametrize(
142+
"outfile",
143+
[123, "", ".", "..", "path/to/dir/", "path\\to\\dir\\", Path(), Path("..")],
144+
)
145+
def test_build_arg_list_invalid_output(outfile):
146+
"""
147+
Test that build_arg_list raises an exception when output file name is invalid.
148+
"""
149+
with pytest.raises(GMTInvalidInput):
150+
build_arg_list({}, outfile=outfile)
151+
152+
140153
def test_args_in_kwargs():
141154
"""
142155
Test that args_in_kwargs function returns correct Boolean responses.

0 commit comments

Comments
 (0)