Skip to content

Commit ce66b22

Browse files
authored
Add templates for flake8, coveragerc, noxfile, and black. (#6642)
1 parent 59d2883 commit ce66b22

File tree

5 files changed

+118
-76
lines changed

5 files changed

+118
-76
lines changed

packages/google-cloud-language/.coveragerc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ exclude_lines =
99
pragma: NO COVER
1010
# Ignore debug-only repr
1111
def __repr__
12+
# Ignore abstract methods
13+
raise NotImplementedError
1214
omit =
13-
*/gapic/*
14-
*/proto/*
15+
*/gapic/*.py
16+
*/proto/*.py
17+
*/google-cloud-python/core/*.py
18+
*/site-packages/*.py

packages/google-cloud-language/.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[flake8]
2+
ignore = E203, E266, E501, W503
23
exclude =
34
# Exclude generated code.
45
**/proto/**
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include README.rst LICENSE
22
recursive-include google *.json *.proto
33
recursive-include tests *
4-
global-exclude *.pyc __pycache__
4+
global-exclude *.py[co]
5+
global-exclude __pycache__
Lines changed: 90 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Copyright 2016 Google LLC
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2018 Google LLC
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
46
# you may not use this file except in compliance with the License.
57
# You may obtain a copy of the License at
68
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
9+
# https://www.apache.org/licenses/LICENSE-2.0
810
#
911
# Unless required by applicable law or agreed to in writing, software
1012
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,102 +15,126 @@
1315
# limitations under the License.
1416

1517
from __future__ import absolute_import
16-
1718
import os
1819

1920
import nox
2021

2122

22-
LOCAL_DEPS = (
23-
os.path.join('..', 'api_core'),
24-
os.path.join('..', 'core'),
25-
)
23+
LOCAL_DEPS = (os.path.join("..", "api_core"), os.path.join("..", "core"))
2624

25+
@nox.session(python="3.7")
26+
def blacken(session):
27+
"""Run black.
2728
28-
def default(session):
29-
"""Default unit test session.
29+
Format code to uniform standard.
30+
"""
31+
session.install("black")
32+
session.run(
33+
"black",
34+
"google",
35+
"tests",
36+
"docs",
37+
"--exclude",
38+
".*/proto/.*|.*/gapic/.*|.*/.*_pb2.py",
39+
)
40+
41+
42+
@nox.session(python="3.7")
43+
def lint(session):
44+
"""Run linters.
3045
31-
This is intended to be run **without** an interpreter set, so
32-
that the current ``python`` (on the ``PATH``) or the version of
33-
Python corresponding to the ``nox`` binary the ``PATH`` can
34-
run the tests.
46+
Returns a failure if the linters find linting errors or sufficiently
47+
serious code quality issues.
3548
"""
36-
# Install all test dependencies, then install local packages in-place.
37-
session.install('mock', 'pytest', 'pytest-cov')
49+
session.install("flake8", "black", *LOCAL_DEPS)
50+
session.run(
51+
"black",
52+
"--check",
53+
"google",
54+
"tests",
55+
"docs",
56+
"--exclude",
57+
".*/proto/.*|.*/gapic/.*|.*/.*_pb2.py",
58+
)
59+
session.run("flake8", "google", "tests")
60+
61+
62+
@nox.session(python="3.7")
63+
def lint_setup_py(session):
64+
"""Verify that setup.py is valid (including RST check)."""
65+
session.install("docutils", "pygments")
66+
session.run("python", "setup.py", "check", "--restructuredtext", "--strict")
67+
68+
69+
def default(session):
70+
# Install all test dependencies, then install this package in-place.
71+
session.install("mock", "pytest", "pytest-cov")
3872
for local_dep in LOCAL_DEPS:
39-
session.install('-e', local_dep)
40-
session.install('-e', '.')
73+
session.install("-e", local_dep)
74+
session.install("-e", ".")
4175

4276
# Run py.test against the unit tests.
4377
session.run(
44-
'py.test', '--quiet',
45-
'--cov=google.cloud.language_v1',
46-
'--cov=google.cloud.language_v1beta2',
47-
'--cov-append',
48-
'--cov-config=.coveragerc',
49-
'--cov-report=',
50-
'--cov-fail-under=97',
51-
'tests/unit',
52-
*session.posargs
78+
"py.test",
79+
"--quiet",
80+
"--cov=google.cloud",
81+
"--cov=tests.unit",
82+
"--cov-append",
83+
"--cov-config=.coveragerc",
84+
"--cov-report=",
85+
"--cov-fail-under=97",
86+
os.path.join("tests", "unit"),
87+
*session.posargs,
5388
)
5489

5590

56-
@nox.session(python=['2.7', '3.5', '3.6', '3.7'])
91+
@nox.session(python=["2.7", "3.5", "3.6", "3.7"])
5792
def unit(session):
5893
"""Run the unit test suite."""
5994
default(session)
6095

6196

62-
@nox.session(python=['2.7', '3.6'])
97+
@nox.session(python=["2.7", "3.7"])
6398
def system(session):
6499
"""Run the system test suite."""
65-
66-
# Sanity check: Only run system tests if the environment variable is set.
67-
if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', ''):
68-
session.skip('Credentials must be set via environment variable.')
100+
system_test_path = os.path.join("tests", "system.py")
101+
system_test_folder_path = os.path.join("tests", "system")
102+
# Sanity check: Only run tests if the environment variable is set.
103+
if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
104+
session.skip("Credentials must be set via environment variable")
105+
106+
system_test_exists = os.path.exists(system_test_path)
107+
system_test_folder_exists = os.path.exists(system_test_folder_path)
108+
# Sanity check: only run tests if found.
109+
if not system_test_exists and not system_test_folder_exists:
110+
session.skip("System tests were not found")
69111

70112
# Use pre-release gRPC for system tests.
71-
session.install('--pre', 'grpcio')
113+
session.install("--pre", "grpcio")
72114

73-
# Install all test dependencies, then install local packages in-place.
74-
session.install('mock', 'pytest')
115+
# Install all test dependencies, then install this package into the
116+
# virtualenv's dist-packages.
117+
session.install("mock", "pytest")
75118
for local_dep in LOCAL_DEPS:
76-
session.install('-e', local_dep)
77-
session.install('-e', '../storage/')
78-
session.install('-e', '../test_utils/')
79-
session.install('-e', '.')
119+
session.install("-e", local_dep)
120+
session.install("-e", "../test_utils/")
121+
session.install("-e", ".")
80122

81123
# Run py.test against the system tests.
82-
session.run('py.test', '--quiet', 'tests/system/')
83-
84-
85-
@nox.session(python='3.6')
86-
def lint(session):
87-
"""Run linters.
88-
89-
Returns a failure if the linters find linting errors or sufficiently
90-
serious code quality issues.
91-
"""
92-
session.install('flake8', *LOCAL_DEPS)
93-
session.install('.')
94-
session.run('flake8', 'google', 'tests')
124+
if system_test_exists:
125+
session.run("py.test", "--quiet", system_test_path, *session.posargs)
126+
if system_test_folder_exists:
127+
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)
95128

96129

97-
@nox.session(python='3.6')
98-
def lint_setup_py(session):
99-
"""Verify that setup.py is valid (including RST check)."""
100-
session.install('docutils', 'Pygments')
101-
session.run(
102-
'python', 'setup.py', 'check', '--restructuredtext', '--strict')
103-
104-
105-
@nox.session(python='3.6')
130+
@nox.session(python="3.7")
106131
def cover(session):
107132
"""Run the final coverage report.
108133
109134
This outputs the coverage report aggregating coverage from the unit
110135
test runs (not system test runs), and then erases coverage data.
111136
"""
112-
session.install('coverage', 'pytest-cov')
113-
session.run('coverage', 'report', '--show-missing', '--fail-under=100')
114-
session.run('coverage', 'erase')
137+
session.install("coverage", "pytest-cov")
138+
session.run("coverage", "report", "--show-missing", "--fail-under=100")
139+
140+
session.run("coverage", "erase")

packages/google-cloud-language/synth.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,28 @@
1818
from synthtool import gcp
1919

2020
gapic = gcp.GAPICGenerator()
21-
22-
versions = ['v1beta2', 'v1']
21+
common = gcp.CommonTemplates()
22+
versions = ["v1beta2", "v1"]
2323

2424

25+
# ----------------------------------------------------------------------------
26+
# Generate language GAPIC layer
27+
# ----------------------------------------------------------------------------
2528
for version in versions:
2629
library = gapic.py_library(
27-
'language',
30+
"language",
2831
version,
29-
config_path=f'/google/cloud/language/artman_language_{version}.yaml',
30-
artman_output_name=f'language-{version}')
32+
config_path=f"/google/cloud/language/artman_language_{version}.yaml",
33+
artman_output_name=f"language-{version}",
34+
)
35+
36+
s.move(library / f"google/cloud/language_{version}/proto")
37+
s.move(library / f"google/cloud/language_{version}/gapic")
38+
s.move(library / f"tests/unit/gapic/{version}")
39+
s.move(library / f"tests/system/gapic/{version}")
3140

32-
s.move(library / f'google/cloud/language_{version}/proto')
33-
s.move(library / f'google/cloud/language_{version}/gapic')
34-
s.move(library / f'tests/unit/gapic/{version}')
35-
s.move(library / f'tests/system/gapic/{version}')
41+
# ----------------------------------------------------------------------------
42+
# Add templated files
43+
# ----------------------------------------------------------------------------
44+
templated_files = common.py_library(unit_cov_level=87, cov_level=87)
45+
s.move(templated_files)

0 commit comments

Comments
 (0)