PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/test-simple

https://bitbucket.org/gward/vcprompt/
Shell | 356 lines | 269 code | 73 blank | 14 comment | 20 complexity | fb91950bfd4e02adb9f1e3e194135100 MD5 | raw file
  1. #!/bin/sh
  2. # Simple tests that do not require any external tools: i.e.
  3. # these just setup little fake working copies and make sure
  4. # that vcprompt does the right thing in them.
  5. . ./common.sh
  6. test_no_vc()
  7. {
  8. cd $tmpdir
  9. mkdir novc && cd novc
  10. assert_vcprompt "no vc 1" ""
  11. assert_vcprompt "no vc 2" "" "%n:%b"
  12. }
  13. test_root()
  14. {
  15. cd /
  16. assert_vcprompt "root dir" ""
  17. }
  18. test_simple_cvs()
  19. {
  20. cd $tmpdir
  21. mkdir cvs && cd cvs
  22. mkdir CVS && touch CVS/Entries
  23. assert_vcprompt "cvs notag" "cvs:trunk" "%n:%b"
  24. echo "Nsometag" > CVS/Tag
  25. assert_vcprompt "cvs nobranch" "cvs:(unknown)" "%n:%b"
  26. echo "Tfoo" > CVS/Tag
  27. assert_vcprompt "cvs branch" "foo"
  28. mkdir build && cd build
  29. assert_vcprompt "cvs subdir 1" "foo"
  30. mkdir CVS && touch CVS/Entries
  31. echo "Tblah" > CVS/Tag
  32. assert_vcprompt "cvs subdir 2" "blah"
  33. }
  34. test_simple_git()
  35. {
  36. cd $tmpdir
  37. mkdir git && cd git
  38. mkdir .git
  39. assert_vcprompt "git broken 1" ""
  40. assert_vcprompt "git broken 2" "" "%n:%b"
  41. echo 3f786850e387550fdab836ed7e6dc881de23001b > .git/HEAD
  42. assert_vcprompt "git nobranch" "(unknown)"
  43. assert_vcprompt "git nobranch (show rev)" "(unknown):3f786850e387" "%b:%r"
  44. echo "ref: refs/heads/foo" > .git/HEAD
  45. assert_vcprompt "git branch" "git:foo" "%n:%b"
  46. mkdir -p .git/refs/heads
  47. echo ffca1632148005094dc0d491aa19f8ba7f68b81c > .git/refs/heads/foo
  48. assert_vcprompt "git branch and rev" "foo:ffca16321480" "%b:%r"
  49. mkdir subdir && cd subdir
  50. assert_vcprompt "git subdir" "foo"
  51. }
  52. test_simple_fossil()
  53. {
  54. cd $tmpdir
  55. mkdir fossil && cd fossil
  56. touch _FOSSIL_
  57. assert_vcprompt "fossil broken" "fossil:(unknown)" "%n:%b"
  58. rm _FOSSIL_
  59. assert_vcprompt "fossil norepo" "" "%n:%b"
  60. assert_vcprompt "fossil norepo" "" "%n:%b"
  61. touch .fslckout
  62. assert_vcprompt "fossil broken" "fossil:(unknown)" "%n:%b"
  63. }
  64. test_simple_hg()
  65. {
  66. cd $tmpdir
  67. mkdir hg && cd hg
  68. mkdir .hg
  69. # Mercurial 1.4 does not create .hg/branch by default
  70. assert_vcprompt "hg nobranch" "hg:default" "%n:%b"
  71. echo foo > .hg/branch
  72. assert_vcprompt "hg top" "hg:foo" "%n:%b"
  73. printf '0123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  74. > .hg/dirstate
  75. assert_vcprompt "hg nodeid" "hg:303132333435" "%n:%r"
  76. printf '0123456789abcdefghijABCDEFGHIJKLMNOPQRST' \
  77. > .hg/dirstate
  78. assert_vcprompt "hg merge nodeid" "hg:303132333435,414243444546" "%n:%r"
  79. printf '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' > .hg/dirstate
  80. printf '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' >> .hg/dirstate
  81. assert_vcprompt "hg initial nodeid" "hg:" "%n:%r"
  82. mkdir subdir
  83. cd subdir
  84. assert_vcprompt "hg subdir" "foo"
  85. }
  86. test_simple_hg_bookmarks ()
  87. {
  88. cd $tmpdir
  89. mkdir hg_bookmarks && cd hg_bookmarks
  90. mkdir .hg
  91. echo default > .hg/branch
  92. touch .hg/bookmarks.current
  93. assert_vcprompt "hg_bookmarks nobookmark" "hg:default" "%n:%b"
  94. /bin/echo -n blah > .hg/bookmarks.current
  95. assert_vcprompt "hg_bookmarks bookmark" "hg:blah" "%n:%b"
  96. }
  97. test_simple_hg_mq ()
  98. {
  99. cd $tmpdir
  100. mkdir hg_mq && cd hg_mq
  101. mkdir .hg
  102. echo foo > .hg/branch
  103. assert_vcprompt "hg_mq nomq" "hg:/foo" "%n:%r/%b"
  104. mkdir .hg/patches
  105. touch .hg/patches/status
  106. assert_vcprompt "hg_mq applied 0" "hg:/foo" "%n:%r/%b"
  107. printf '0123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  108. > .hg/dirstate
  109. echo '303132333435363738396162636465666768696a:bar' >> .hg/patches/status
  110. assert_vcprompt "hg_mq applied 1" "foo/bar" "%b/%p"
  111. printf 'a123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  112. > .hg/dirstate
  113. echo '613132333435363738396162636465666768696a:baz' >> .hg/patches/status
  114. echo > .hg/patches.queue # default queue
  115. assert_vcprompt "hg_mq applied 2" "foo/baz" "%b/%p"
  116. mkdir .hg/patches-foo
  117. printf 'a123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  118. > .hg/dirstate
  119. echo '613132333435363738396162636465666768696a:qux' >> .hg/patches-foo/status
  120. echo -e 'foo\npatches\n' > .hg/patches.queues
  121. echo 'foo' > .hg/patches.queue # named queue
  122. assert_vcprompt "hg_mq applied named mq" "foo/qux" "%b/%p"
  123. }
  124. test_simple_hg_revlog ()
  125. {
  126. cd $tmpdir
  127. mkdir hg_tip && cd hg_tip
  128. mkdir .hg .hg/store
  129. # not inlined
  130. (
  131. printf '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
  132. printf '0123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0'
  133. printf '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
  134. printf 'a123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0'
  135. ) > .hg/store/00changelog.i
  136. printf '0123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  137. > .hg/dirstate
  138. assert_vcprompt "hg_revlog notip" "hg:0" "%n:%r"
  139. printf 'a123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  140. > .hg/dirstate
  141. assert_vcprompt "hg_revlog tip" "hg:1" "%n:%r"
  142. # inlined
  143. (
  144. printf '\0\001\0\0\0\0\0\0\0\0\0\002\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
  145. printf '0123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0'
  146. printf '\0\0' # inlined data
  147. printf '\0\0\0\0\0\0\0\0\0\0\0\001\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
  148. printf 'a123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0'
  149. printf '\0' # inlined data
  150. ) > .hg/store/00changelog.i
  151. printf '0123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  152. > .hg/dirstate
  153. assert_vcprompt "hg_revlog inlined notip" "hg:0" "%n:%r"
  154. printf 'a123456789abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' \
  155. > .hg/dirstate
  156. assert_vcprompt "hg_revlog inlined tip" "hg:1" "%n:%r"
  157. }
  158. # custom format for .svn/entries (svn 1.4 .. 1.6)
  159. test_simple_svn()
  160. {
  161. cd $tmpdir
  162. mkdir simple_svn && cd simple_svn
  163. mkdir .svn
  164. assert_vcprompt "svn broken" "" "%n:%r"
  165. cat > .svn/entries <<EOF
  166. 10
  167. dir
  168. 123
  169. file:///whatever
  170. file:///whatever
  171. 2009-05-17T11:31:06.550081Z
  172. 123
  173. EOF
  174. assert_vcprompt "svn revision" "svn:123" "%n:%r"
  175. }
  176. # XML format for .svn/entries (svn < 1.4)
  177. test_xml_svn()
  178. {
  179. cd $tmpdir
  180. mkdir xml_svn && cd xml_svn
  181. mkdir .svn
  182. cat > .svn/entries <<EOF
  183. <?xml version="1.0" encoding="utf-8"?>
  184. <wc-entries
  185. xmlns="svn:">
  186. <entry
  187. committed-rev="14"
  188. name=""
  189. committed-date="2007-01-02T23:04:49.013976Z"
  190. url="svn+ssh://blah/home/svn/project/trunk"
  191. last-author="greg"
  192. kind="dir"
  193. uuid="8d4eacf1-9596-41ff-ba68-66ac5da389ce"
  194. url="svn+ssh://blah/home/svn/project"
  195. prop-time="2007-01-04T01:48:15.000000Z"
  196. revision="14"/>
  197. EOF
  198. assert_vcprompt "svn xml 1" "svn:14" "%n:%r"
  199. }
  200. test_truncated_svn()
  201. {
  202. cd $tmpdir
  203. mkdir truncated_svn && cd truncated_svn
  204. mkdir .svn
  205. cat > .svn/entries <<EOF
  206. 10
  207. EOF
  208. assert_vcprompt "svn truncated 1" "" "%n:%r"
  209. # Add one more line to the entries file: it's still truncated!
  210. echo "dir" >> .svn/entries
  211. assert_vcprompt "svn truncated 2" "" "%n:%r"
  212. }
  213. test_bad_dir()
  214. {
  215. cd $tmpdir
  216. set -e
  217. mkdir -p d1/d2
  218. cd d1/d2
  219. chmod a-rx ..
  220. assert_vcprompt "bad dir" ""
  221. chmod a+rx ..
  222. }
  223. test_env_var()
  224. {
  225. cd $tmpdir
  226. mkdir env_var && cd env_var
  227. mkdir .hg
  228. VCPROMPT_FORMAT='foo:%n' assert_vcprompt 'env var' 'foo:hg' '-'
  229. VCPROMPT_FORMAT='foo:%n' assert_vcprompt 'env var override' 'bar:hg' 'bar:%n'
  230. }
  231. test_format_trailing_percent()
  232. {
  233. cd $tmpdir
  234. mkdir format_trailing_percent
  235. cd format_trailing_percent
  236. mkdir .hg
  237. # unfortunately, this won't catch the problem, because the shell
  238. # filters trailing \0 characters in command substitution
  239. assert_vcprompt 'format trailing percent' 'foo:hg' 'foo:%n%'
  240. # therefore, we have to roll our own check
  241. if run_vcprompt 'foo:%n%' | od -b | grep -q ' 000'; then
  242. echo 'fail: trailing percent resulted in \\0 being printed' >&2
  243. failed='y'
  244. fi
  245. }
  246. test_help()
  247. {
  248. cd $tmpdir
  249. if $vcprompt -h > /dev/null; then
  250. echo "fail: help exit status: expected non-zero" >&2
  251. failed="y"
  252. else
  253. echo "pass: help exit status"
  254. fi
  255. for pat in '%b' '%r' '%u' '%m' '%n' '%%'; do
  256. if ! $vcprompt -h | grep "$pat" > /dev/null; then
  257. echo "fail: help text should contain '$pat'" >&2
  258. failed="y"
  259. fi
  260. done
  261. echo "pass: help text"
  262. }
  263. #set -e
  264. find_vcprompt
  265. setup
  266. failed=""
  267. test_no_vc
  268. test_root
  269. test_simple_cvs
  270. test_simple_fossil
  271. test_simple_git
  272. test_simple_hg
  273. test_simple_hg_bookmarks
  274. test_simple_hg_mq
  275. test_simple_hg_revlog
  276. test_simple_svn
  277. test_xml_svn
  278. test_truncated_svn
  279. test_bad_dir
  280. test_env_var
  281. test_format_trailing_percent
  282. test_help
  283. report