You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

89 lines
2.1 KiB

  1. #!/bin/bash
  2. # Selectively run tests for this repo, based on what has changed
  3. # in a commit. Runs short tests for the whole repo, and full tests
  4. # for changed directories.
  5. set -e
  6. prefix=cloud.google.com/go
  7. dryrun=false
  8. if [[ $1 == "-n" ]]; then
  9. dryrun=true
  10. shift
  11. fi
  12. if [[ $1 == "" ]]; then
  13. echo >&2 "usage: $0 [-n] COMMIT"
  14. exit 1
  15. fi
  16. # Files or directories that cause all tests to run if modified.
  17. declare -A run_all
  18. run_all=([.travis.yml]=1 [run-tests.sh]=1)
  19. function run {
  20. if $dryrun; then
  21. echo $*
  22. else
  23. (set -x; $*)
  24. fi
  25. }
  26. # Find all the packages that have changed in this commit.
  27. declare -A changed_packages
  28. for f in $(git diff-tree --no-commit-id --name-only -r $1); do
  29. if [[ ${run_all[$f]} == 1 ]]; then
  30. # This change requires a full test. Do it and exit.
  31. run go test -race -v $prefix/...
  32. exit
  33. fi
  34. # Map, e.g., "spanner/client.go" to "$prefix/spanner".
  35. d=$(dirname $f)
  36. if [[ $d == "." ]]; then
  37. pkg=$prefix
  38. else
  39. pkg=$prefix/$d
  40. fi
  41. changed_packages[$pkg]=1
  42. done
  43. echo "changed packages: ${!changed_packages[*]}"
  44. # Reports whether its argument, a package name, depends (recursively)
  45. # on a changed package.
  46. function depends_on_changed_package {
  47. # According to go list, a package does not depend on itself, so
  48. # we test that separately.
  49. if [[ ${changed_packages[$1]} == 1 ]]; then
  50. return 0
  51. fi
  52. for dep in $(go list -f '{{range .Deps}}{{.}} {{end}}' $1); do
  53. if [[ ${changed_packages[$dep]} == 1 ]]; then
  54. return 0
  55. fi
  56. done
  57. return 1
  58. }
  59. # Collect the packages into two separate lists. (It is faster to call "go test" on a
  60. # list of packages than to individually "go test" each one.)
  61. shorts=
  62. fulls=
  63. for pkg in $(go list $prefix/...); do # for each package in the repo
  64. if depends_on_changed_package $pkg; then # if it depends on a changed package
  65. fulls="$fulls $pkg" # run the full test
  66. else # otherwise
  67. shorts="$shorts $pkg" # run the short test
  68. fi
  69. done
  70. run go test -race -v -short $shorts
  71. if [[ $fulls != "" ]]; then
  72. run go test -race -v $fulls
  73. fi