diff --git a/scripts/finit b/scripts/finit index 00bf5f9..cabe6f3 100755 --- a/scripts/finit +++ b/scripts/finit @@ -18,7 +18,7 @@ source "${mydir}"/../helper/set_variable.sh source "${mydir}"/../helper/get_confirmation.sh function usage() { - echo -e "Usage: dotbare finit [-h] ...\n" + echo -e "Usage: dotbare finit [-h] [-u] ...\n" echo -e "Init the git bare repository if doesn't exist" # shellcheck disable=SC2016 echo -e 'The bare repository will be initialised under ${DOTBARE_DIR}, default to $HOME/.cfg if not set' @@ -52,11 +52,12 @@ if [[ -z "${remote_url}" ]]; then echo "modify DOTBARE_DIR and DOTBARE_TREE to customize location, more information run dotbare finit -h" echo "git bare repository will be initialised at ${DOTBARE_DIR}" echo "git bare repository will be tracking ${DOTBARE_TREE}" - confirm=$(get_confirmation) + [[ -z "${confirm}" ]] && confirm=$(get_confirmation) [[ "${confirm}" != 'y' ]] && exit 1 if [[ -d "${DOTBARE_DIR}" ]]; then echo "${DOTBARE_DIR} already exist" + exit 1 else git init --bare "${DOTBARE_DIR}" /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \ diff --git a/tests/finit.bats b/tests/finit.bats new file mode 100755 index 0000000..1dee4d8 --- /dev/null +++ b/tests/finit.bats @@ -0,0 +1,42 @@ +#!/usr/bin/env bats + +setup() { + export confirm='y' + export DOTBARE_DIR="$HOME/.local/share/dotbare_test/.cfg" + export DOTBARE_TREE="$HOME/.local/share/dotbare_test" +} + +teardown() { + unset confirm +} + +help() { + bash "${BATS_TEST_DIRNAME}"/../dotbare finit -h +} + +init() { + bash "${BATS_TEST_DIRNAME}"/../dotbare finit +} + +migration() { + bash "${BATS_TEST_DIRNAME}"/../dotbare finit -u https://github.com/kazhala/dotfiles.git +} + +@test "finit help" { + run help + [ "${status}" -eq 0 ] + [ "${lines[0]}" = "Usage: dotbare finit [-h] [-u] ..." ] +} + +@test "init dotbare" { + run init + [ "${status}" -eq 0 ] + run init + [ "${status}" -eq 1 ] + [ "${lines[3]}" = "${DOTBARE_DIR} already exist" ] +} + +@test "migration" { + run migration + [ "${status}" -eq 0 ] +}