zshが完了した自動化されたテストを書くには?

zshが完了した自動化されたテストを書くには?

zsh完了機能が自動的に生成されるプロジェクトがあります。作業中にいくつかの極端なケースとバグを見つけ、それを書き留め、変更があるたびに再テストしました。明らかに適切なテストスイートを作成したいのですが、方法がわかりません。

Bashを完了するためのテストは非常に簡単です。COMP_*変数を設定して関数を実行し、COMP_REPLYzshに対して同様のことをしたいと思います。

私はできるだけ最善を尽くしてcompsysのドキュメントを読んでいますが、解決策は見えません。

コンテキストを手動で設定し、完成を実行してから一連の説明などを見たいと思います。

完成度をテストする方法を見つけた人はいますか?

ベストアンサー1

Zshでは、テストの完了はもう少し複雑です。これは、Zshの補完コマンドが補完ウィジェット内でのみ実行でき、Zsh行エディタがアクティブなときにのみ呼び出される可能性があるためです。スクリプト内で完成を完了するには、呼び出されるものを使用する必要があります。擬似端末ここでは、完成ウィジェットを有効にするためのアクティブなコマンドラインを持つことができます。

# Set up your completions as you would normally.
compdef _my-command my-command
_my-command () {
        _arguments '--help[display help text]'  # Just an example.
}

# Define our test function.
comptest () {
        # Add markup to make the output easier to parse.
        zstyle ':completion:*:default' list-colors \
                'no=<COMPLETION>' 'lc=' 'rc=' 'ec=</COMPLETION>'
        zstyle ':completion:*' group-name ''
        zstyle ':completion:*:messages' format \
                '<MESSAGE>%d</MESSAGE>'
        zstyle ':completion:*:descriptions' format \
                '<HEADER>%d</HEADER>'

        # Bind a custom widget to TAB.
        bindkey '^I' complete-word
        zle -C {,,}complete-word
        complete-word () {
                # Make the completion system believe we're on a 
                # normal command line, not in vared.
                unset 'compstate[vared]'

                # Add a delimiter before and after the completions.
                # Use of ^B and ^C as delimiters here is arbitrary.
                # Just use something that won't normally be printed.
                compadd -x $'\C-B'
                _main_complete "$@"
                compadd -J -last- -x $'\C-C'

                exit
        }

        vared -c tmp
}

zmodload zsh/zpty  # Load the pseudo terminal module.
zpty {,}comptest   # Create a new pty and run our function in it.

# Simulate a command being typed, ending with TAB to get completions.
zpty -w comptest $'my-command --h\t'

# Read up to the first delimiter. Discard all of this.
zpty -r comptest REPLY $'*\C-B'

zpty -r comptest REPLY $'*\C-C'  # Read up to the second delimiter.

# Print out the results.
print -r -- "${REPLY%$'\C-C'}"   # Trim off the ^C, just in case.

zpty -d comptest  # Delete the pty.

上記の例を実行すると、次のものが印刷されます。


<HEADER>option</HEADER>
<COMPLETION>--help    display help text</COMPLETION>

完全な完了出力をテストせずにコマンドラインに挿入される文字列のみをテストするには、以下を参照してください。https://stackoverflow.com/questions/65386043/unit-testing-zsh-completion-script/69164362#69164362

おすすめ記事