initial commit
[git-wtree.git] / test-git-wtree.sh
1 #!/bin/sh
2
3 . $(dirname $0)/git-wtree.sh
4
5 #set -e
6
7 SELF_PID=$$
8 SELF_ROOT=$(readlink -e $(dirname $0))
9 FIXED_FAKE_ROOT=/tmp/git-wtree-test.root
10 FAKE_ROOT=$([ -z "${GIT_WTREE_TEST_FIXED_ROOT}" ] && mktemp -d || (mkdir ${FIXED_FAKE_ROOT} && ${FIXED_FAKE_ROOT}))
11 trap cleanup  KILL QUIT EXIT
12
13 fail() {
14     echo -n "*** FAILED: "
15     [ $# -gt 0 ] && echo "$*" || echo "unknown reason"
16     kill -9 ${SELF_PID}
17 }
18
19 cleanup() {
20     cd "${SELF_ROOT}"
21     [ -d "${FAKE_ROOT}" ] && (echo "- Clean the fake root ${FAKE_ROOT} up"; rm -rf "${FAKE_ROOT}")
22 }
23
24 echo "- Fake root: ${FAKE_ROOT}"
25 [ -n "${FAKE_ROOT}" -a -d "${FAKE_ROOT}" ] || fail "fake root ${FAKE_ROOT} location is set"
26
27 echo "- Prepare fake git repository"
28 mkdir ${FAKE_ROOT}/main.git || fail "fake root ${FAKE_ROOT} is created"
29
30 cd ${FAKE_ROOT}/main.git
31 git init . || fail "fake git repository is initialized"
32
33 touch dummy.file || fail "dummy file in the git repository is created"
34 git add dummy.file || fail "dummy file is added to the git repository"
35 git commit -m "initial auto commit" || fail "initial commit is made to the git repository"
36
37 echo "= TEST: Master is a single available worktree"
38 worktrees=$(git_wtree_cmd_ls | wc -l)
39 [ 1 -eq ${worktrees} ] || fail "master is an only one workspace"
40
41 echo "= TEST(new)"
42 echo "== requires --name argument"
43 git_wtree_cmd_new 2>&1 | grep -qi 'ERROR.*missing branch name'
44 [ 0 -eq $? ] || "error message about missing '--name' argument"
45
46 echo "== requires --dir argument"
47 git_wtree_cmd_new --name branch-name 2>&1 | grep -qi 'ERROR.*missing directory name'
48 [ 0 -eq $? ] || fail "error message about missing '--dir' argument"
49
50 echo "== requires 'worktree.root' in config"
51 git_wtree_cmd_new --name worktree --dir worktree.d 2>&1 | grep -qi "ERROR.*worktree.root.*should point"
52 [ 0 -eq $? ] || fail "error message about missing 'worktree.root' variable"
53
54 echo "- Set worktree.root"
55 git config --local worktree.root ${FAKE_ROOT} || fail "'worktree.root' is set to a config"
56 git config --local worktree.root | grep -qi "${FAKE_ROOT}"
57 [ 0 -eq $? ] || fail "'worktree.root' is available through config"
58
59 echo "== creates worktree"
60 git_wtree_cmd_new --name test-branch-name --dir test-branch.d || fail "new worktree is created"
61 git worktree list | grep -q 'test-branch.d.*test-branch-name'
62 [ 0 -eq $? ] || fail "newly created worktree is listed"
63
64 echo "== fails on a duplicated name"
65 git_wtree_cmd_new --name test-branch-name --dir alternative-branch.d 2>&1 | grep -q FAILED
66 [ 0 -eq $? ] || fail "error message about failed creation of already existing branch"
67
68 echo "= TEST(ls)"
69 echo "== Lists worktrees"
70 worktrees=$(git_wtree_cmd_ls | grep -E '^master|^test-branch-name' | wc -l)
71 [ 2 -eq ${worktrees} ] || fail "Master and the newly created worktree are listed: ${worktrees}"
72
73 echo "= TEST(cd)"
74 echo "== PWD is changed to worktree"
75 pwd | grep -qi ${FAKE_ROOT}/main.git
76 [ 0 -eq $? ] || fail "initial directory is main fake root"
77 git_wtree_cmd_tool_cd test-branch-name 2>&1
78 [ x"${FAKE_ROOT}/test-branch.d" = x"$(pwd)" ] || fail "current directory is test-branche's one"
79
80 echo "== PWD is changed to master"
81 pwd | grep -qi ${FAKE_ROOT}/test-branch.d
82 [ 0 -eq $? ] || fail "initial directory is worktree's one"
83 git_wtree_cmd_tool_cd master 2>&1
84 [ x"${FAKE_ROOT}/main.git" = x"$(pwd)" ] || fail "current directory is master's one"
85
86 echo "= TEST(drop)"
87 cd ${FAKE_ROOT}/main.git
88
89 echo "== requires --name argument"
90 git_wtree_cmd_drop 2>&1 | grep -qi "ERROR.*missing branch name"
91 [ 0 -eq $? ] || fail "error message about no candidates to drop"
92
93 echo "== drops worktree directory"
94 git_wtree_cmd_drop --name test-branch-name || fail "drop command is succeeded"
95 worktrees=$(git_wtree_cmd_ls | grep master | wc -l)
96 [ 1 -eq ${worktrees} ] || fail "only master branch is left"
97
98 echo "== fails on already dropped worktree directory"
99 for branch_name in test-branch-name never-existed-branch-name; do
100     git_wtree_cmd_drop --name ${branch_name} 2>&1 | grep -qi "ERROR.*0 candidates"
101     [ 0 -eq $? ] || fail "error message about no candidates to drop"
102 done
103