From d7244ed3ef6254b0c3bd98e6c224f4e13cbe3c95 Mon Sep 17 00:00:00 2001
From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com>
Date: Fri, 4 Mar 2022 16:55:45 -0500
Subject: [PATCH] chore: Adding support for pytest-xdist and pytest-parallel
 (#248)

Source-Link: https://github.com/googleapis/synthtool/commit/82f5cb283efffe96e1b6cd634738e0e7de2cd90a
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:5d8da01438ece4021d135433f2cf3227aa39ef0eaccc941d62aa35e6902832ae

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
---
 kms/attestations/noxfile.py | 80 +++++++++++++++++++++----------------
 kms/snippets/noxfile.py     | 80 +++++++++++++++++++++----------------
 2 files changed, 92 insertions(+), 68 deletions(-)

diff --git a/kms/attestations/noxfile.py b/kms/attestations/noxfile.py
index 20cdfc620138..4c808af73ea2 100644
--- a/kms/attestations/noxfile.py
+++ b/kms/attestations/noxfile.py
@@ -188,42 +188,54 @@ def _session_tests(
     # check for presence of tests
     test_list = glob.glob("*_test.py") + glob.glob("test_*.py")
     test_list.extend(glob.glob("tests"))
+
     if len(test_list) == 0:
         print("No tests found, skipping directory.")
-    else:
-        if TEST_CONFIG["pip_version_override"]:
-            pip_version = TEST_CONFIG["pip_version_override"]
-            session.install(f"pip=={pip_version}")
-        """Runs py.test for a particular project."""
-        if os.path.exists("requirements.txt"):
-            if os.path.exists("constraints.txt"):
-                session.install("-r", "requirements.txt", "-c", "constraints.txt")
-            else:
-                session.install("-r", "requirements.txt")
-
-        if os.path.exists("requirements-test.txt"):
-            if os.path.exists("constraints-test.txt"):
-                session.install(
-                    "-r", "requirements-test.txt", "-c", "constraints-test.txt"
-                )
-            else:
-                session.install("-r", "requirements-test.txt")
-
-        if INSTALL_LIBRARY_FROM_SOURCE:
-            session.install("-e", _get_repo_root())
-
-        if post_install:
-            post_install(session)
-
-        session.run(
-            "pytest",
-            *(PYTEST_COMMON_ARGS + session.posargs),
-            # Pytest will return 5 when no tests are collected. This can happen
-            # on travis where slow and flaky tests are excluded.
-            # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
-            success_codes=[0, 5],
-            env=get_pytest_env_vars(),
-        )
+        return
+
+    if TEST_CONFIG["pip_version_override"]:
+        pip_version = TEST_CONFIG["pip_version_override"]
+        session.install(f"pip=={pip_version}")
+    """Runs py.test for a particular project."""
+    concurrent_args = []
+    if os.path.exists("requirements.txt"):
+        if os.path.exists("constraints.txt"):
+            session.install("-r", "requirements.txt", "-c", "constraints.txt")
+        else:
+            session.install("-r", "requirements.txt")
+        with open("requirements.txt") as rfile:
+            packages = rfile.read()
+
+    if os.path.exists("requirements-test.txt"):
+        if os.path.exists("constraints-test.txt"):
+            session.install(
+                "-r", "requirements-test.txt", "-c", "constraints-test.txt"
+            )
+        else:
+            session.install("-r", "requirements-test.txt")
+        with open("requirements-test.txt") as rtfile:
+            packages += rtfile.read()
+
+    if INSTALL_LIBRARY_FROM_SOURCE:
+        session.install("-e", _get_repo_root())
+
+    if post_install:
+        post_install(session)
+
+    if "pytest-parallel" in packages:
+        concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto'])
+    elif "pytest-xdist" in packages:
+        concurrent_args.extend(['-n', 'auto'])
+
+    session.run(
+        "pytest",
+        *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args),
+        # Pytest will return 5 when no tests are collected. This can happen
+        # on travis where slow and flaky tests are excluded.
+        # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
+        success_codes=[0, 5],
+        env=get_pytest_env_vars(),
+    )
 
 
 @nox.session(python=ALL_VERSIONS)
diff --git a/kms/snippets/noxfile.py b/kms/snippets/noxfile.py
index 20cdfc620138..4c808af73ea2 100644
--- a/kms/snippets/noxfile.py
+++ b/kms/snippets/noxfile.py
@@ -188,42 +188,54 @@ def _session_tests(
     # check for presence of tests
     test_list = glob.glob("*_test.py") + glob.glob("test_*.py")
     test_list.extend(glob.glob("tests"))
+
     if len(test_list) == 0:
         print("No tests found, skipping directory.")
-    else:
-        if TEST_CONFIG["pip_version_override"]:
-            pip_version = TEST_CONFIG["pip_version_override"]
-            session.install(f"pip=={pip_version}")
-        """Runs py.test for a particular project."""
-        if os.path.exists("requirements.txt"):
-            if os.path.exists("constraints.txt"):
-                session.install("-r", "requirements.txt", "-c", "constraints.txt")
-            else:
-                session.install("-r", "requirements.txt")
-
-        if os.path.exists("requirements-test.txt"):
-            if os.path.exists("constraints-test.txt"):
-                session.install(
-                    "-r", "requirements-test.txt", "-c", "constraints-test.txt"
-                )
-            else:
-                session.install("-r", "requirements-test.txt")
-
-        if INSTALL_LIBRARY_FROM_SOURCE:
-            session.install("-e", _get_repo_root())
-
-        if post_install:
-            post_install(session)
-
-        session.run(
-            "pytest",
-            *(PYTEST_COMMON_ARGS + session.posargs),
-            # Pytest will return 5 when no tests are collected. This can happen
-            # on travis where slow and flaky tests are excluded.
-            # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
-            success_codes=[0, 5],
-            env=get_pytest_env_vars(),
-        )
+        return
+
+    if TEST_CONFIG["pip_version_override"]:
+        pip_version = TEST_CONFIG["pip_version_override"]
+        session.install(f"pip=={pip_version}")
+    """Runs py.test for a particular project."""
+    concurrent_args = []
+    if os.path.exists("requirements.txt"):
+        if os.path.exists("constraints.txt"):
+            session.install("-r", "requirements.txt", "-c", "constraints.txt")
+        else:
+            session.install("-r", "requirements.txt")
+        with open("requirements.txt") as rfile:
+            packages = rfile.read()
+
+    if os.path.exists("requirements-test.txt"):
+        if os.path.exists("constraints-test.txt"):
+            session.install(
+                "-r", "requirements-test.txt", "-c", "constraints-test.txt"
+            )
+        else:
+            session.install("-r", "requirements-test.txt")
+        with open("requirements-test.txt") as rtfile:
+            packages += rtfile.read()
+
+    if INSTALL_LIBRARY_FROM_SOURCE:
+        session.install("-e", _get_repo_root())
+
+    if post_install:
+        post_install(session)
+
+    if "pytest-parallel" in packages:
+        concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto'])
+    elif "pytest-xdist" in packages:
+        concurrent_args.extend(['-n', 'auto'])
+
+    session.run(
+        "pytest",
+        *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args),
+        # Pytest will return 5 when no tests are collected. This can happen
+        # on travis where slow and flaky tests are excluded.
+        # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
+        success_codes=[0, 5],
+        env=get_pytest_env_vars(),
+    )
 
 
 @nox.session(python=ALL_VERSIONS)