PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/test.sh

https://github.com/tobinibot/wkhtmltopdf
Shell | 288 lines | 223 code | 43 blank | 22 comment | 82 complexity | 6c3bc3bd6da1211975e276c8dc203d23 MD5 | raw file
  1. #!/bin/bash
  2. #
  3. # Copyright 2010 wkhtmltopdf authors
  4. #
  5. # This file is part of wkhtmltopdf.
  6. #
  7. # wkhtmltopdf is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # wkhtmltopdf is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with wkhtmltopdf. If not, see <http:#www.gnu.org/licenses/>.
  19. #This script will test a compiled wkhtmltopdf for some basic functionality
  20. function result() { printf "%-30s [%-4s]\n" "$1" "$2";}
  21. function good() { result "$1" " OK ";}
  22. function bad() { result "$1" "Fail"; [ "$2" != "false" ] && export failed=$(($failed+1));}
  23. function fs() { du -b "$1" | sed -re 's/([0-9]*).*/\1/';}
  24. function wk() { $WK -q "$@"; }
  25. function pages() {
  26. pdfinfo $1 | sed -nre 's/Pages:[\n ]*//p'
  27. }
  28. function testTest() {
  29. (which pdftotext > $LEVEL1 && which pdfinfo > $LEVEL1) && good $1 || bad $1
  30. }
  31. #Test if we can convert a html file containing
  32. #An image of some format
  33. function doTestImgSupport() {
  34. [ -f img.$1 ] || convert img.jpg img.$1
  35. rm -rf tmp.pdf
  36. echo "<html><head><title>Img test $1</title></head><body><h1>The $1 image</h1><img src=\"img.$1\" /></body></html>" > tmp.html
  37. wk tmp.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  38. S=`fs tmp.pdf`
  39. ([ -f tmp.pdf ] && [[ $S -ge 20000 ]]) && good "$1 Suppport ($S)" || bad "$1 Support ($S)" "$2"
  40. }
  41. function testJPEGSupport() {
  42. doTestImgSupport jpg true
  43. }
  44. function testGIFSupport() {
  45. doTestImgSupport gif true
  46. }
  47. function testPNGSupport() {
  48. doTestImgSupport png true
  49. }
  50. function testMNGSupport() {
  51. doTestImgSupport mng false
  52. }
  53. function testTIFFSupport() {
  54. doTestImgSupport tiff false
  55. }
  56. function testLocalFile() {
  57. echo "<html><head><title>Local Test</title></head><body><h1>Hello</h1></body></html>" > tmp.html
  58. wk tmp.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  59. ([ -f tmp.pdf ] && pdftotext tmp.pdf /dev/stdout | grep -q Hello) && good $1 || bad $1
  60. }
  61. function testUserStyleSheet() {
  62. echo "<html><head><title>Local Test</title></head><body><p>.</p></body></html>" > tmp.html
  63. echo "p:before {content: \"Hello \"}" > tmp.css
  64. wk tmp.html --user-style-sheet tmp.css tmp.pdf 2>$LEVEL2 >$LEVEL1
  65. ([ -f tmp.pdf ] && pdftotext tmp.pdf /dev/stdout | grep -q Hello) && good $1 || bad $1 false
  66. }
  67. function testPipeIn() {
  68. rm -rf tmp.pdf
  69. echo "<html><head><title>Local Test</title></head><body><h1>Hello</h1></body></html>" > tmp.html
  70. cat tmp.html | wk - tmp.pdf 2>$LEVEL2 >$LEVEL1
  71. ([ -f tmp.pdf ] && pdftotext tmp.pdf /dev/stdout | grep -q Hello) && good $1 || bad $1
  72. }
  73. function testPipeOutSupport() {
  74. echo "<html><head><title>Local Test</title></head><body><h1>Hello</h1></body></html>" > tmp.html
  75. wk tmp.html - > tmp.pdf 2>$LEVEL2
  76. ([ -f tmp.pdf ] && pdftotext tmp.pdf /dev/stdout | grep -q Hello) && good $1 || bad $1
  77. }
  78. function testRemote() {
  79. wk http://www.google.dk/ tmp.pdf 2>$LEVEL2 >$LEVEL1
  80. [ -f tmp.pdf ] && good $1 || bad $1
  81. }
  82. function testSSL() {
  83. wk https://www.google.com/accouts tmp.pdf 2>$LEVEL2 >$LEVEL1
  84. [ -f tmp.pdf ] && good $1 || bad $1
  85. }
  86. function testHeaderFooter() {
  87. echo "<html><head><title>Local Test</title></head><body><h1>monster</h1></body></html>" > tmp.html
  88. wk tmp.html --footer-left hat --header-right emacs tmp.pdf 2>$LEVEL2 >$LEVEL1
  89. ([ -f tmp.pdf ] &&
  90. pdftotext tmp.pdf /dev/stdout | grep -q monster &&
  91. pdftotext tmp.pdf /dev/stdout | grep -q emacs &&
  92. pdftotext tmp.pdf /dev/stdout | grep -q hat) && good $1 || bad $1
  93. }
  94. function testLoadError() {
  95. wk about:blank http://a.nstld --load-error-handling abort tmp.pdf 2>$LEVEL1 >$LEVEL1 && (bad $1 && return)
  96. wk about:blank http://a.nstld --load-error-handling skip tmp1.pdf 2>$LEVEL2 >$LEVEL1 || (bad $1 && return)
  97. wk about:blank http://a.nstld --load-error-handling ignore tmp2.pdf 2>$LEVEL2 >$LEVEL1 || (bad $1 && return)
  98. ([ -f tmp1.pdf ] && [ -f tmp2.pdf ] &&
  99. [ "$(pages tmp1.pdf)" == 1 ] &&
  100. [ "$(pages tmp2.pdf)" == 2 ] ) && good $1 || bad $1
  101. }
  102. function testFontSpacing() {
  103. echo "<html><body style=\"font-size: 7pt; font-family: Arial;\">vii vaa vuu vvv vee</body></html>" > tmp.html
  104. wk tmp.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  105. ([ -f tmp.pdf ] &&
  106. pdftotext tmp.pdf /dev/stdout | grep -q "vii vaa vuu vvv vee") && good $1 || bad $1 false
  107. }
  108. function testToc() {
  109. echo "<html><head></head><body><h1>foo</h1><h2>bar</h2><h3>baz</h3></body>" > tmp.html
  110. wk toc tmp.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  111. ([ -f tmp.pdf ] &&
  112. [ "$(pdftotext tmp.pdf /dev/stdout | grep -c foo)" == 2 ] &&
  113. [ "$(pdftotext tmp.pdf /dev/stdout | grep -c bar)" == 2 ]) && good $1 || bad $1
  114. }
  115. function testOutline() {
  116. echo "<html><head></head><body><h1>foo</h1><h2>bar</h2><h3>baz</h3></body>" > tmp.html
  117. wk --outline --outline-depth 2 tmp.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  118. ([ -f tmp.pdf ] &&
  119. cat tmp.pdf | grep -q ".f.o.o" &&
  120. cat tmp.pdf | grep -q ".b.a.r" &&
  121. ! cat tmp.pdf | grep -q ".b.a.z") && good $1 || bad $1
  122. }
  123. function testJSRedirect() {
  124. wk http://madalgo.au.dk/~jakobt/jsredirect.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  125. ([ -f tmp.pdf ] &&
  126. pdftotext tmp.pdf /dev/stdout | grep -q Right) && good $1 || bad $1
  127. }
  128. function testServersideRedirect() {
  129. wk http://madalgo.au.dk/~jakobt/redirect.php tmp.pdf 2>$LEVEL2 >$LEVEL1
  130. ([ -f tmp.pdf ] &&
  131. pdftotext tmp.pdf /dev/stdout | grep -q Right) && good $1 || bad $1
  132. }
  133. function test404() {
  134. wk http://madalgo.au.dk/~jakobt/nosuchfile.nsf tmp.pdf 2>$LEVEL2 >$LEVEL1 && bad $1 || good $1
  135. }
  136. function testBadDest() {
  137. echo "<html><head></head><body><h1>foo</h1><h2>bar</h2><h3>baz</h3></body>" > tmp.html
  138. (! wk tmp.html /proc/cpuinfo 2> tmp.out >$LEVEL1 && grep -q "Error" tmp.out) && good $1 || bad $1
  139. }
  140. function testMultipleInputDocuments() {
  141. echo "<html><head><title>Local Test</title></head><body><h1>Hello</h1></body></html>" > tmp.html
  142. echo "<html><head><title>Local Test</title></head><body><h1>world</h1></body></html>" > tmp2.html
  143. wk tmp.html tmp2.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  144. ([ -f tmp.pdf ] &&
  145. pdftotext tmp.pdf /dev/stdout | grep -q Hello &&
  146. pdftotext tmp.pdf /dev/stdout | grep -q world) && good $1 || bad $1
  147. }
  148. function testHtmlHeader() {
  149. echo "<html><body>Header</body></html>" > tmp.html
  150. echo "<html><head><title>Local Test</title></head><body><h1>world</h1></body></html>" > tmp2.html
  151. wk --header-html tmp.html tmp2.html tmp.pdf 2>$LEVEL2 >$LEVEL1
  152. ([ -f tmp.pdf ] &&
  153. pdftotext tmp.pdf /dev/stdout | grep -q Header &&
  154. pdftotext tmp.pdf /dev/stdout | grep -q world) && good $1 || bad $1
  155. }
  156. function testCustomHeader() {
  157. wk http://madalgo.au.dk/~jakobt/cookiewrite.php --custom-header "Cookie" "cookie=hello" tmp.pdf 2>$LEVEL2 >$LEVEL1
  158. ([ -f tmp.pdf ] &&
  159. pdftotext tmp.pdf /dev/stdout | grep -q hello) && good $1 || bad $1
  160. }
  161. function testCookies() {
  162. wk --cookie mykey myvalue1 --cookie mykey2 myvalue2 --cookie-jar tmp.jar http://cs.au.dk/~jakobt/cookie.php tmp.pdf 2>$LEVEL2 >$LEVEL1
  163. wk --cookie-jar tmp.jar http://cs.au.dk/~jakobt/cookie.php tmp2.pdf 2>$LEVEL2 >$LEVEL1
  164. ( [ -f tmp.pdf ] &&
  165. pdftotext tmp.pdf /dev/stdout | grep -q "mykey:myvalue1;" &&
  166. pdftotext tmp.pdf /dev/stdout | grep -q "mykey2:myvalue2;" &&
  167. [ -f tmp2.pdf ] &&
  168. pdftotext tmp2.pdf /dev/stdout | grep -q "writetest:success;") &&
  169. good $1 || bad $1
  170. }
  171. function testTitle() {
  172. title="\()fooæøåおさか おかみ"
  173. wk --title "$title" http://google.com tmp.pdf 2>$LEVEL2 >$LEVEL1
  174. ([ -f tmp.pdf ] && [[ "$(pdfinfo tmp.pdf | sed -nre 's/Title:[\t ]*//p')" == "$title" ]]) && good $1 || bad $1
  175. }
  176. function testQMakeBuild() {
  177. (cd .. && git checkout-index --prefix=./test/wkhtmltopdf/ -a) || (bad "$1 (1)" && return 1)
  178. cd wkhtmltopdf
  179. qmake 2>$LEVEL2 >$LEVEL1 || (bad "$1 (2)" && return 1)
  180. make -j2 2>$LEVEL2 >$LEVEL1 && good $1 || bad $1
  181. cd ..
  182. }
  183. function testArgumentFromStdin() {
  184. echo "<html><body>XFooZ</body></html>" > tmp.html
  185. wk --read-args-from-stdin 2>$LEVEL2 >$LEVEL1 <<EOF
  186. -q tmp.html tmp1.pdf
  187. -q tmp.html tmp2.pdf
  188. EOF
  189. ( [ -f tmp1.pdf ] && pdftotext tmp1.pdf /dev/stdout | grep -q XFooZ &&
  190. [ -f tmp2.pdf ] && pdftotext tmp2.pdf /dev/stdout | grep -q XFooZ) && good $1 || bad $1
  191. }
  192. function usage() {
  193. echo "Usage $0 [OPTIONS] [EXPRESSION]"
  194. echo ""
  195. echo "Options:"
  196. echo "-h Display this message"
  197. echo "-v Be more verbose"
  198. echo "-q Be more quiet"
  199. echo "-w path wkhtmltopdf binary to test"
  200. echo ""
  201. echo "Expression:"
  202. echo "grep regular expresion on what tests to run"
  203. }
  204. LEVEL1=/dev/null
  205. LEVEL2=/dev/stdout
  206. LEVEL3=/dev/stdout
  207. export WK=../bin/wkhtmltopdf
  208. while getopts hvqw: O; do
  209. case "$O" in
  210. [h?])
  211. usage
  212. exit 1
  213. ;;
  214. v)
  215. shift 1
  216. LEVEL1=/dev/stdout
  217. ;;
  218. q)
  219. shift 1
  220. LEVEL2=/dev/null
  221. ;;
  222. w)
  223. shift 2
  224. export WK=$OPTARG
  225. ;;
  226. esac
  227. done
  228. R=$1
  229. if [ -z "$R" ]; then
  230. R=.*
  231. fi
  232. TESTS=$(cat $0 | sed -nre 's/^function test(.*)\(\)\ \{.*/\1/p' | grep "$R");
  233. #This is just some random image
  234. img=http://madalgo.au.dk/img/Forside/KatrinebjMay-05.jpg
  235. [ -d test ] || mkdir test
  236. cd test
  237. [ -f img.jpg ] || wget $img -O img.jpg
  238. failed=0
  239. TMPFILES="tmp.pdf tmp.html tmp.css tmp2.html tmp.jar tmp1.pdf tmp2.pdf wkhtmltopdf/"
  240. for test in $TESTS; do
  241. rm -rf $TMPFILES
  242. eval test$test $test
  243. done
  244. rm -rf $TMPFILES
  245. exit $failed