という名前のファイルに bash-completion スクリプトがありasdf-completion
、次を含むとします。
_asdf() {
COMPREPLY=()
local cur prev
cur=$(_get_cword)
COMPREPLY=( $( compgen -W "one two three four five six" -- "$cur") )
return 0
}
complete -F _asdf asdf
これはシェル関数_asdf
を使用して、架空のasdf
コマンドの補完を提供します。適切な環境変数を (bash の man ページから) 設定すると、同じ結果が得られます。これは、COMPREPLY
変数への潜在的な展開の配置です。単体テストでそれを行う例を次に示します。
import subprocess
import unittest
class BashTestCase(unittest.TestCase):
def test_complete(self):
completion_file="asdf-completion"
partial_word="f"
cmd=["asdf", "other", "arguments", partial_word]
cmdline = ' '.join(cmd)
out = subprocess.Popen(['bash', '-i', '-c',
r'source {compfile}; COMP_LINE="{cmdline}" COMP_WORDS=({cmdline}) COMP_CWORD={cword} COMP_POINT={cmdlen} $(complete -p {cmd} | sed "s/.*-F \\([^ ]*\\) .*/\\1/") && echo ${{COMPREPLY[*]}}'.format(
compfile=completion_file, cmdline=cmdline, cmdlen=len(cmdline), cmd=cmd[0], cword=cmd.index(partial_word)
)],
stdout=subprocess.PIPE)
stdout, stderr = out.communicate()
self.assertEqual(stdout, "four five\n")
if (__name__=='__main__'):
unittest.main()
これは、 を使用するすべての補完で機能するはずですが、他の補完で-F
も機能する可能性があります。
使用する je4d のコメントexpect
は、より完全なテストに適しています。