Skip to content

Commit 53bd9c9

Browse files
yoshi-automationdandhlee
authored andcommitted
chore: update testing and docs config (#89)
* changes without context autosynth cannot find the source of changes triggered by earlier changes in this repository, or by version upgrades to tools such as linters. * docs: add proto-plus to intersphinx mapping Source-Author: Tim Swast <[email protected]> Source-Date: Tue Oct 27 12:01:14 2020 -0500 Source-Repo: googleapis/synthtool Source-Sha: ea52b8a0bd560f72f376efcf45197fb7c8869120 Source-Link: googleapis/synthtool@ea52b8a * fix(python_library): fix external unit test dependencies I recently submitted https://github.com/googleapis/synthtool/pull/811/files, allowing external dependencies for unit tests. This fixes a small missing comma bug Source-Author: Daniel Sanche <[email protected]> Source-Date: Thu Oct 29 16:58:01 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 6542bd723403513626f61642fc02ddca528409aa Source-Link: googleapis/synthtool@6542bd7 * chore: add type hint check Source-Author: Leah E. Cole <[email protected]> Source-Date: Wed Nov 4 17:36:32 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: 3d3e94c4e02370f307a9a200b0c743c3d8d19f29 Source-Link: googleapis/synthtool@3d3e94c * chore: add blacken to template Source-Author: Leah E. Cole <[email protected]> Source-Date: Thu Nov 5 15:22:03 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: 1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b Source-Link: googleapis/synthtool@1f1148d * fix: address lint issues Source-Author: Leah E. Cole <[email protected]> Source-Date: Thu Nov 12 11:30:49 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: e89175cf074dccc4babb4eca66ae913696e47a71 Source-Link: googleapis/synthtool@e89175c * docs(python): update intersphinx for grpc and auth * docs(python): update intersphinx for grpc and auth * use https for python intersphinx Co-authored-by: Tim Swast <[email protected]> Source-Author: Bu Sun Kim <[email protected]> Source-Date: Wed Nov 18 14:37:25 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 9a7d9fbb7045c34c9d3d22c1ff766eeae51f04c9 Source-Link: googleapis/synthtool@9a7d9fb * docs(python): fix intersphinx link for google-auth Source-Author: Bu Sun Kim <[email protected]> Source-Date: Thu Nov 19 10:16:05 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: a073c873f3928c561bdf87fdfbf1d081d1998984 Source-Link: googleapis/synthtool@a073c87 * chore: add config / docs for 'pre-commit' support Source-Author: Tres Seaver <[email protected]> Source-Date: Tue Dec 1 16:01:20 2020 -0500 Source-Repo: googleapis/synthtool Source-Sha: 32af6da519a6b042e3da62008e2a75e991efb6b4 Source-Link: googleapis/synthtool@32af6da * chore(deps): update precommit hook pre-commit/pre-commit-hooks to v3.3.0 Source-Author: WhiteSource Renovate <[email protected]> Source-Date: Wed Dec 2 17:18:24 2020 +0100 Source-Repo: googleapis/synthtool Source-Sha: 69629b64b83c6421d616be2b8e11795738ec8a6c Source-Link: googleapis/synthtool@69629b6 * test(python): give filesystem paths to pytest-cov https://pytest-cov.readthedocs.io/en/latest/config.html The pytest-cov docs seem to suggest a filesystem path is expected. Source-Author: Bu Sun Kim <[email protected]> Source-Date: Wed Dec 2 09:28:04 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: f94318521f63085b9ccb43d42af89f153fb39f15 Source-Link: googleapis/synthtool@f943185 * chore: update noxfile.py.j2 * Update noxfile.py.j2 add changes from @glasnt to the template template to ensure that enforcing type hinting doesn't fail for repos with the sample noxfile (aka all samples repos) See https://github.com/GoogleCloudPlatform/python-docs-samples/pull/4869/files for context * fix typo Source-Author: Leah E. Cole <[email protected]> Source-Date: Thu Dec 3 13:44:30 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: 18c5dbdb4ac8cf75d4d8174e7b4558f48e76f8a1 Source-Link: googleapis/synthtool@18c5dbd * chore(deps): update precommit hook pre-commit/pre-commit-hooks to v3.4.0 Co-authored-by: Tres Seaver <[email protected]> Source-Author: WhiteSource Renovate <[email protected]> Source-Date: Wed Dec 16 18:13:24 2020 +0100 Source-Repo: googleapis/synthtool Source-Sha: aa255b15d52b6d8950cca48cfdf58f7d27a60c8a Source-Link: googleapis/synthtool@aa255b1 * docs(python): document adding Python 3.9 support, dropping 3.5 support Closes #787 Source-Author: Tres Seaver <[email protected]> Source-Date: Thu Dec 17 16:08:02 2020 -0500 Source-Repo: googleapis/synthtool Source-Sha: b670a77a454f415d247907908e8ee7943e06d718 Source-Link: googleapis/synthtool@b670a77 * chore: exclude `.nox` directories from linting The samples tests create `.nox` directories with all dependencies installed. These directories should be excluded from linting. I've tested this change locally, and it significantly speeds up linting on my machine. Source-Author: Tim Swast <[email protected]> Source-Date: Tue Dec 22 13:04:04 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 373861061648b5fe5e0ac4f8a38b32d639ee93e4 Source-Link: googleapis/synthtool@3738610
1 parent 248857f commit 53bd9c9

File tree

2 files changed

+77
-37
lines changed

2 files changed

+77
-37
lines changed

translation/samples/snippets/hybrid_glossaries/noxfile.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
from pathlib import Path
1919
import sys
20+
from typing import Callable, Dict, List, Optional
2021

2122
import nox
2223

@@ -37,22 +38,28 @@
3738

3839
TEST_CONFIG = {
3940
# You can opt out from the test for specific Python versions.
40-
"ignored_versions": ["2.7"],
41+
'ignored_versions': ["2.7"],
42+
43+
# Old samples are opted out of enforcing Python type hints
44+
# All new samples should feature them
45+
'enforce_type_hints': False,
46+
4147
# An envvar key for determining the project id to use. Change it
4248
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4349
# build specific Cloud project. You can also use your own string
4450
# to use your own Cloud project.
45-
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
51+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
4652
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
53+
4754
# A dictionary you want to inject into your test. Don't put any
4855
# secrets here. These values will override predefined values.
49-
"envs": {},
56+
'envs': {},
5057
}
5158

5259

5360
try:
5461
# Ensure we can import noxfile_config in the project's directory.
55-
sys.path.append(".")
62+
sys.path.append('.')
5663
from noxfile_config import TEST_CONFIG_OVERRIDE
5764
except ImportError as e:
5865
print("No user noxfile_config found: detail: {}".format(e))
@@ -62,17 +69,17 @@
6269
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)
6370

6471

65-
def get_pytest_env_vars():
72+
def get_pytest_env_vars() -> Dict[str, str]:
6673
"""Returns a dict for pytest invocation."""
6774
ret = {}
6875

6976
# Override the GCLOUD_PROJECT and the alias.
70-
env_key = TEST_CONFIG["gcloud_project_env"]
77+
env_key = TEST_CONFIG['gcloud_project_env']
7178
# This should error out if not set.
72-
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
79+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
7380

7481
# Apply user supplied envs.
75-
ret.update(TEST_CONFIG["envs"])
82+
ret.update(TEST_CONFIG['envs'])
7683
return ret
7784

7885

@@ -81,7 +88,7 @@ def get_pytest_env_vars():
8188
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
8289

8390
# Any default versions that should be ignored.
84-
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
91+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
8592

8693
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
8794

@@ -91,7 +98,7 @@ def get_pytest_env_vars():
9198
#
9299

93100

94-
def _determine_local_import_names(start_dir):
101+
def _determine_local_import_names(start_dir: str) -> List[str]:
95102
"""Determines all import names that should be considered "local".
96103
97104
This is used when running the linter to insure that import order is
@@ -129,17 +136,30 @@ def _determine_local_import_names(start_dir):
129136

130137

131138
@nox.session
132-
def lint(session):
133-
session.install("flake8", "flake8-import-order")
139+
def lint(session: nox.sessions.Session) -> None:
140+
if not TEST_CONFIG['enforce_type_hints']:
141+
session.install("flake8", "flake8-import-order")
142+
else:
143+
session.install("flake8", "flake8-import-order", "flake8-annotations")
134144

135145
local_names = _determine_local_import_names(".")
136146
args = FLAKE8_COMMON_ARGS + [
137147
"--application-import-names",
138148
",".join(local_names),
139-
".",
149+
"."
140150
]
141151
session.run("flake8", *args)
152+
#
153+
# Black
154+
#
155+
156+
157+
@nox.session
158+
def blacken(session: nox.sessions.Session) -> None:
159+
session.install("black")
160+
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
142161

162+
session.run("black", *python_files)
143163

144164
#
145165
# Sample Tests
@@ -149,7 +169,7 @@ def lint(session):
149169
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
150170

151171

152-
def _session_tests(session, post_install=None):
172+
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
153173
"""Runs py.test for a particular project."""
154174
if os.path.exists("requirements.txt"):
155175
session.install("-r", "requirements.txt")
@@ -175,22 +195,22 @@ def _session_tests(session, post_install=None):
175195

176196

177197
@nox.session(python=ALL_VERSIONS)
178-
def py(session):
198+
def py(session: nox.sessions.Session) -> None:
179199
"""Runs py.test for a sample using the specified version of Python."""
180200
if session.python in TESTED_VERSIONS:
181201
_session_tests(session)
182202
else:
183-
session.skip(
184-
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
185-
)
203+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
204+
session.python
205+
))
186206

187207

188208
#
189209
# Readmegen
190210
#
191211

192212

193-
def _get_repo_root():
213+
def _get_repo_root() -> Optional[str]:
194214
""" Returns the root folder of the project. """
195215
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
196216
p = Path(os.getcwd())
@@ -213,7 +233,7 @@ def _get_repo_root():
213233

214234
@nox.session
215235
@nox.parametrize("path", GENERATED_READMES)
216-
def readmegen(session, path):
236+
def readmegen(session: nox.sessions.Session, path: str) -> None:
217237
"""(Re-)generates the readme for a sample."""
218238
session.install("jinja2", "pyyaml")
219239
dir_ = os.path.dirname(path)

translation/samples/snippets/noxfile.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
from pathlib import Path
1919
import sys
20+
from typing import Callable, Dict, List, Optional
2021

2122
import nox
2223

@@ -37,22 +38,28 @@
3738

3839
TEST_CONFIG = {
3940
# You can opt out from the test for specific Python versions.
40-
"ignored_versions": ["2.7"],
41+
'ignored_versions': ["2.7"],
42+
43+
# Old samples are opted out of enforcing Python type hints
44+
# All new samples should feature them
45+
'enforce_type_hints': False,
46+
4147
# An envvar key for determining the project id to use. Change it
4248
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4349
# build specific Cloud project. You can also use your own string
4450
# to use your own Cloud project.
45-
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
51+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
4652
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
53+
4754
# A dictionary you want to inject into your test. Don't put any
4855
# secrets here. These values will override predefined values.
49-
"envs": {},
56+
'envs': {},
5057
}
5158

5259

5360
try:
5461
# Ensure we can import noxfile_config in the project's directory.
55-
sys.path.append(".")
62+
sys.path.append('.')
5663
from noxfile_config import TEST_CONFIG_OVERRIDE
5764
except ImportError as e:
5865
print("No user noxfile_config found: detail: {}".format(e))
@@ -62,17 +69,17 @@
6269
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)
6370

6471

65-
def get_pytest_env_vars():
72+
def get_pytest_env_vars() -> Dict[str, str]:
6673
"""Returns a dict for pytest invocation."""
6774
ret = {}
6875

6976
# Override the GCLOUD_PROJECT and the alias.
70-
env_key = TEST_CONFIG["gcloud_project_env"]
77+
env_key = TEST_CONFIG['gcloud_project_env']
7178
# This should error out if not set.
72-
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
79+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
7380

7481
# Apply user supplied envs.
75-
ret.update(TEST_CONFIG["envs"])
82+
ret.update(TEST_CONFIG['envs'])
7683
return ret
7784

7885

@@ -81,7 +88,7 @@ def get_pytest_env_vars():
8188
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
8289

8390
# Any default versions that should be ignored.
84-
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
91+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
8592

8693
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
8794

@@ -91,7 +98,7 @@ def get_pytest_env_vars():
9198
#
9299

93100

94-
def _determine_local_import_names(start_dir):
101+
def _determine_local_import_names(start_dir: str) -> List[str]:
95102
"""Determines all import names that should be considered "local".
96103
97104
This is used when running the linter to insure that import order is
@@ -129,17 +136,30 @@ def _determine_local_import_names(start_dir):
129136

130137

131138
@nox.session
132-
def lint(session):
133-
session.install("flake8", "flake8-import-order")
139+
def lint(session: nox.sessions.Session) -> None:
140+
if not TEST_CONFIG['enforce_type_hints']:
141+
session.install("flake8", "flake8-import-order")
142+
else:
143+
session.install("flake8", "flake8-import-order", "flake8-annotations")
134144

135145
local_names = _determine_local_import_names(".")
136146
args = FLAKE8_COMMON_ARGS + [
137147
"--application-import-names",
138148
",".join(local_names),
139-
".",
149+
"."
140150
]
141151
session.run("flake8", *args)
152+
#
153+
# Black
154+
#
155+
156+
157+
@nox.session
158+
def blacken(session: nox.sessions.Session) -> None:
159+
session.install("black")
160+
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
142161

162+
session.run("black", *python_files)
143163

144164
#
145165
# Sample Tests
@@ -149,7 +169,7 @@ def lint(session):
149169
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
150170

151171

152-
def _session_tests(session, post_install=None):
172+
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
153173
"""Runs py.test for a particular project."""
154174
if os.path.exists("requirements.txt"):
155175
session.install("-r", "requirements.txt")
@@ -175,7 +195,7 @@ def _session_tests(session, post_install=None):
175195

176196

177197
@nox.session(python=ALL_VERSIONS)
178-
def py(session):
198+
def py(session: nox.sessions.Session) -> None:
179199
"""Runs py.test for a sample using the specified version of Python."""
180200
if session.python in TESTED_VERSIONS:
181201
_session_tests(session)
@@ -190,7 +210,7 @@ def py(session):
190210
#
191211

192212

193-
def _get_repo_root():
213+
def _get_repo_root() -> Optional[str]:
194214
""" Returns the root folder of the project. """
195215
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
196216
p = Path(os.getcwd())
@@ -213,7 +233,7 @@ def _get_repo_root():
213233

214234
@nox.session
215235
@nox.parametrize("path", GENERATED_READMES)
216-
def readmegen(session, path):
236+
def readmegen(session: nox.sessions.Session, path: str) -> None:
217237
"""(Re-)generates the readme for a sample."""
218238
session.install("jinja2", "pyyaml")
219239
dir_ = os.path.dirname(path)

0 commit comments

Comments
 (0)