Skip to content

Commit

Permalink
Test(completions): Add basic tests
Browse files Browse the repository at this point in the history
Check they parse in CI. Closes #24.

Signed-off-by: Paul Mabileau <paulmabileau@hotmail.fr>
  • Loading branch information
PaulDance committed May 25, 2024
1 parent c7906a1 commit 8287d07
Show file tree
Hide file tree
Showing 3 changed files with 434 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::process;

use cargo_test_macro::cargo_test;
use snapbox::cmd::Command;
use snapbox::data::DataFormat;
use snapbox::{Data, IntoData};

mod common;
use common::*;

fn validate_shell(shell: &str, run: bool) {
cargo_liner()
.args(["completions", shell])
.assert()
.success()
.stderr_eq_("...")
.stdout_eq_(
Data::read_from(
&snapbox::utils::current_dir!()
.join(format!("fixtures/completions/validate_{shell}.stdout")),
Some(DataFormat::Binary),
)
.raw(),
);

// Actually run the script to see what happens, but only if asked to and
// the given shell is available from the current environment.
if run {
if let Ok(status) = process::Command::new(shell).arg("--version").status() {
if status.success() {
Command::new(shell)
.arg(format!(
"tests/fixtures/completions/validate_{shell}.stdout"
))
.assert()
.success()
.stdout_eq_("".into_data().raw())
.stderr_eq_("".into_data().raw());
}
}
}
}

#[cargo_test]
fn validate_bash() {
validate_shell("bash", true);
}

#[cargo_test]
fn validate_zsh() {
// Running the script with Zsh does not work... while it works.
validate_shell("zsh", false);
}
194 changes: 194 additions & 0 deletions tests/fixtures/completions/validate_bash.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
_cargo-liner() {
local i cur prev opts cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""

for i in ${COMP_WORDS[@]}
do
case "${cmd},${i}" in
",$1")
cmd="cargo__liner"
;;
cargo__liner,completions)
cmd="cargo__liner__completions"
;;
cargo__liner,help)
cmd="cargo__liner__help"
;;
cargo__liner,import)
cmd="cargo__liner__import"
;;
cargo__liner,ship)
cmd="cargo__liner__ship"
;;
cargo__liner__help,completions)
cmd="cargo__liner__help__completions"
;;
cargo__liner__help,help)
cmd="cargo__liner__help__help"
;;
cargo__liner__help,import)
cmd="cargo__liner__help__import"
;;
cargo__liner__help,ship)
cmd="cargo__liner__help__ship"
;;
*)
;;
esac
done

case "${cmd}" in
cargo__liner)
opts="-v -q -h -V --verbose --quiet --color --help --version ship import completions help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--color)
COMPREPLY=($(compgen -W "auto always never" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__completions)
opts="-v -q -h --verbose --quiet --color --help bash elvish fish powershell zsh"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--color)
COMPREPLY=($(compgen -W "auto always never" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__help)
opts="ship import completions help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__help__completions)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__help__help)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__help__import)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__help__ship)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__import)
opts="-e -c -p -f -s -l -v -q -h --exact --compatible --patch --force --keep-self --keep-local --verbose --quiet --color --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--color)
COMPREPLY=($(compgen -W "auto always never" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
cargo__liner__ship)
opts="-n -s -c -f -v -q -h --no-self --only-self --skip-check --force --verbose --quiet --color --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--color)
COMPREPLY=($(compgen -W "auto always never" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
esac
}

if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
complete -F _cargo-liner -o nosort -o bashdefault -o default cargo-liner
else
complete -F _cargo-liner -o bashdefault -o default cargo-liner
fi
Loading

0 comments on commit 8287d07

Please sign in to comment.