Skip to content

build_arg_list: Raise an exception if an invalid output file name is given #3336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 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,18 @@ 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\\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