Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions pygmt/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pygmt.helpers import (
GMTTempFile,
args_in_kwargs,
build_arg_list,
data_kind,
kwargs_to_strings,
unique_name,
Expand Down Expand Up @@ -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.
Expand Down