/test/lib/git/repo_index_test.sh

https://github.com/ndbroadbent/scm_breeze · Shell · 212 lines · 147 code · 31 blank · 34 comment · 10 complexity · 036c1052205a89ee8faab806d1b59ac0 MD5 · raw file

  1. #!/bin/bash
  2. # ------------------------------------------------------------------------------
  3. # SCM Breeze - Streamline your SCM workflow.
  4. # Copyright 2011 Nathan Broadbent (http://madebynathan.com). All Rights Reserved.
  5. # Released under the LGPL (GNU Lesser General Public License)
  6. # ------------------------------------------------------------------------------
  7. #
  8. # Unit tests for git shell scripts
  9. export scmbDir="$(cd -P "$(dirname "$0")" && pwd)/../../.."
  10. # Zsh compatibility
  11. if [ -n "${ZSH_VERSION:-}" ]; then
  12. shell="zsh"
  13. SHUNIT_PARENT=$0
  14. setopt shwordsplit
  15. fi
  16. # Load test helpers
  17. source "$scmbDir/test/support/test_helper.sh"
  18. # Load functions to test
  19. source "$scmbDir/lib/scm_breeze.sh"
  20. source "$scmbDir/lib/git/repo_index.sh"
  21. # Setup and tear down
  22. #-----------------------------------------------------------------------------
  23. oneTimeSetUp() {
  24. GIT_REPO_DIR=$(mktemp -d -t scm_breeze.XXXXXXXXXX)
  25. GIT_REPOS="/tmp/test_repo_1:/tmp/test_repo_11"
  26. git_status_command="git status"
  27. git_index_file="$GIT_REPO_DIR/.git_index"
  28. silentGitCommands
  29. cd $GIT_REPO_DIR
  30. # Setup test repos in temp repo dir
  31. for repo in github bitbucket source_forge TestCaps; do
  32. mkdir $repo
  33. cd $repo
  34. git init
  35. cd - >/dev/null
  36. done
  37. # Add some nested dirs for testing resursive tab completion
  38. mkdir -p github/videos/octocat/live_action
  39. # Add hidden dir to test that '.git' is filtered, but other hidden dirs are available.
  40. mkdir -p github/.im_hidden
  41. # Setup a test repo with some submodules
  42. # (just a dummy '.gitmodules' file and some nested .git directories)
  43. mkdir submodules_everywhere
  44. cd submodules_everywhere
  45. git init
  46. cat >.gitmodules <<EOF
  47. [submodule "very/nested/directory/red_submodule"]
  48. [submodule "very/nested/directory/green_submodule"]
  49. [submodule "very/nested/directory/blue_submodule"]
  50. EOF
  51. mkdir -p "very/nested/directory"
  52. cd "very/nested/directory"
  53. for repo in red_submodule green_submodule blue_submodule; do
  54. mkdir $repo
  55. cd $repo
  56. git init
  57. cd - >/dev/null
  58. done
  59. # Setup some custom repos outside the main repo dir
  60. local IFS=":"
  61. for dir in $GIT_REPOS; do
  62. mkdir -p $dir
  63. cd $dir
  64. git init
  65. done
  66. verboseGitCommands
  67. cd "$orig_cwd"
  68. }
  69. oneTimeTearDown() {
  70. rm -rf "${GIT_REPO_DIR}"
  71. local IFS=":"
  72. for dir in $GIT_REPOS; do rm -rf $dir; done
  73. }
  74. ensureIndex() {
  75. _check_git_index
  76. }
  77. index_no_newlines() {
  78. tr "\\n" " " < $git_index_file
  79. }
  80. #-----------------------------------------------------------------------------
  81. # Unit tests
  82. #-----------------------------------------------------------------------------
  83. test_repo_index_command() {
  84. git_index --rebuild >/dev/null
  85. # Test that all repos are detected, and sorted alphabetically
  86. assertIncludes "$(index_no_newlines)" $(
  87. cat <<EXPECT | sort -t "." -k1,1 | tr -d '\n' | awk '{print ".*"$1}'
  88. bitbucket.*
  89. blue_submodule.*
  90. github.*
  91. green_submodule.*
  92. red_submodule.*
  93. source_forge.*
  94. submodules_everywhere.*
  95. test_repo_11.*
  96. test_repo_1.*
  97. EXPECT
  98. )
  99. }
  100. test_check_git_index() {
  101. ensureIndex
  102. echo "should not be regenerated" >>$git_index_file
  103. _check_git_index
  104. # Test that index is not rebuilt unless empty
  105. assertIncludes "$(index_no_newlines)" "should not be regenerated"
  106. rm $git_index_file
  107. # Test the index is rebuilt
  108. _check_git_index
  109. assertTrue "[ -f $git_index_file ]"
  110. }
  111. test_git_index_count() {
  112. assertEquals "10" "$(_git_index_count)"
  113. }
  114. test_repo_list() {
  115. ensureIndex
  116. list=$(git_index --list)
  117. assertIncludes "$list" "bitbucket" || return
  118. assertIncludes "$list" "blue_submodule" || return
  119. assertIncludes "$list" "test_repo_11"
  120. }
  121. # Test matching rules for changing directory
  122. test_git_index_changing_directory() {
  123. ensureIndex
  124. git_index "github"
  125. assertEquals "$GIT_REPO_DIR/github" "$PWD"
  126. git_index "github/"
  127. assertEquals "$GIT_REPO_DIR/github" "$PWD"
  128. git_index "bucket"
  129. assertEquals "$GIT_REPO_DIR/bitbucket" "$PWD"
  130. git_index "testcaps"
  131. assertEquals "$GIT_REPO_DIR/TestCaps" "$PWD"
  132. git_index "green_sub"
  133. assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/green_submodule" "$PWD"
  134. git_index "_submod"
  135. assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/blue_submodule" "$PWD"
  136. git_index "test_repo_1"
  137. assertEquals "/tmp/test_repo_1" "$PWD"
  138. git_index "test_repo_11"
  139. assertEquals "/tmp/test_repo_11" "$PWD"
  140. git_index "test_repo_"
  141. assertEquals "/tmp/test_repo_1" "$PWD"
  142. git_index "github/videos/octocat/live_action"
  143. assertEquals "$GIT_REPO_DIR/github/videos/octocat/live_action" "$PWD"
  144. }
  145. test_git_index_tab_completion() {
  146. # Only run tab completion test for bash
  147. if [[ "$0" == *bash ]]; then
  148. ensureIndex
  149. COMP_CWORD=0
  150. # Test that '--' commands have tab completion
  151. COMP_WORDS="--"
  152. _git_index_tab_completion
  153. assertEquals "Incorrect number of tab-completed '--' commands" "5" "$(tab_completions | wc -w)"
  154. COMP_WORDS="gith"
  155. _git_index_tab_completion
  156. assertIncludes "$(tab_completions)" "github/"
  157. # Test completion for project sub-directories when project ends with '/'
  158. COMP_WORDS="github/"
  159. _git_index_tab_completion
  160. assertIncludes "$(tab_completions)" "github/videos/"
  161. # Check that '.git/' is filtered from completion, but other hidden dirs are available
  162. assertNotIncludes "$(tab_completions)" "github/.git/"
  163. assertIncludes "$(tab_completions)" "github/.im_hidden/"
  164. COMP_WORDS="github/videos/"
  165. _git_index_tab_completion
  166. assertIncludes "$(tab_completions)" "github/videos/octocat/"
  167. # Test that completion checks for other matching projects even if one matches perfectly
  168. COMP_WORDS="test_repo_1"
  169. _git_index_tab_completion
  170. assertIncludes "$(tab_completions)" "test_repo_1/ test_repo_11/"
  171. fi
  172. }
  173. # Test changing to top-level directory (when arg begins with '/')
  174. test_changing_to_top_level_directory() {
  175. mkdir "$GIT_REPO_DIR/gems"
  176. git_index "/gems"
  177. assertEquals "$GIT_REPO_DIR/gems" "$PWD"
  178. }
  179. # load and run shUnit2
  180. # Call this function to run tests
  181. source "$scmbDir/test/support/shunit2"