-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(env): complete commands and variable assignments
Refs #1111 Co-authored-by: Koichi Murase <[email protected]>
- Loading branch information
1 parent
a3e8a5e
commit 5c75fa3
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# bash completion for env(1) -*- shell-script -*- | ||
|
||
_comp_cmd_env() | ||
{ | ||
local cur prev words cword was_split comp_args | ||
_comp_initialize -s -- "$@" || return | ||
|
||
local i noargopts='!(-*|*[uCS]*)' | ||
for ((i = 1; i <= cword; i++)); do | ||
if [[ ${words[i]} != -* && ${words[i]} != *=* ]]; then | ||
_comp_command_offset $i | ||
return | ||
fi | ||
# shellcheck disable=SC2254 | ||
[[ ${words[i]} == @(--@(unset|chdir|split-string)|-${noargopts}[uCS]) ]] && | ||
((i++)) | ||
done | ||
|
||
# shellcheck disable=SC2254 | ||
case "$prev" in | ||
--unset | -${noargopts}u) | ||
_comp_compgen -- -A variable | ||
return | ||
;; | ||
--chdir | -${noargopts}C) | ||
_comp_compgen_filedir -d | ||
return | ||
;; | ||
--split-string | -${noargopts}S) | ||
return | ||
;; | ||
--block-signal | --default-signal | --ignore-signal) | ||
# TODO signals, but only if completing with a =SIG | ||
;; | ||
esac | ||
|
||
[[ $was_split ]] && return | ||
|
||
_comp_variable_assignments "$cur" && return | ||
|
||
if [[ $cur == -* ]]; then | ||
_comp_compgen_help || _comp_compgen_usage | ||
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace | ||
return | ||
fi | ||
} && | ||
complete -F _comp_cmd_env env | ||
|
||
# ex: filetype=sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
import pytest | ||
|
||
from conftest import assert_complete | ||
|
||
|
||
class TestEnv: | ||
@pytest.mark.complete("env --", require_longopt=True) | ||
def test_1(self, completion): | ||
assert completion | ||
|
||
@pytest.mark.complete("env __unknown_variable__=") | ||
def test_unknown_variable_falls_back_to_filedir(self, completion): | ||
assert "shared/" in completion | ||
|
||
@pytest.mark.complete("env LANG=", xfail="! locale -a &>/dev/null") | ||
def test_lang_envvar(self, completion): | ||
assert any(x == "C" or x.startswith("C.") for x in completion) | ||
|
||
@pytest.mark.parametrize( | ||
"opts", | ||
[ | ||
"", | ||
"foo=bar", | ||
"--debug", | ||
"foo=bar --debug", | ||
"--debug foo=bar", | ||
], | ||
) | ||
def test_command(self, bash, opts): | ||
completion = assert_complete(bash, "env %s s" % opts) | ||
assert completion == "h" or "sh" in completion |