common.bash 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. set -euo pipefail
  3. # Copyright (C) 2016 The Syncthing Authors.
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public
  6. # License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. # You can obtain one at https://mozilla.org/MPL/2.0/.
  8. ulimit -t 600 || true
  9. ulimit -d 1024000 || true
  10. ulimit -m 1024000 || true
  11. export CGO_ENABLED=0
  12. export GO386=387
  13. export GOARM=5
  14. function init {
  15. echo Initializing
  16. export GOPATH=$(pwd)
  17. export WORKSPACE="${WORKSPACE:-$GOPATH}"
  18. go version
  19. rm -f *.tar.gz *.zip *.deb *.snap
  20. cd src/github.com/syncthing/syncthing
  21. version=$(go run build.go version)
  22. echo "Building $version"
  23. echo
  24. }
  25. function clean {
  26. echo Cleaning
  27. rm -rf "$GOPATH/pkg"
  28. git clean -fxd
  29. rm -rf parts # created by snapcraft, contains git repo so not cleaned by git above
  30. echo
  31. }
  32. function fetchExtra {
  33. echo Fetching extra resources
  34. mkdir extra
  35. curl -s -o extra/Getting-Started.pdf https://docs.syncthing.net/pdf/Getting-Started.pdf
  36. curl -s -o extra/FAQ.pdf https://docs.syncthing.net/pdf/FAQ.pdf
  37. echo
  38. }
  39. function checkAuthorsCopyright {
  40. echo Basic metadata checks
  41. go run script/check-authors.go
  42. go run script/check-copyright.go lib/ cmd/ script/
  43. echo
  44. }
  45. function build {
  46. echo Build
  47. go run build.go
  48. echo
  49. }
  50. function test {
  51. echo Test with race
  52. CGO_ENABLED=1 go run build.go -race test
  53. echo
  54. }
  55. function testWithCoverage {
  56. echo Test with coverage
  57. CGO_ENABLED=1
  58. echo "mode: set" > coverage.out
  59. fail=0
  60. # For every package in the repo
  61. for dir in $(go list ./lib/... ./cmd/...) ; do
  62. # run the tests
  63. GOPATH="$(pwd)/Godeps/_workspace:$GOPATH" go test -coverprofile=profile.out $dir
  64. if [ -f profile.out ] ; then
  65. # and if there was test output, append it to coverage.out
  66. grep -v "mode: " profile.out >> coverage.out
  67. rm profile.out
  68. fi
  69. done
  70. gocov convert coverage.out | gocov-xml > coverage.xml
  71. # This is usually run from within Jenkins. If it is, we need to
  72. # tweak the paths in coverage.xml so cobertura finds the
  73. # source.
  74. if [[ "${WORKSPACE:-default}" != "default" ]] ; then
  75. sed "s#$WORKSPACE##g" < coverage.xml > coverage.xml.new && mv coverage.xml.new coverage.xml
  76. fi
  77. notCovered=$(egrep -c '\s0$' coverage.out)
  78. total=$(wc -l coverage.out | awk '{print $1}')
  79. coverPct=$(awk "BEGIN{print (1 - $notCovered / $total) * 100}")
  80. echo "$coverPct" > "coverage.txt"
  81. echo "Test coverage is $coverPct%%"
  82. echo
  83. CGO_ENABLED=0 # reset to before
  84. }
  85. function buildSource {
  86. echo Archiving source
  87. echo "$version" > RELEASE
  88. pushd .. >/dev/null
  89. tar c -z -f "$WORKSPACE/syncthing-source-$version.tar.gz" --exclude .git syncthing
  90. popd >/dev/null
  91. echo
  92. }