From d842233b24689f10d75f72fe2c795fbc90cb00a4 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 29 Jul 2021 00:38:17 -0400 Subject: [PATCH] chore: use templated noxfile.py (#366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: use templated noxfile.py * Update noxfile.py * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * coverage * coverage * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * Replace fixup with customize Co-authored-by: larkee <31196561+larkee@users.noreply.github.com> Co-authored-by: Owl Bot --- noxfile.py | 35 ++++++++++++++-------- owlbot.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 14 deletions(-) diff --git a/noxfile.py b/noxfile.py index efc4f53738..6579eecd49 100644 --- a/noxfile.py +++ b/noxfile.py @@ -64,14 +64,7 @@ def lint(session): @nox.session(python=DEFAULT_PYTHON_VERSION) def blacken(session): - """Run black. - - Format code to uniform standard. - - This currently uses Python 3.6 due to the automated Kokoro run of synthtool. - That run uses an image that doesn't have 3.6 installed. Before updating this - check the state of the `gcp_ubuntu_config` we use for that Kokoro run. - """ + """Run black. Format code to uniform standard.""" session.install(BLACK_VERSION) session.run( "black", *BLACK_PATHS, @@ -156,6 +149,10 @@ def system(session): "Credentials or emulator host must be set via environment variable" ) + # Install pyopenssl for mTLS testing. + if os.environ.get("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true": + session.install("pyopenssl") + system_test_exists = os.path.exists(system_test_path) system_test_folder_exists = os.path.exists(system_test_folder_path) # Sanity check: only run tests if found. @@ -172,9 +169,21 @@ def system(session): # Run py.test against the system tests. if system_test_exists: - session.run("py.test", "--quiet", system_test_path, *session.posargs) + session.run( + "py.test", + "--quiet", + f"--junitxml=system_{session.python}_sponge_log.xml", + system_test_path, + *session.posargs, + ) if system_test_folder_exists: - session.run("py.test", "--quiet", system_test_folder_path, *session.posargs) + session.run( + "py.test", + "--quiet", + f"--junitxml=system_{session.python}_sponge_log.xml", + system_test_folder_path, + *session.posargs, + ) @nox.session(python=DEFAULT_PYTHON_VERSION) @@ -195,7 +204,7 @@ def docs(session): """Build the docs for this library.""" session.install("-e", ".[tracing]") - session.install("sphinx", "alabaster", "recommonmark") + session.install("sphinx==4.0.1", "alabaster", "recommonmark") shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) session.run( @@ -217,7 +226,9 @@ def docfx(session): """Build the docfx yaml files for this library.""" session.install("-e", ".[tracing]") - session.install("sphinx", "alabaster", "recommonmark", "gcp-sphinx-docfx-yaml") + session.install( + "sphinx==4.0.1", "alabaster", "recommonmark", "gcp-sphinx-docfx-yaml" + ) shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) session.run( diff --git a/owlbot.py b/owlbot.py index 0899ba8d90..635dc54225 100644 --- a/owlbot.py +++ b/owlbot.py @@ -109,8 +109,8 @@ def get_staging_dirs( # ---------------------------------------------------------------------------- # Add templated files # ---------------------------------------------------------------------------- -templated_files = common.py_library(microgenerator=True, samples=True) -s.move(templated_files, excludes=[".coveragerc", "noxfile.py"]) +templated_files = common.py_library(microgenerator=True, samples=True, cov_level=99) +s.move(templated_files, excludes=[".coveragerc"]) # Ensure CI runs on a new instance each time s.replace( @@ -127,4 +127,85 @@ def get_staging_dirs( python.py_samples() +# ---------------------------------------------------------------------------- +# Customize noxfile.py +# ---------------------------------------------------------------------------- + +def place_before(path, text, *before_text, escape=None): + replacement = "\n".join(before_text) + "\n" + text + if escape: + for c in escape: + text = text.replace(c, '\\' + c) + s.replace([path], text, replacement) + +open_telemetry_test = """ + session.install("-e", ".[tracing]", "-c", constraints_path) + + # Run py.test against the unit tests with OpenTelemetry. + session.run( + "py.test", + "--quiet", + "--cov=google.cloud.spanner", + "--cov=google.cloud", + "--cov=tests.unit", + "--cov-append", + "--cov-config=.coveragerc", + "--cov-report=", + "--cov-fail-under=0", + os.path.join("tests", "unit"), + *session.posargs, + ) +""" + +place_before( + "noxfile.py", + "@nox.session(python=UNIT_TEST_PYTHON_VERSIONS)", + open_telemetry_test, + escape="()" +) + +skip_tests_if_env_var_not_set ="""# Sanity check: Only run tests if the environment variable is set. + if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", "") and not os.environ.get( + "SPANNER_EMULATOR_HOST", "" + ): + session.skip( + "Credentials or emulator host must be set via environment variable" + ) +""" + +place_before( + "noxfile.py", + "# Install pyopenssl for mTLS testing.", + skip_tests_if_env_var_not_set, + escape="()" +) + +s.replace( + "noxfile.py", + """f"--junitxml=unit_{session.python}_sponge_log.xml", + "--cov=google/cloud", + "--cov=tests/unit",""", + """\"--cov=google.cloud.spanner", + "--cov=google.cloud", + "--cov=tests.unit",""" +) + +s.replace( + "noxfile.py", + """session.install\("-e", "."\)""", + """session.install("-e", ".[tracing]")""" +) + +s.replace( + "noxfile.py", + """# Install all test dependencies, then install this package into the + # virtualenv's dist-packages. + session.install\("mock", "pytest", "google-cloud-testutils", "-c", constraints_path\) + session.install\("-e", ".", "-c", constraints_path\)""", + """# Install all test dependencies, then install this package into the + # virtualenv's dist-packages. + session.install("mock", "pytest", "google-cloud-testutils", "-c", constraints_path) + session.install("-e", ".[tracing]", "-c", constraints_path)""" +) + s.shell.run(["nox", "-s", "blacken"], hide_output=False)