1616
1717import os
1818from pathlib import Path
19+ import sys
1920
2021import nox
2122
2223
24+ # WARNING - WARNING - WARNING - WARNING - WARNING
25+ # WARNING - WARNING - WARNING - WARNING - WARNING
26+ # DO NOT EDIT THIS FILE EVER!
27+ # WARNING - WARNING - WARNING - WARNING - WARNING
28+ # WARNING - WARNING - WARNING - WARNING - WARNING
29+
30+ # Copy `noxfile_config.py` to your directory and modify it instead.
31+
32+
33+ # `TEST_CONFIG` dict is a configuration hook that allows users to
34+ # modify the test configurations. The values here should be in sync
35+ # with `noxfile_config.py`. Users will copy `noxfile_config.py` into
36+ # their directory and modify it.
37+
38+ TEST_CONFIG = {
39+ # You can opt out from the test for specific Python versions.
40+ 'ignored_versions' : ["2.7" ],
41+
42+ # Declare optional test sessions you want to opt-in. Currently we
43+ # have the following optional test sessions:
44+ # 'cloud_run' # Test session for Cloud Run application.
45+ 'opt_in_sessions' : [],
46+
47+ # An envvar key for determining the project id to use. Change it
48+ # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
49+ # build specific Cloud project. You can also use your own string
50+ # to use your own Cloud project.
51+ 'gcloud_project_env' : 'GCLOUD_PROJECT' ,
52+ # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
53+
54+ # A dictionary you want to inject into your test. Don't put any
55+ # secrets here. These values will override predefined values.
56+ 'envs' : {},
57+ }
58+
59+
60+ try :
61+ # Ensure we can import noxfile_config in the project's directory.
62+ sys .path .append ('.' )
63+ from noxfile_config import TEST_CONFIG_OVERRIDE
64+ except ImportError as e :
65+ print ("No user noxfile_config found: detail: {}" .format (e ))
66+ TEST_CONFIG_OVERRIDE = {}
67+
68+ # Update the TEST_CONFIG with the user supplied values.
69+ TEST_CONFIG .update (TEST_CONFIG_OVERRIDE )
70+
71+
72+ def get_pytest_env_vars ():
73+ """Returns a dict for pytest invocation."""
74+ ret = {}
75+
76+ # Override the GCLOUD_PROJECT and the alias.
77+ env_key = TEST_CONFIG ['gcloud_project_env' ]
78+ # This should error out if not set.
79+ ret ['GOOGLE_CLOUD_PROJECT' ] = os .environ [env_key ]
80+ ret ['GCLOUD_PROJECT' ] = os .environ [env_key ]
81+
82+ # Apply user supplied envs.
83+ ret .update (TEST_CONFIG ['envs' ])
84+ return ret
85+
86+
2387# DO NOT EDIT - automatically generated.
2488# All versions used to tested samples.
2589ALL_VERSIONS = ["2.7" , "3.6" , "3.7" , "3.8" ]
2690
2791# Any default versions that should be ignored.
28- IGNORED_VERSIONS = [ "2.7" ]
92+ IGNORED_VERSIONS = TEST_CONFIG [ 'ignored_versions' ]
2993
3094TESTED_VERSIONS = sorted ([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS ])
3195
@@ -76,11 +140,25 @@ def lint(session):
76140 session .install ("flake8" , "flake8-import-order" )
77141
78142 local_names = _determine_local_import_names ("." )
79- args = FLAKE8_COMMON_ARGS + [
143+ options = [
80144 "--application-import-names" ,
81- "," .join (local_names ),
82- "." ,
145+ "," .join (local_names )
83146 ]
147+
148+ # We currently look at pytest.ini for flake8 config.
149+ # You can add your own exclude and ignore by using `extend-`
150+ #
151+ # Example config:
152+ # [flake8]
153+ # extend-ignore = I100
154+ # extend-exclude = myapp1,myapp2
155+ if os .path .isfile ("pytest.ini" ):
156+ options += [
157+ "--append-config" ,
158+ "pytest.ini" ,
159+ ]
160+ options .append ("." )
161+ args = FLAKE8_COMMON_ARGS + options
84162 session .run ("flake8" , * args )
85163
86164
@@ -109,7 +187,8 @@ def _session_tests(session, post_install=None):
109187 # Pytest will return 5 when no tests are collected. This can happen
110188 # on travis where slow and flaky tests are excluded.
111189 # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
112- success_codes = [0 , 5 ]
190+ success_codes = [0 , 5 ],
191+ env = get_pytest_env_vars ()
113192 )
114193
115194
@@ -119,8 +198,22 @@ def py(session):
119198 if session .python in TESTED_VERSIONS :
120199 _session_tests (session )
121200 else :
122- print ("SKIPPED: {} tests are disabled for this sample." .format (session .python ))
201+ session .skip ("SKIPPED: {} tests are disabled for this sample." .format (
202+ session .python
203+ ))
204+
123205
206+ #
207+ # cloud_run session
208+ #
209+
210+ @nox .session
211+ def cloud_run (session ):
212+ """Run tests for cloud run."""
213+ if 'cloud_run' in TEST_CONFIG ['opt_in_sessions' ]:
214+ _session_tests (session )
215+ else :
216+ session .skip ('SKIPPED: cloud_run tests are disabled for this sample.' )
124217
125218#
126219# Readmegen
0 commit comments