Skip to content

Commit

Permalink
test(command): add integration tests (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Mar 22, 2021
1 parent e976378 commit 37b07e9
Show file tree
Hide file tree
Showing 32 changed files with 881 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -25,11 +25,11 @@ jobs:
deno-version: ${{ matrix.deno }}

- name: Run tests
run: deno test --allow-env --unstable
run: deno test --allow-all --unstable
if: ${{ matrix.deno != 'v1.x' }}

- name: Run tests
run: deno test --coverage=./cov --allow-env --unstable
run: deno test --coverage=./cov --allow-all --unstable
if: ${{ matrix.deno == 'v1.x' }}

- name: Generate lcov
Expand Down
4 changes: 2 additions & 2 deletions command/command.ts
Expand Up @@ -1334,12 +1334,12 @@ export class Command<
}

/** Output generated help without exiting. */
public showVersion() {
public showVersion(): void {
Deno.stdout.writeSync(new TextEncoder().encode(this.getVersion()));
}

/** Output generated help without exiting. */
public showHelp() {
public showHelp(): void {
Deno.stdout.writeSync(new TextEncoder().encode(this.getHelp()));
}

Expand Down
12 changes: 6 additions & 6 deletions command/completions/_bash_completions_generator.ts
Expand Up @@ -28,7 +28,7 @@ _${replaceSpecialChars(path)}() {
prev="\${COMP_WORDS[COMP_CWORD-1]}"
cmd="_"
opts=()
_${replaceSpecialChars(this.cmd.getName())}_complete() {
local action="$1"; shift
mapfile -t values < <( ${this.cmd.getName()} completions complete "\${action}" "\${@}" )
Expand All @@ -38,18 +38,18 @@ _${replaceSpecialChars(path)}() {
}
${this.generateCompletions(this.cmd).trim()}
for word in "\${COMP_WORDS[@]}"; do
case "\${word}" in
-*) ;;
*)
*)
cmd_tmp="\${cmd}_\${word//[^[:alnum:]]/_}"
if type "\${cmd_tmp}" &>/dev/null; then
cmd="\${cmd_tmp}"
fi
esac
done
\${cmd}
if [[ \${#opts[@]} -eq 0 ]]; then
Expand All @@ -70,7 +70,7 @@ _${replaceSpecialChars(path)}() {
# shellcheck disable=SC2207
COMPREPLY=($(printf '%q\\n' "\${result[@]}"))
fi
return 0
}
Expand Down Expand Up @@ -148,7 +148,7 @@ ${childCommandCompletions}`;
opts += 'case "${prev}" in';
for (const option of options) {
const flags: string = option.flags
.map((flag) => flag.trim())
.map((flag: string) => flag.trim())
.join("|");

const completionsCmd: string = this.generateOptionCompletionsCommand(
Expand Down
25 changes: 25 additions & 0 deletions command/test/integration/command.ts
@@ -0,0 +1,25 @@
import { Command, CompletionsCommand, HelpCommand } from "../../mod.ts";

const cmd = new Command()
.version("1.0.0")
.name("completions-test")
.description("Completions test.")
.globalOption("-g, --global <val:boolean>", "Foo option.")
.option("-m, --main <val:boolean>", "Bar option.")
.default("help")
// foo
.command(
"foo",
new Command()
.description("Foo command.")
.option("-f, --foo", "Foo option.")
// bar
.command("bar", "Bar command.")
.option("-b, --bar", "Bar option."),
)
.command("help", new HelpCommand().global())
.command("completions", new CompletionsCommand());

if (import.meta.main) {
await cmd.parse();
}
@@ -0,0 +1 @@
completions complete boolean
@@ -0,0 +1,2 @@
true
false
@@ -0,0 +1 @@
completions complete boolean foo
@@ -0,0 +1,2 @@
true
false
@@ -0,0 +1 @@
completions complete boolean foo bar
@@ -0,0 +1,2 @@
true
false
@@ -0,0 +1 @@
completions complete command help
@@ -0,0 +1,3 @@
foo
help
completions
@@ -0,0 +1 @@
completions bash
228 changes: 228 additions & 0 deletions command/test/integration/fixtures/completions_generate_bash.out
@@ -0,0 +1,228 @@
#!/usr/bin/env bash
# bash completion support for completions-test v1.0.0

_completions_test() {
local word cur prev
local -a opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd="_"
opts=()

_completions_test_complete() {
local action="$1"; shift
mapfile -t values < <( completions-test completions complete "${action}" "${@}" )
for i in "${values[@]}"; do
opts+=("$i")
done
}

__completions_test() {
opts=(-h --help -V --version -g --global -m --main foo help completions)

if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-V|--version) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean ;;
-m|--main) opts=(); _completions_test_complete boolean ;;
esac
}

__completions_test_foo() {
opts=(-h --help -g --global -f --foo help bar)

if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean foo ;;
-f|--foo) ;;
esac
}

__completions_test_foo_help() {
opts=(-h --help -g --global)
_completions_test_complete command foo help
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean foo help ;;
esac
}

__completions_test_foo_bar() {
opts=(-h --help -g --global -b --bar help)

if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean foo bar ;;
-b|--bar) ;;
esac
}

__completions_test_foo_bar_help() {
opts=(-h --help -g --global)
_completions_test_complete command foo bar help
if [[ ${cur} == -* || ${COMP_CWORD} -eq 4 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean foo bar help ;;
esac
}

__completions_test_help() {
opts=(-h --help -g --global)
_completions_test_complete command help
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean help ;;
esac
}

__completions_test_completions() {
opts=(-h --help -g --global help bash fish zsh)

if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions ;;
esac
}

__completions_test_completions_help() {
opts=(-h --help -g --global)
_completions_test_complete command completions help
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions help ;;
esac
}

__completions_test_completions_bash() {
opts=(-h --help -g --global help)

if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions bash ;;
esac
}

__completions_test_completions_bash_help() {
opts=(-h --help -g --global)
_completions_test_complete command completions bash help
if [[ ${cur} == -* || ${COMP_CWORD} -eq 4 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions bash help ;;
esac
}

__completions_test_completions_fish() {
opts=(-h --help -g --global help)

if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions fish ;;
esac
}

__completions_test_completions_fish_help() {
opts=(-h --help -g --global)
_completions_test_complete command completions fish help
if [[ ${cur} == -* || ${COMP_CWORD} -eq 4 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions fish help ;;
esac
}

__completions_test_completions_zsh() {
opts=(-h --help -g --global help)

if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions zsh ;;
esac
}

__completions_test_completions_zsh_help() {
opts=(-h --help -g --global)
_completions_test_complete command completions zsh help
if [[ ${cur} == -* || ${COMP_CWORD} -eq 4 ]] ; then
return 0
fi
case "${prev}" in
-h|--help) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean completions zsh help ;;
esac
}

for word in "${COMP_WORDS[@]}"; do
case "${word}" in
-*) ;;
*)
cmd_tmp="${cmd}_${word//[^[:alnum:]]/_}"
if type "${cmd_tmp}" &>/dev/null; then
cmd="${cmd_tmp}"
fi
esac
done

${cmd}

if [[ ${#opts[@]} -eq 0 ]]; then
# shellcheck disable=SC2207
COMPREPLY=($(compgen -f "${cur}"))
return 0
fi

local values
values="$( printf "\n%s" "${opts[@]}" )"
local IFS=$'\n'
# shellcheck disable=SC2207
local result=($(compgen -W "${values[@]}" -- "${cur}"))
if [[ ${#result[@]} -eq 0 ]]; then
# shellcheck disable=SC2207
COMPREPLY=($(compgen -f "${cur}"))
else
# shellcheck disable=SC2207
COMPREPLY=($(printf '%q\n' "${result[@]}"))
fi

return 0
}

complete -F _completions_test -o bashdefault -o default completions-test
@@ -0,0 +1 @@
completions fish

0 comments on commit 37b07e9

Please sign in to comment.