Skip to content

Commit aa36723

Browse files
committed
Moving Travis helpers out of run_pylint into shared module.
1 parent dc18351 commit aa36723

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

scripts/run_pylint.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
from script_utils import LOCAL_BRANCH_ENV
3333
from script_utils import LOCAL_REMOTE_ENV
34+
from script_utils import in_travis
35+
from script_utils import in_travis_pr
36+
from script_utils import travis_branch
3437

3538

3639
IGNORED_DIRECTORIES = [
@@ -146,10 +149,10 @@ def get_files_for_linting(allow_limited=True):
146149
against for changed files. (This requires ``allow_limited=True``.)
147150
148151
To speed up linting on Travis pull requests against master, we manually
149-
set the diff base to origin/master. We don't do this on non-pull requests
150-
since origin/master will be equivalent to the currently checked out code.
151-
One could potentially use ${TRAVIS_COMMIT_RANGE} to find a diff base but
152-
this value is not dependable.
152+
set the diff base to the branch the pull request is against. We don't do
153+
this on "push" builds since "master" will be the currently checked out
154+
code. One could potentially use ${TRAVIS_COMMIT_RANGE} to find a diff base
155+
but this value is not dependable.
153156
154157
To allow faster local ``tox`` runs, the local remote and local branch
155158
environment variables can be set to specify a remote branch to diff
@@ -164,12 +167,12 @@ def get_files_for_linting(allow_limited=True):
164167
linted.
165168
"""
166169
diff_base = None
167-
if (os.getenv('TRAVIS_BRANCH') == 'master' and
168-
os.getenv('TRAVIS_PULL_REQUEST') != 'false'):
169-
# In the case of a pull request into master, we want to
170-
# diff against HEAD in master.
171-
diff_base = 'origin/master'
172-
elif os.getenv('TRAVIS') is None:
170+
if in_travis():
171+
# In the case of a pull request into a branch, we want to
172+
# diff against HEAD in that branch.
173+
if in_travis_pr():
174+
diff_base = travis_branch()
175+
else:
173176
# Only allow specified remote and branch in local dev.
174177
remote = os.getenv(LOCAL_REMOTE_ENV)
175178
branch = os.getenv(LOCAL_BRANCH_ENV)

scripts/script_utils.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,72 @@
1414

1515
"""Common helpers for testing scripts."""
1616

17+
import os
18+
1719

1820
LOCAL_REMOTE_ENV = 'GOOGLE_CLOUD_TESTING_REMOTE'
1921
LOCAL_BRANCH_ENV = 'GOOGLE_CLOUD_TESTING_BRANCH'
22+
IN_TRAVIS_ENV = 'TRAVIS'
23+
TRAVIS_PR_ENV = 'TRAVIS_PULL_REQUEST'
24+
TRAVIS_BRANCH_ENV = 'TRAVIS_BRANCH'
25+
26+
27+
def in_travis():
28+
"""Detect if we are running in Travis.
29+
30+
.. _Travis env docs: https://docs.travis-ci.com/user/\
31+
environment-variables\
32+
#Default-Environment-Variables
33+
34+
See `Travis env docs`_.
35+
36+
:rtype: bool
37+
:returns: Flag indicating if we are running on Travis.
38+
"""
39+
return os.getenv(IN_TRAVIS_ENV) == 'true'
40+
41+
42+
def in_travis_pr():
43+
"""Detect if we are running in a pull request on Travis.
44+
45+
.. _Travis env docs: https://docs.travis-ci.com/user/\
46+
environment-variables\
47+
#Default-Environment-Variables
48+
49+
See `Travis env docs`_.
50+
51+
.. note::
52+
53+
This assumes we already know we are running in Travis.
54+
55+
:rtype: bool
56+
:returns: Flag indicating if we are in a pull request on Travis.
57+
"""
58+
# NOTE: We're a little extra cautious and make sure that the
59+
# PR environment variable is an integer.
60+
try:
61+
int(os.getenv(TRAVIS_PR_ENV, ''))
62+
return True
63+
except ValueError:
64+
return False
65+
66+
67+
def travis_branch():
68+
"""Get the current branch of the PR.
69+
70+
.. _Travis env docs: https://docs.travis-ci.com/user/\
71+
environment-variables\
72+
#Default-Environment-Variables
73+
74+
See `Travis env docs`_.
75+
76+
.. note::
77+
78+
This assumes we already know we are running in Travis
79+
during a PR.
80+
81+
:rtype: str
82+
:returns: The name of the branch the current pull request is
83+
changed against.
84+
"""
85+
return os.getenv(TRAVIS_BRANCH_ENV)

0 commit comments

Comments
 (0)