tests.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/bin/bash
  2. #
  3. # Build and runs tests for the protobuf project. The tests as written here are
  4. # used by both Jenkins and Travis, though some specialized logic is required to
  5. # handle the differences between them.
  6. on_travis() {
  7. if [ "$TRAVIS" == "true" ]; then
  8. "$@"
  9. fi
  10. }
  11. # For when some other test needs the C++ main build, including protoc and
  12. # libprotobuf.
  13. internal_build_cpp() {
  14. if [ -f src/protoc ]; then
  15. # Already built.
  16. return
  17. fi
  18. if [[ $(uname -s) == "Linux" && "$TRAVIS" == "true" ]]; then
  19. # Install GCC 4.8 to replace the default GCC 4.6. We need 4.8 for more
  20. # decent C++ 11 support in order to compile conformance tests.
  21. sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
  22. sudo apt-get update -qq
  23. sudo apt-get install -qq g++-4.8
  24. export CXX="g++-4.8" CC="gcc-4.8"
  25. fi
  26. ./autogen.sh
  27. ./configure
  28. make -j2
  29. }
  30. build_cpp() {
  31. internal_build_cpp
  32. make check -j2
  33. cd conformance && make test_cpp && cd ..
  34. }
  35. build_cpp_distcheck() {
  36. ./autogen.sh
  37. ./configure
  38. make distcheck -j2
  39. }
  40. build_csharp() {
  41. # Just for the conformance tests. We don't currently
  42. # need to really build protoc, but it's simplest to keep with the
  43. # conventions of the other builds.
  44. internal_build_cpp
  45. NUGET=/usr/local/bin/nuget.exe
  46. if [ "$TRAVIS" == "true" ]; then
  47. # Install latest version of Mono
  48. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
  49. echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
  50. echo "deb http://download.mono-project.com/repo/debian wheezy-libtiff-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list
  51. sudo apt-get update -qq
  52. sudo apt-get install -qq mono-devel referenceassemblies-pcl nunit
  53. wget www.nuget.org/NuGet.exe -O nuget.exe
  54. NUGET=../../nuget.exe
  55. fi
  56. (cd csharp/src; mono $NUGET restore)
  57. csharp/buildall.sh
  58. cd conformance && make test_csharp && cd ..
  59. }
  60. build_golang() {
  61. # Go build needs `protoc`.
  62. internal_build_cpp
  63. # Add protoc to the path so that the examples build finds it.
  64. export PATH="`pwd`/src:$PATH"
  65. # Install Go and the Go protobuf compiler plugin.
  66. sudo apt-get update -qq
  67. sudo apt-get install -qq golang
  68. export GOPATH="$HOME/gocode"
  69. mkdir -p "$GOPATH/src/github.com/google"
  70. ln -s "`pwd`" "$GOPATH/src/github.com/google/protobuf"
  71. export PATH="$GOPATH/bin:$PATH"
  72. go get github.com/golang/protobuf/protoc-gen-go
  73. cd examples && make gotest && cd ..
  74. }
  75. use_java() {
  76. version=$1
  77. case "$version" in
  78. jdk6)
  79. on_travis sudo apt-get install openjdk-6-jdk
  80. export PATH=/usr/lib/jvm/java-6-openjdk-amd64/bin:$PATH
  81. ;;
  82. jdk7)
  83. on_travis sudo apt-get install openjdk-7-jdk
  84. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  85. ;;
  86. oracle7)
  87. if [ "$TRAVIS" == "true" ]; then
  88. sudo apt-get install python-software-properties # for apt-add-repository
  89. echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | \
  90. sudo debconf-set-selections
  91. yes | sudo apt-add-repository ppa:webupd8team/java
  92. yes | sudo apt-get install oracle-java7-installer
  93. fi;
  94. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  95. ;;
  96. esac
  97. if [ "$TRAVIS" != "true" ]; then
  98. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  99. MVN="$MVN -e -X --offline -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  100. fi;
  101. which java
  102. java -version
  103. }
  104. # --batch-mode supresses download progress output that spams the logs.
  105. MVN="mvn --batch-mode"
  106. build_java() {
  107. version=$1
  108. dir=java_$version
  109. # Java build needs `protoc`.
  110. internal_build_cpp
  111. cp -r java $dir
  112. cd $dir && $MVN clean && $MVN test
  113. cd ../..
  114. }
  115. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  116. # So this can't run in parallel with two different sets of tests.
  117. build_java_with_conformance_tests() {
  118. # Java build needs `protoc`.
  119. internal_build_cpp
  120. cd java && $MVN test && $MVN install
  121. cd util && $MVN package assembly:single
  122. cd ../..
  123. cd conformance && make test_java && cd ..
  124. }
  125. build_javanano() {
  126. # Java build needs `protoc`.
  127. internal_build_cpp
  128. cd javanano && $MVN test && cd ..
  129. }
  130. build_java_jdk6() {
  131. use_java jdk6
  132. build_java jdk6
  133. }
  134. build_java_jdk7() {
  135. use_java jdk7
  136. build_java_with_conformance_tests
  137. }
  138. build_java_oracle7() {
  139. use_java oracle7
  140. build_java oracle7
  141. }
  142. build_javanano_jdk6() {
  143. use_java jdk6
  144. build_javanano
  145. }
  146. build_javanano_jdk7() {
  147. use_java jdk7
  148. build_javanano
  149. }
  150. build_javanano_oracle7() {
  151. use_java oracle7
  152. build_javanano
  153. }
  154. internal_install_python_deps() {
  155. if [ "$TRAVIS" != "true" ]; then
  156. return;
  157. fi
  158. # Install tox (OS X doesn't have pip).
  159. if [ $(uname -s) == "Darwin" ]; then
  160. sudo easy_install tox
  161. else
  162. sudo pip install tox
  163. fi
  164. # Only install Python2.6/3.x on Linux.
  165. if [ $(uname -s) == "Linux" ]; then
  166. sudo apt-get install -y python-software-properties # for apt-add-repository
  167. sudo apt-add-repository -y ppa:fkrull/deadsnakes
  168. sudo apt-get update -qq
  169. sudo apt-get install -y python2.6 python2.6-dev
  170. sudo apt-get install -y python3.3 python3.3-dev
  171. sudo apt-get install -y python3.4 python3.4-dev
  172. fi
  173. }
  174. internal_objectivec_common () {
  175. # Make sure xctool is up to date. Adapted from
  176. # http://docs.travis-ci.com/user/osx-ci-environment/
  177. # We don't use a before_install because we test multiple OSes.
  178. brew update
  179. brew outdated xctool || brew upgrade xctool
  180. # Reused the build script that takes care of configuring and ensuring things
  181. # are up to date. Xcode and conformance tests will be directly invoked.
  182. objectivec/DevTools/full_mac_build.sh \
  183. --core-only --skip-xcode --skip-objc-conformance
  184. }
  185. internal_xctool_debug_and_release() {
  186. # Always use -reporter plain to avoid escape codes in output (makes travis
  187. # logs easier to read).
  188. xctool -reporter plain -configuration Debug "$@"
  189. xctool -reporter plain -configuration Release "$@"
  190. }
  191. build_objectivec_ios() {
  192. internal_objectivec_common
  193. # https://github.com/facebook/xctool/issues/509 - unlike xcodebuild, xctool
  194. # doesn't support >1 destination, so we have to build first and then run the
  195. # tests one destination at a time.
  196. internal_xctool_debug_and_release \
  197. -project objectivec/ProtocolBuffers_iOS.xcodeproj \
  198. -scheme ProtocolBuffers \
  199. -sdk iphonesimulator \
  200. build-tests
  201. IOS_DESTINATIONS=(
  202. "platform=iOS Simulator,name=iPhone 4s,OS=8.1" # 32bit
  203. "platform=iOS Simulator,name=iPhone 6,OS=9.2" # 64bit
  204. "platform=iOS Simulator,name=iPad 2,OS=8.1" # 32bit
  205. "platform=iOS Simulator,name=iPad Air,OS=9.2" # 64bit
  206. )
  207. for i in "${IOS_DESTINATIONS[@]}" ; do
  208. internal_xctool_debug_and_release \
  209. -project objectivec/ProtocolBuffers_iOS.xcodeproj \
  210. -scheme ProtocolBuffers \
  211. -sdk iphonesimulator \
  212. -destination "${i}" \
  213. run-tests
  214. done
  215. }
  216. build_objectivec_osx() {
  217. internal_objectivec_common
  218. internal_xctool_debug_and_release \
  219. -project objectivec/ProtocolBuffers_OSX.xcodeproj \
  220. -scheme ProtocolBuffers \
  221. -destination "platform=OS X,arch=x86_64" \
  222. test
  223. cd conformance && make test_objc && cd ..
  224. }
  225. build_python() {
  226. internal_build_cpp
  227. internal_install_python_deps
  228. cd python
  229. # Only test Python 2.6/3.x on Linux
  230. if [ $(uname -s) == "Linux" ]; then
  231. envlist=py\{26,27,33,34\}-python
  232. else
  233. envlist=py27-python
  234. fi
  235. tox -e $envlist
  236. cd ..
  237. }
  238. build_python_cpp() {
  239. internal_build_cpp
  240. internal_install_python_deps
  241. export LD_LIBRARY_PATH=../src/.libs # for Linux
  242. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  243. cd python
  244. # Only test Python 2.6/3.x on Linux
  245. if [ $(uname -s) == "Linux" ]; then
  246. # py26 is currently disabled due to json_format
  247. envlist=py\{27,33,34\}-cpp
  248. else
  249. envlist=py27-cpp
  250. fi
  251. tox -e $envlist
  252. cd ..
  253. }
  254. build_ruby19() {
  255. internal_build_cpp # For conformance tests.
  256. cd ruby && bash travis-test.sh ruby-1.9 && cd ..
  257. }
  258. build_ruby20() {
  259. internal_build_cpp # For conformance tests.
  260. cd ruby && bash travis-test.sh ruby-2.0 && cd ..
  261. }
  262. build_ruby21() {
  263. internal_build_cpp # For conformance tests.
  264. cd ruby && bash travis-test.sh ruby-2.1 && cd ..
  265. }
  266. build_ruby22() {
  267. internal_build_cpp # For conformance tests.
  268. cd ruby && bash travis-test.sh ruby-2.2 && cd ..
  269. }
  270. build_jruby() {
  271. internal_build_cpp # For conformance tests.
  272. cd ruby && bash travis-test.sh jruby && cd ..
  273. }
  274. build_javascript() {
  275. internal_build_cpp
  276. cd js && npm install && npm test && cd ..
  277. }
  278. # Note: travis currently does not support testing more than one language so the
  279. # .travis.yml cheats and claims to only be cpp. If they add multiple language
  280. # support, this should probably get updated to install steps and/or
  281. # rvm/gemfile/jdk/etc. entries rather than manually doing the work.
  282. # .travis.yml uses matrix.exclude to block the cases where app-get can't be
  283. # use to install things.
  284. # -------- main --------
  285. if [ "$#" -ne 1 ]; then
  286. echo "
  287. Usage: $0 { cpp |
  288. csharp |
  289. java_jdk6 |
  290. java_jdk7 |
  291. java_oracle7 |
  292. javanano_jdk6 |
  293. javanano_jdk7 |
  294. javanano_oracle7 |
  295. objectivec_ios |
  296. objectivec_osx |
  297. python |
  298. python_cpp |
  299. ruby19 |
  300. ruby20 |
  301. ruby21 |
  302. ruby22 |
  303. jruby }
  304. "
  305. exit 1
  306. fi
  307. set -e # exit immediately on error
  308. set -x # display all commands
  309. eval "build_$1"