test(fcheckout): improve test coverage with mocking

pull/13/head
kevin zhuang 4 years ago
parent e220c66fd4
commit b5ff22ed6d

@ -92,7 +92,7 @@ function get_git_file() {
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
ls-files --full-name --directory "${DOTBARE_TREE}" \
| fzf --header="${header}" \
--preview "head -50 ${DOTBARE_TREE}/{}" \
--preview "cat ${DOTBARE_TREE}/{}" \
| awk -v home="${DOTBARE_TREE}" -v print_opt="${print_opt}" '{
if (print_opt == "full") {
print home "/" $0

@ -4,7 +4,7 @@
#
# @params
# Globals
# ${DOTBARE_DIR}: location of the bare repo
# ${DOTBARE_DIR}: location of the bare repo
# ${DOTBARE_TREE}: which folder is the bare repo tracking
# ${DOTBARE_BACKUP}: backup directory for all tracked files
# ${DOTBARE_KEY}: defualt key bindings

@ -1,5 +1,9 @@
#!/usr/bin/env bats
setup() {
export PATH="${BATS_TEST_DIRNAME}:$PATH"
}
help() {
bash "${BATS_TEST_DIRNAME}"/../dotbare fcheckout -h
}
@ -8,14 +12,57 @@ invalid_option() {
bash "${BATS_TEST_DIRNAME}"/../dotbare fcheckout -p
}
checkout_branch() {
bash "${BATS_TEST_DIRNAME}"/../dotbare fcheckout --branch
}
checkout_commit() {
bash "${BATS_TEST_DIRNAME}"/../dotbare fcheckout -c
}
checkout_modified_file() {
bash "${BATS_TEST_DIRNAME}"/../dotbare fcheckout -y
}
checkout_selected_file() {
bash "${BATS_TEST_DIRNAME}"/../dotbare fcheckout --yes -s
}
@test "fcheckout help" {
run help
[ "${status}" -eq 0 ]
[ "${lines[0]}" = "Usage: dotbare fcheckout [-h] [-a] [-b] [-c] ..." ]
[ "${lines[0]}" = "Usage: dotbare fcheckout [-h] [-s] [-b] [-c] ..." ]
}
@test "fchekcout invalid option" {
run invalid_option
[ "${status}" -eq 1 ]
[ "${lines[0]}" = "Invalid option: p" ]
[ "${lines[0]}" = "Invalid option: -p" ]
}
@test "fchekcout branch" {
run checkout_branch
result=$(echo "${lines[0]}" | tr '`' "'")
[ "${status}" -eq 129 ]
[ "${result}" = "error: unknown option 'branch'" ]
}
@test "fchekcout commit" {
run checkout_commit
result=$(echo "${lines[0]}" | tr '`' "'")
[ "${status}" -eq 129 ]
[ "${result}" = "error: unknown option 'commitshow'" ]
}
@test "fcheckout modified" {
run checkout_modified_file
[ "${status}" -eq 1 ]
[ "${lines[0]}" = "error: pathspec '/Users/kevinzhuang/modifiedfile' did not match any file(s) known to git" ]
}
@test "fcheckout select" {
run checkout_selected_file
[ "${status}" -eq 1 ]
[ "${lines[0]}" = "error: pathspec 'commitdiff' did not match any file(s) known to git" ]
[ "${lines[1]}" = "error: pathspec '/Users/kevinzhuang/selectgitfile' did not match any file(s) known to git" ]
}

@ -6,4 +6,14 @@
# usage:
# export PATH="${BATS_TEST_DIRNAME}:$PATH"
echo "$@"
if [[ "$*" =~ "--header=Select a commit to checkout" ]] && [[ "$*" =~ "show --color" ]]; then
echo "--commitshow"
elif [[ "$*" =~ "--no-multi --header=Select a branch to checkout" ]]; then
echo "--branch"
elif [[ "$*" =~ "--header=select a file to checkout version in HEAD --preview" ]]; then
echo "-- modifiedfile"
elif [[ "$*" =~ '--header=select a file to checkout' ]] && [[ "$*" =~ "cat" ]]; then
echo "selectgitfile"
elif [[ "$*" =~ '--header=select the target commit' ]] && [[ "$*" =~ "diff --color" ]]; then
echo "commitdiff"
fi

Loading…
Cancel
Save