Skip to content
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

bash nounset mode fixes #143

Merged
merged 1 commit into from
Jul 28, 2023
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: 4 additions & 4 deletions shtab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
# set default values (called for the initial parser & any subparsers)
_set_parser_defaults() {
local subparsers_var="${prefix}_subparsers[@]"
sub_parsers=${!subparsers_var}
sub_parsers=${!subparsers_var-}

local current_option_strings_var="${prefix}_option_strings[@]"
current_option_strings=${!current_option_strings_var}
Expand All @@ -356,13 +356,13 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
current_action="${prefix}_$(_shtab_replace_nonword $1)"

local current_action_compgen_var=${current_action}_COMPGEN
current_action_compgen="${!current_action_compgen_var}"
current_action_compgen="${!current_action_compgen_var-}"

local current_action_choices_var="${current_action}_choices[@]"
current_action_choices="${!current_action_choices_var}"
current_action_choices="${!current_action_choices_var-}"

local current_action_nargs_var="${current_action}_nargs"
if [ -n "${!current_action_nargs_var}" ]; then
if [ -n "${!current_action_nargs_var-}" ]; then
current_action_nargs="${!current_action_nargs_var}"
else
current_action_nargs=1
Expand Down
10 changes: 5 additions & 5 deletions tests/test_shtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, init_script=""):
def test(self, cmd="1", failure_message=""):
"""Equivalent to `bash -c '{init}; [[ {cmd} ]]'`."""
init = self.init + "\n" if self.init else ""
proc = subprocess.Popen(["bash", "-o", "pipefail", "-ec", f"{init}[[ {cmd} ]]"])
proc = subprocess.Popen(["bash", "-o", "pipefail", "-euc", f"{init}[[ {cmd} ]]"])
stdout, stderr = proc.communicate()
assert (0 == proc.wait() and not stdout and not stderr), f"""\
{failure_message}
Expand All @@ -36,7 +36,7 @@ def compgen(self, compgen_cmd, word, expected_completions, failure_message=""):
)


@pytest.mark.parametrize("init,test", [("export FOO=1", '"$FOO" -eq 1'), ("", '-z "$FOO"')])
@pytest.mark.parametrize("init,test", [("export FOO=1", '"$FOO" -eq 1'), ("", '-z "${FOO-}"')])
def test_bash(init, test):
shell = Bash(init)
shell.test(test)
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_subparser_custom_complete(shell, caplog):
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "s", "sub")
shell.compgen('-W "$_shtab_test_pos_0_choices"', "s", "sub")
shell.test('"$($_shtab_test_sub_pos_0_COMPGEN o)" = "one"')
shell.test('-z "$_shtab_test_COMPGEN"')
shell.test('-z "${_shtab_test_COMPGEN-}"')

assert not caplog.record_tuples

Expand All @@ -217,7 +217,7 @@ def test_subparser_aliases(shell, caplog):
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "y", "ysub")
shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "y", "ysub")
shell.test('"$($_shtab_test_sub_pos_0_COMPGEN o)" = "one"')
shell.test('-z "$_shtab_test_COMPGEN"')
shell.test('-z "${_shtab_test_COMPGEN-}"')

assert not caplog.record_tuples

Expand All @@ -235,7 +235,7 @@ def test_subparser_colons(shell, caplog):
shell = Bash(completion)
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "s", "sub:cmd")
shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "s", "sub:cmd")
shell.test('-z "$_shtab_test_COMPGEN"')
shell.test('-z "${_shtab_test_COMPGEN-}"')

assert not caplog.record_tuples

Expand Down