Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c554eae

Browse files
authored
Merge pull request #2481 from dhermes/separate-travis-pieces
Moving Travis helpers out of run_pylint into shared module.
2 parents 900923b + aa36723 commit c554eae

File tree

4 files changed

+110
-19
lines changed

4 files changed

+110
-19
lines changed

CONTRIBUTING.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ Coding Style
151151
- In order to make ``tox -e lint`` run faster, you can set some environment
152152
variables::
153153

154-
export GOOGLE_CLOUD_REMOTE_FOR_LINT="upstream"
155-
export GOOGLE_CLOUD_BRANCH_FOR_LINT="master"
154+
export GOOGLE_CLOUD_TESTING_REMOTE="upstream"
155+
export GOOGLE_CLOUD_TESTING_BRANCH="master"
156156

157157
By doing this, you are specifying the location of the most up-to-date
158158
version of ``google-cloud-python``. The the suggested remote name ``upstream``

scripts/run_pylint.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
import subprocess
3030
import sys
3131

32+
from script_utils import LOCAL_BRANCH_ENV
33+
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
37+
3238

3339
IGNORED_DIRECTORIES = [
3440
os.path.join('bigtable', 'google', 'cloud', 'bigtable', '_generated'),
@@ -143,14 +149,14 @@ def get_files_for_linting(allow_limited=True):
143149
against for changed files. (This requires ``allow_limited=True``.)
144150
145151
To speed up linting on Travis pull requests against master, we manually
146-
set the diff base to origin/master. We don't do this on non-pull requests
147-
since origin/master will be equivalent to the currently checked out code.
148-
One could potentially use ${TRAVIS_COMMIT_RANGE} to find a diff base but
149-
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.
150156
151-
To allow faster local ``tox`` runs, the environment variables
152-
``GOOGLE_CLOUD_REMOTE_FOR_LINT`` and ``GOOGLE_CLOUD_BRANCH_FOR_LINT`` can
153-
be set to specify a remote branch to diff against.
157+
To allow faster local ``tox`` runs, the local remote and local branch
158+
environment variables can be set to specify a remote branch to diff
159+
against.
154160
155161
:type allow_limited: bool
156162
:param allow_limited: Boolean indicating if a reduced set of files can
@@ -161,15 +167,15 @@ def get_files_for_linting(allow_limited=True):
161167
linted.
162168
"""
163169
diff_base = None
164-
if (os.getenv('TRAVIS_BRANCH') == 'master' and
165-
os.getenv('TRAVIS_PULL_REQUEST') != 'false'):
166-
# In the case of a pull request into master, we want to
167-
# diff against HEAD in master.
168-
diff_base = 'origin/master'
169-
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:
170176
# Only allow specified remote and branch in local dev.
171-
remote = os.getenv('GOOGLE_CLOUD_REMOTE_FOR_LINT')
172-
branch = os.getenv('GOOGLE_CLOUD_BRANCH_FOR_LINT')
177+
remote = os.getenv(LOCAL_REMOTE_ENV)
178+
branch = os.getenv(LOCAL_BRANCH_ENV)
173179
if remote is not None and branch is not None:
174180
diff_base = '%s/%s' % (remote, branch)
175181

scripts/script_utils.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Common helpers for testing scripts."""
16+
17+
import os
18+
19+
20+
LOCAL_REMOTE_ENV = 'GOOGLE_CLOUD_TESTING_REMOTE'
21+
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)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export GOOGLE_APPLICATION_CREDENTIALS="app_credentials.json.sample"
2-
export GOOGLE_CLOUD_REMOTE_FOR_LINT="upstream"
3-
export GOOGLE_CLOUD_BRANCH_FOR_LINT="master"
2+
export GOOGLE_CLOUD_TESTING_REMOTE="upstream"
3+
export GOOGLE_CLOUD_TESTING_BRANCH="master"
44
export GOOGLE_CLOUD_TESTS_API_KEY="abcd1234"

0 commit comments

Comments
 (0)