Skip to content

Commit 160cd78

Browse files
committed
Simplify noxfile and add version control.
1 parent 004aca1 commit 160cd78

File tree

4 files changed

+74
-446
lines changed

4 files changed

+74
-446
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,13 @@ for file in **/requirements.txt; do
9292

9393
# If no local noxfile exists, copy the one from root
9494
if [[ ! -f "noxfile.py" ]]; then
95-
cp "$ROOT/noxfile-template.py" "./noxfile.py"
96-
echo -e "\n Using noxfile from project root. \n"
95+
PARENT_DIR=$(cd ../ && pwd)
96+
while [[ "$PARENT_DIR" != "$ROOT" && ! -f "$PARENT_DIR/noxfile-template.py" ]];
97+
do
98+
PARENT_DIR=$(dirname "$PARENT_DIR")
99+
done
100+
cp "$PARENT_DIR/noxfile-template.py" "./noxfile.py"
101+
echo -e "\n Using noxfile-template from parent folder ($PARENT_DIR). \n"
97102
fi
98103

99104
# Use nox to execute the tests for the project.

noxfile-template.py

Lines changed: 67 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,30 @@
1414

1515
from __future__ import print_function
1616

17-
import fnmatch
1817
import os
1918
from pathlib import Path
20-
import tempfile
2119

2220
import nox
2321

24-
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
25-
p = Path(os.getcwd())
26-
for i in range(10):
27-
if p is None:
28-
raise Exception("Unable to detect repository root.")
29-
if Path(p / ".git").exists():
30-
REPO_ROOT = str(p)
31-
break
32-
p = p.parent
3322

34-
#
35-
# Helpers and utility functions
36-
#
37-
38-
39-
def _list_files(folder, pattern):
40-
"""Lists all files below the given folder that match the pattern."""
41-
for root, folders, files in os.walk(folder):
42-
for filename in files:
43-
if fnmatch.fnmatch(filename, pattern):
44-
yield os.path.join(root, filename)
23+
# DO NOT EDIT - automatically generated.
24+
# All versions used to tested samples.
25+
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
4526

27+
# Any default versions that should be ignored.
28+
IGNORED_VERSIONS = ["2.7"]
4629

47-
def _collect_dirs(
48-
start_dir,
49-
blacklist=set(["conftest.py", "noxfile.py", "lib", "third_party"]),
50-
suffix="requirements.txt",
51-
recurse_further=False,
52-
):
53-
"""Recursively collects a list of dirs that contain a file matching the
54-
given suffix.
30+
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
5531

56-
This works by listing the contents of directories and finding
57-
directories that have `"requirements.text` files.
58-
"""
59-
# Collect all the directories that have tests in them.
60-
for parent, subdirs, files in os.walk(start_dir):
61-
if "./." in parent:
62-
continue # Skip top-level dotfiles
63-
elif any(f for f in files if f.endswith(suffix) and f not in blacklist):
64-
# Don't recurse further for tests, since py.test will do that.
65-
if not recurse_further:
66-
del subdirs[:]
67-
# This dir has desired files in it. yield it.
68-
yield parent
69-
else:
70-
# Filter out dirs we don't want to recurse into
71-
subdirs[:] = [s for s in subdirs if s[0].isalpha() and s not in blacklist]
32+
#
33+
# Style Checks
34+
#
7235

7336

37+
# Ignore I202 "Additional newline in a section of imports." to accommodate
38+
# region tags in import blocks. Since we specify an explicit ignore, we also
39+
# have to explicitly ignore the list of default ignores:
40+
# `E121,E123,E126,E226,E24,E704,W503,W504` as shown by `flake8 --help`.
7441
def _determine_local_import_names(start_dir):
7542
"""Determines all import names that should be considered "local".
7643
@@ -87,75 +54,46 @@ def _determine_local_import_names(start_dir):
8754
]
8855

8956

90-
#
91-
# App Engine specific helpers
92-
#
93-
94-
95-
_GAE_ROOT = os.environ.get("GAE_ROOT")
96-
if _GAE_ROOT is None:
97-
_GAE_ROOT = tempfile.mkdtemp()
98-
99-
100-
def _setup_appengine_sdk(session):
101-
"""Installs the App Engine SDK, if needed."""
102-
session.env["GAE_SDK_PATH"] = os.path.join(_GAE_ROOT, "google_appengine")
103-
session.run("gcp-devrel-py-tools", "download-appengine-sdk", _GAE_ROOT)
104-
105-
106-
#
107-
# Test sessions
108-
#
109-
110-
111-
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
112-
113-
# Ignore I202 "Additional newline in a section of imports." to accommodate
114-
# region tags in import blocks. Since we specify an explicit ignore, we also
115-
# have to explicitly ignore the list of default ignores:
116-
# `E121,E123,E126,E226,E24,E704,W503,W504` as shown by `flake8 --help`.
11757
FLAKE8_COMMON_ARGS = [
11858
"--show-source",
119-
"--builtin",
120-
"gettext",
121-
"--max-complexity",
122-
"20",
123-
"--import-order-style",
124-
"google",
125-
"--exclude",
126-
".nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py",
127-
"--ignore=E121,E123,E126,E203, E226,E24,E266,E501,E704,W503,W504,I100,I201,I202",
59+
"--builtin='gettext'",
60+
"--max-complexity=20",
61+
"--import-order-style=google",
62+
"--exclude='.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py'",
63+
"--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I100,I201,I202",
12864
"--max-line-length=88",
12965
]
13066

13167

132-
# Collect sample directories.
133-
ALL_TESTED_SAMPLES = sorted(list(_collect_dirs(".")))
68+
@nox.session
69+
def lint(session):
70+
session.install("flake8", "flake8-import-order")
13471

135-
GAE_STANDARD_SAMPLES = [
136-
sample
137-
for sample in ALL_TESTED_SAMPLES
138-
if str(Path(sample).absolute().relative_to(REPO_ROOT)).startswith(
139-
"appengine/standard/"
140-
)
141-
]
72+
local_names = _determine_local_import_names(".")
73+
args = FLAKE8_COMMON_ARGS + [
74+
"--application-import-names",
75+
",".join(local_names),
76+
".",
77+
]
78+
session.run("flake8", *args)
14279

143-
PY2_ONLY_SAMPLES = GAE_STANDARD_SAMPLES
14480

145-
NON_GAE_STANDARD_SAMPLES_PY3 = sorted(
146-
list(set(ALL_TESTED_SAMPLES) - set(GAE_STANDARD_SAMPLES))
147-
)
81+
#
82+
# Sample Tests
83+
#
14884

14985

150-
def _session_tests(session, sample, post_install=None):
151-
"""Runs py.test for a particular sample."""
152-
session.install("-r", REPO_ROOT + "/testing/requirements.txt")
86+
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
15387

154-
session.chdir(sample)
15588

89+
def _session_tests(session, post_install=None):
90+
"""Runs py.test for a particular project."""
15691
if os.path.exists("requirements.txt"):
15792
session.install("-r", "requirements.txt")
15893

94+
if os.path.exists("requirements-testing.txt"):
95+
session.install("-r", "requirements-testing.txt")
96+
15997
if post_install:
16098
post_install(session)
16199

@@ -169,50 +107,46 @@ def _session_tests(session, sample, post_install=None):
169107
)
170108

171109

172-
@nox.session(python="2.7")
173-
@nox.parametrize("sample", PY2_ONLY_SAMPLES)
174-
def gae(session, sample):
175-
"""Runs py.test for an App Engine standard sample."""
176-
177-
# Create a lib directory if needed, otherwise the App Engine vendor library
178-
# will complain.
179-
if not os.path.isdir(os.path.join(sample, "lib")):
180-
os.mkdir(os.path.join(sample, "lib"))
110+
@nox.session(python=ALL_VERSIONS)
111+
def py(session):
112+
"""Runs py.test for a sample using the specified version of Python."""
113+
if session.python in TESTED_VERSIONS:
114+
_session_tests(session)
115+
else:
116+
print("SUCCESS: {} tests are disable for this sample.".format(session.python))
181117

182-
_session_tests(session, sample, _setup_appengine_sdk)
183118

119+
#
120+
# Readmegen
121+
#
184122

185-
@nox.session(python=["3.6", "3.7"])
186-
@nox.parametrize("sample", NON_GAE_STANDARD_SAMPLES_PY3)
187-
def py3(session, sample):
188-
"""Runs py.test for a sample using Python 3.x"""
189-
_session_tests(session, sample)
190123

124+
def _get_repo_root():
125+
""" Returns the root folder of the project. """
126+
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
127+
p = Path(os.getcwd())
128+
for i in range(10):
129+
if p is None:
130+
break
131+
if Path(p / ".git").exists():
132+
return str(p)
133+
p = p.parent
134+
raise Exception("Unable to detect repository root.")
191135

192-
@nox.session(python="3.6")
193-
def lint(session):
194-
session.install("flake8", "flake8-import-order")
195-
196-
local_names = _determine_local_import_names(".")
197-
args = FLAKE8_COMMON_ARGS + [
198-
"--application-import-names",
199-
",".join(local_names),
200-
".",
201-
]
202-
session.run("flake8", *args)
203136

204-
205-
SAMPLES_WITH_GENERATED_READMES = sorted(list(_collect_dirs(".", suffix=".rst.in")))
137+
GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")])
206138

207139

208140
@nox.session
209-
@nox.parametrize("sample", SAMPLES_WITH_GENERATED_READMES)
210-
def readmegen(session, sample):
141+
@nox.parametrize("path", GENERATED_READMES)
142+
def readmegen(session, path):
211143
"""(Re-)generates the readme for a sample."""
212144
session.install("jinja2", "pyyaml")
213145

214-
if os.path.exists(os.path.join(sample, "requirements.txt")):
215-
session.install("-r", os.path.join(sample, "requirements.txt"))
146+
if os.path.exists(os.path.join(path, "requirements.txt")):
147+
session.install("-r", os.path.join(path, "requirements.txt"))
216148

217-
in_file = os.path.join(sample, "README.rst.in")
218-
session.run("python", REPO_ROOT + "/scripts/readme-gen/readme_gen.py", in_file)
149+
in_file = os.path.join(path, "README.rst.in")
150+
session.run(
151+
"python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file
152+
)

0 commit comments

Comments
 (0)