Improve test coverage (#390)

- add test for tldr and cheatsh
- run all possible test cases
This commit is contained in:
Denis Isidoro 2020-08-28 09:58:35 -03:00 committed by GitHub
parent 84273c129c
commit d64ec34912
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 18 deletions

View file

@ -52,8 +52,11 @@ jobs:
- name: Install fzf
run: git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf; yes | ~/.fzf/install;
- name: Install tealdeer
run: sudo npm install -g tldr
- name: Run bash tests
run: ./tests/run 'trivial\|multiple'
run: ./tests/run
lints:
name: Lints

View file

@ -67,7 +67,6 @@ impl Fetcher for Foo {
fn fetch(&self, stdin: &mut std::process::ChildStdin, writer: &mut dyn Writer) -> Result<Option<VariableMap>, Error> {
eprintln!("TODO!!!!");
let cheat = fetch(&self.query)?;
dbg!(&cheat);
read_all(&self.query, &cheat, stdin, writer)
}
}

View file

@ -29,8 +29,8 @@ fn gen_core_finder_opts(config: &Config) -> Result<FinderOpts, Error> {
autoselect: !config.get_no_autoselect(),
overrides: config.fzf_overrides.clone(),
suggestion_type: SuggestionType::SnippetSelection,
query: if config.get_single() { None } else { config.get_query() },
filter: if config.get_single() { config.get_query() } else { None },
query: if config.get_best_match() { None } else { config.get_query() },
filter: if config.get_best_match() { config.get_query() } else { None },
..Default::default()
};
@ -166,7 +166,7 @@ pub fn main(config: Config) -> Result<(), Error> {
})
.context("Failed getting selection and variables from finder")?;
let (key, tags, snippet) = extract_from_selections(&raw_selection[..], config.get_single())?;
let (key, tags, snippet) = extract_from_selections(&raw_selection[..], config.get_best_match())?;
let interpolated_snippet = display::with_new_lines(
replace_variables_from_snippet(snippet, tags, variables.expect("No variables received from finder"), &config)

View file

@ -52,7 +52,7 @@ pub struct Config {
/// Returns the best match
#[structopt(long)]
single: bool,
best_match: bool,
/// Search for cheatsheets using the tldr-pages repository
#[structopt(long)]
@ -207,16 +207,30 @@ impl Config {
deprecated("best <query>");
Some(query.clone())
}
_ => self.query.clone(),
_ => {
let q = self.query.clone();
if q.is_some() {
return q;
}
if self.get_best_match() {
match self.source() {
Source::TLDR(q) => Some(q),
Source::CHEATSH(q) => Some(q),
_ => Some(String::from("")),
}
} else {
None
}
}
}
}
pub fn get_single(&self) -> bool {
pub fn get_best_match(&self) -> bool {
if let Some(Command::Best { .. }) = &self.cmd {
deprecated("best <query>");
true
} else {
self.single
self.best_match
}
}

View file

@ -31,8 +31,8 @@ echo "lorem <multiword> ipsum"
# variable dependency, full -> "2 12 a 2"
echo "<x> <x2> <y> <x>"
# variable dependency, we can ignore intermediate values -> "12"
: <x>; echo "<x2>"
; # variable dependency, we can ignore intermediate values -> "foo 12"
; printf "foo "; : <x>; echo "<x2>"
# nested unused value -> "path: /my/pictures"
echo "path: <pictures_folder>"

View file

@ -8,19 +8,23 @@ TEST_CHEAT_PATH="${NAVI_HOME}/tests/no_prompt_cheats"
_navi() {
stty sane || true
local -r navi="./target/debug/navi"
local filter="$*"
local filter="${filter::-2}"
RUST_BACKTRACE=1 NAVI_PATH="$TEST_CHEAT_PATH" "$navi" best "$filter"
RUST_BACKTRACE=1 NAVI_PATH="$TEST_CHEAT_PATH" "$navi" "$@"
}
_navi_test() {
_navi "$1" \
_navi_cases() {
local filter="$*"
filter="${filter::-2}"
_navi --query "$filter" --best-match
}
_navi_cases_test() {
_navi_cases "$1" \
| test::equals "$2"
}
_get_all_tests() {
cat "${TEST_CHEAT_PATH}/cases.cheat" \
| grep '#' \
| grep '^#' \
| grep ' ->' \
| sed 's/\\n/ /g' \
| sed -E 's/# (.*) -> "(.*)"/\1|\2/g'
@ -52,7 +56,23 @@ for i in $(_get_tests "$filter"); do
IFS="$ifs"
query="$(echo "$i" | cut -d'|' -f1)"
expected="$(echo "$i" | cut -d'|' -f2)"
test::run "$query" _navi_test "$query" "$expected"
test::run "$query" _navi_cases_test "$query" "$expected"
done
_navi_tldr() {
_navi --tldr docker --query ps --print --best-match \
| test::equals "docker ps"
}
_navi_cheatsh() {
_navi --cheatsh docker --query remove --print --best-match \
| test::equals "docker rm container_name"
}
test::set_suite "tldr"
test::run "$query" _navi_tldr
test::set_suite "cheatsh"
test::run "$query" _navi_cheatsh
test::finish