test.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. # This file is part of wkhtmltopdf.
  3. #
  4. # wkhtmltopdf is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # wkhtmltopdf is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with wkhtmltopdf. If not, see <http:#www.gnu.org/licenses/>.
  16. #This script will test a compiled wkhtmltopdf for some basic functionality
  17. #This is just some random image
  18. img=http://madalgo.au.dk/img/Forside/KatrinebjMay-05.jpg
  19. [ -d test ] || mkdir test
  20. cd test
  21. [ -f img.jpg ] || wget $img -O img.jpg
  22. export WK=../wkhtmltopdf
  23. failed=0
  24. function result() { printf "%-30s [%-4s]\n" "$1" "$2";}
  25. function good() { result "$1" " OK ";}
  26. function bad() { result "$1" "Fail"; export failed=$(($failed+1));}
  27. function fs() { du -b "$1" | sed -re 's/([0-9]*).*/\1/';}
  28. function wk() { $WK --redirect-delay 0 -q $*;}
  29. #Test if we can convert a html file containing
  30. #An image of some format
  31. function testImgSupport() {
  32. [ -f img.$1 ] || convert img.jpg img.$1
  33. rm -rf tmp.pdf
  34. echo "<html><head><title>Img test $1</title></head><body><h1>The $1 image</h1><img src=\"img.$1\" /></body></html>" > tmp.html
  35. wk tmp.html tmp.pdf
  36. S=`fs tmp.pdf`
  37. ([ -f tmp.pdf ] && [[ $S -ge 20000 ]]) && good "$1 Suppport ($S)" || bad "$1 Support ($S)"
  38. }
  39. #Test if we can convert a local file, and that it has some of the right words
  40. function testLocalFileSupport() {
  41. rm -rf tmp.pdf
  42. echo "<html><head><title>Local Test</title></head><body><h1>Hello</h1></body></html>" > tmp.html
  43. wk tmp.html tmp.pdf
  44. ([ -f tmp.pdf ] && pdftotext tmp.pdf /dev/stdout | grep -q Hello) && good LocalFile || bad LocalFile
  45. }
  46. function testPipeInSupport() {
  47. rm -rf tmp.pdf
  48. echo "<html><head><title>Local Test</title></head><body><h1>Hello</h1></body></html>" > tmp.html
  49. cat tmp.html | wk - tmp.pdf
  50. ([ -f tmp.pdf ] && pdftotext tmp.pdf /dev/stdout | grep -q Hello) && good PipeInFile || bad PipeInFile
  51. }
  52. function testPipeOutSupport() {
  53. rm -rf tmp.pdf
  54. echo "<html><head><title>Local Test</title></head><body><h1>Hello</h1></body></html>" > tmp.html
  55. wk tmp.html - > tmp.pdf
  56. ([ -f tmp.pdf ] && pdftotext tmp.pdf /dev/stdout | grep -q Hello) && good PipeOutFile || bad PipeQutFile
  57. }
  58. #Test if we can convert a remove site.
  59. function testRemote() {
  60. rm -rf tmp.pdf
  61. wk http://www.google.dk/ tmp.pdf
  62. [ -f tmp.pdf ] && good Remote || bad Remote
  63. }
  64. #Test if we support SSL based sites
  65. function testSSL() {
  66. rm -rf tmp.pdf
  67. wk https://eopen.microsoft.com/ tmp.pdf
  68. [ -f tmp.pdf ] && good SSL || bad SSL
  69. }
  70. #Test if the header footer stuff works
  71. function testHeaderFooter() {
  72. rm -rf tmp.pdf
  73. echo "<html><head><title>Local Test</title></head><body><h1>monster</h1></body></html>" > tmp.html
  74. wk tmp.html tmp.pdf --footer-left hat --header-right emacs
  75. ([ -f tmp.pdf ] &&
  76. pdftotext tmp.pdf /dev/stdout | grep -q monster &&
  77. pdftotext tmp.pdf /dev/stdout | grep -q emacs &&
  78. pdftotext tmp.pdf /dev/stdout | grep -q hat) && good HeaderFooter || bad HeaderFooter
  79. }
  80. function testToc() {
  81. echo "<html><head></head><body><h1 style=\"visibility: hidden\">foo</h1><h2 style=\"visibility: hidden\">bar</h2><h3 style=\"visibility: hidden\">baz</h3></body>" > tmp.html
  82. wk tmp.html tmp.pdf --toc --toc-depth 2
  83. ([ -f tmp.pdf ] &&
  84. pdftotext tmp.pdf /dev/stdout | grep -q foo &&
  85. pdftotext tmp.pdf /dev/stdout | grep -q bar &&
  86. ! pdftotext tmp.pdf /dev/stdout | grep -q baz) && good Toc || bad Toc
  87. }
  88. function testOutline() {
  89. echo "<html><head></head><body><h1 style=\"visibility: hidden\">foo</h1><h2 style=\"visibility: hidden\">bar</h2><h3 style=\"visibility: hidden\">baz</h3></body>" > tmp.html
  90. wk tmp.html tmp.pdf --outline --outline-depth 2
  91. ([ -f tmp.pdf ] &&
  92. cat tmp.pdf | grep -q ".f.o.o" &&
  93. cat tmp.pdf | grep -q ".b.a.r" &&
  94. ! cat tmp.pdf | grep -q ".b.a.z") && good Outline || bad Outline
  95. }
  96. function testBuild() {
  97. rm -rf wkhtmltopdf
  98. git checkout-index --prefix=./wkhtmltopdf/ -a || (bad "Build $1 (1)" && return 1)
  99. cd wkhtmltopdf
  100. if [[ "$1" == "qmake" ]]; then
  101. qmake 2>/dev/null >/dev/null || (bad "Build $1 (2)" && return 1)
  102. else
  103. cmake . 2>/dev/null >/dev/null || (bad "Build $1 (2)" && return 1)
  104. fi
  105. make -j5 >/dev/null 2>/dev/null && good "Build $1" || bad "Build $1 (3)"
  106. cd ..
  107. rm -rf wkhtmltopdf
  108. }
  109. good TestTest
  110. testLocalFileSupport
  111. testPipeInSupport
  112. testPipeOutSupport
  113. testToc
  114. testOutline
  115. testImgSupport jpg
  116. testImgSupport gif
  117. testImgSupport png
  118. #testImgSupport mng
  119. #testImgSupport tiff
  120. testRemote
  121. testSSL
  122. testHeaderFooter
  123. #testBuild qmake
  124. #testBuild cmake
  125. #Lets clean up
  126. rm tmp.html tmp.pdf
  127. exit $failed