travis_diff.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. # @file: toolset/travis/travis_diff.py
  3. # @author: Nate Brady
  4. #
  5. # @description: This script is only for use within Travis-CI. It is meant to
  6. # look through the commit history and determine whether or not the current
  7. # framework test directory needs to be run. It compares the state of the PR
  8. # branch against the target branch.
  9. #
  10. # Any changes found in the toolset/* directory other than continuous/*, travis/*
  11. # and scaffolding/* will cause all tests to be run.
  12. #
  13. # The following commands can be put in commit messages to affect which tests
  14. # will run:
  15. #
  16. # [ci skip] - Provided by Travis. Travis won't trigger any builds.
  17. # [ci run-all] - This will force all tests to run.
  18. # [ci fw-only Java/gemini JavaScript/nodejs] - Ensures that only Java/gemini and
  19. # JavaScript/nodejs tests are run despite the detected changes.
  20. # [ci fw Java/gemini] - Forces Java/gemini to run in addition to detected changes.
  21. # [ci lang-only Java C++] - Ensures that only Java and C++ run despite detected changes.
  22. # [ci lang Java C++] - Forces Java and C++ tests to run in addition to detected changes.
  23. #
  24. # If only a single test within a language group is forced to run, none of the
  25. # other tests in that language group will run.
  26. #
  27. # The master branch will run the full suite of tests.
  28. #
  29. # IMPORTANT: the [ci *] commands must be added to every commit message. We do
  30. # not look at previous commit messages. Make sure to keep your PR branch
  31. # up-to-date with the target branch to avoid running unwanted tests.
  32. import subprocess
  33. import os
  34. import re
  35. def fw_found_in_changes(test, changes_output):
  36. return re.search(
  37. r"frameworks/" + re.escape(test) + "/",
  38. changes_output, re.M)
  39. # Cleans up diffing and grep output and into an array of strings
  40. def clean_output(output):
  41. return os.linesep.join([s for s in output.splitlines() if s])
  42. def quit_diffing():
  43. if len(run_tests):
  44. print("travis-run-tests {!s}".format(" ".join(set(run_tests))))
  45. else:
  46. print("No tests to run.")
  47. exit(0)
  48. curr_branch = ""
  49. is_PR = (os.getenv("TRAVIS_PULL_REQUEST") != "false")
  50. # TRAVIS_BRANCH is the target branch when it's a pull request or the name
  51. # of the branch when it isn't
  52. is_master = not is_PR and os.getenv("TRAVIS_BRANCH") == "master"
  53. if is_PR:
  54. curr_branch = "FETCH_HEAD"
  55. elif not is_master:
  56. curr_branch = os.getenv("TRAVIS_COMMIT")
  57. # Also fetch master to compare against
  58. subprocess.check_output(['bash', '-c', 'git fetch origin master:master'])
  59. # https://stackoverflow.com/questions/25071579/list-all-files-changed-in-a-pull-request-in-git-github
  60. changes = clean_output(
  61. subprocess.check_output([
  62. 'bash', '-c',
  63. 'git --no-pager diff --name-only {0} $(git merge-base {0} master)'
  64. .format(curr_branch)
  65. ]))
  66. print("Determining what to run based on the following file changes: \n{!s}"
  67. .format('\n'.join(changes.split('\n')[0:10])))
  68. if len(changes.split('\n')) > 10:
  69. print("Too many files to show.")
  70. # COMMIT MESSAGES:
  71. # Before any complicated diffing, check for forced runs from the commit message
  72. # Use -2 because travis now inserts a merge commit as the last commit
  73. last_commit_msg = os.getenv("TRAVIS_COMMIT_MESSAGE")
  74. test_dirs = []
  75. run_tests = []
  76. # Break the test env variable down into test directories
  77. if os.getenv("TESTLANG"):
  78. dir = "frameworks/" + os.getenv("TESTLANG") + "/"
  79. test_dirs = map(lambda x: os.getenv("TESTLANG") + "/" + x,
  80. filter(lambda x: os.path.isdir(dir + x), os.listdir(dir)))
  81. elif os.getenv("TESTDIR"):
  82. test_dirs = os.getenv("TESTDIR").split(' ')
  83. # Forced full run
  84. if is_master or re.search(r'\[ci run-all\]', last_commit_msg, re.M):
  85. print("All tests have been forced to run from the commit message.")
  86. run_tests = test_dirs
  87. quit_diffing()
  88. # Forced *fw-only* specific tests
  89. if re.search(r'\[ci fw-only .+\]', last_commit_msg, re.M):
  90. tests = re.findall(r'\[ci fw-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  91. for test in tests:
  92. if test in test_dirs:
  93. print("{!s} has been forced to run from the commit message.".format(test))
  94. run_tests.append(test)
  95. # quit here because we're using "only"
  96. quit_diffing()
  97. # Forced *lang-only* specific tests
  98. if re.search(r'\[ci lang-only .+\]', last_commit_msg, re.M):
  99. langs = re.findall(r'\[ci lang-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  100. for test in test_dirs:
  101. for lang in langs:
  102. if test.startswith(lang + "/"):
  103. print("{!s} has been forced to run from the commit message.".format(test))
  104. run_tests.append(test)
  105. # quit here because we're using "only"
  106. quit_diffing()
  107. # Forced framework run in addition to other tests
  108. if re.search(r'\[ci fw .+\]', last_commit_msg, re.M):
  109. tests = re.findall(r'\[ci fw (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  110. for test in tests:
  111. if test in test_dirs:
  112. print("{!s} has been forced to run from the commit message.".format(test))
  113. run_tests.append(test)
  114. # Forced lang run in addition to other running tests
  115. if re.search(r'\[ci lang .+\]', last_commit_msg, re.M):
  116. langs = re.findall(r'\[ci lang (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  117. for test in test_dirs:
  118. for lang in langs:
  119. if test.startswith(lang + "/"):
  120. print("{!s} has been forced to run from the commit message.".format(test))
  121. run_tests.append(test)
  122. # Ignore travis, continuous and scaffolding changes
  123. if re.search(r'^toolset\/(?!(travis\/|continuous\/|scaffolding\/))', changes, re.M) is not None:
  124. print("Found changes to core toolset. Running all tests.")
  125. run_tests = test_dirs
  126. quit_diffing()
  127. for test in test_dirs:
  128. if fw_found_in_changes(test, changes):
  129. print("Found changes that affect {!s}".format(test))
  130. run_tests.append(test)
  131. quit_diffing()