From 4a19b0b647f22c3066fb2686cf70f5356ce55b9e Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 15 Nov 2023 19:47:34 +0000 Subject: [PATCH 1/4] feat: Introduce compatibility with native namespace packages --- google/iam/__init__.py | 15 -------- setup.py | 8 +--- .../unit/test_google_iam.py | 11 ++++-- tests/unit/test_packaging.py | 37 +++++++++++++++++++ 4 files changed, 46 insertions(+), 25 deletions(-) delete mode 100644 google/iam/__init__.py rename google/__init__.py => tests/unit/test_google_iam.py (66%) create mode 100644 tests/unit/test_packaging.py diff --git a/google/iam/__init__.py b/google/iam/__init__.py deleted file mode 100644 index ad1e9d1..0000000 --- a/google/iam/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -# Copyright 2022 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -__import__("pkg_resources").declare_namespace(__name__) diff --git a/setup.py b/setup.py index ef0abae..d8d9ac9 100644 --- a/setup.py +++ b/setup.py @@ -39,15 +39,10 @@ packages = [ package - for package in setuptools.PEP420PackageFinder.find() + for package in setuptools.find_namespace_packages() if package.startswith("google") ] -namespaces = ["google"] -if "google.iam" in packages: - namespaces.append("google.iam") - - setuptools.setup( name=name, version=version, @@ -74,7 +69,6 @@ platforms="Posix; MacOS X; Windows", packages=packages, python_requires=">=3.7", - namespace_packages=namespaces, install_requires=dependencies, include_package_data=True, zip_safe=False, diff --git a/google/__init__.py b/tests/unit/test_google_iam.py similarity index 66% rename from google/__init__.py rename to tests/unit/test_google_iam.py index ad1e9d1..fabccb4 100644 --- a/google/__init__.py +++ b/tests/unit/test_google_iam.py @@ -1,10 +1,10 @@ -# Copyright 2022 Google LLC +# Copyright 2023 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -12,4 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -__import__("pkg_resources").declare_namespace(__name__) +from google.iam.v1 import policy_pb2 + +def test_create_policy(): + # Check that the import works + # and that an Policy instance can be instantiated + policy_pb2.Policy() \ No newline at end of file diff --git a/tests/unit/test_packaging.py b/tests/unit/test_packaging.py new file mode 100644 index 0000000..f239c8b --- /dev/null +++ b/tests/unit/test_packaging.py @@ -0,0 +1,37 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import subprocess +import sys + + +def test_namespace_package_compat(tmp_path): + # The ``google`` namespace package should not be masked + # by the presence of ``grpc-google-iam-v1``. + google = tmp_path / "google" + google.mkdir() + google.joinpath("othermod.py").write_text("") + env = dict(os.environ, PYTHONPATH=str(tmp_path)) + cmd = [sys.executable, "-m", "google.othermod"] + subprocess.check_call(cmd, env=env) + + # The ``google.iam`` namespace package should not be masked + # by the presence of ``grpc-google-iam-v1``. + google_iam = tmp_path / "google" / "iam" + google_iam.mkdir() + google_iam.joinpath("othermod.py").write_text("") + env = dict(os.environ, PYTHONPATH=str(tmp_path)) + cmd = [sys.executable, "-m", "google.iam.othermod"] + subprocess.check_call(cmd, env=env) From 9ad7c40d4e208a45b718a8066d7d756450a44fbe Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 16 Nov 2023 22:42:54 +0000 Subject: [PATCH 2/4] add unit test presubmits --- .github/workflows/unittest.yml | 57 ++++++++++++++++++++++++++++++++++ noxfile.py | 11 +++---- owlbot.py | 2 +- testing/constraints-3.10.txt | 0 testing/constraints-3.11.txt | 0 testing/constraints-3.12.txt | 0 testing/constraints-3.7.txt | 0 testing/constraints-3.8.txt | 0 testing/constraints-3.9.txt | 0 9 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/unittest.yml create mode 100644 testing/constraints-3.10.txt create mode 100644 testing/constraints-3.11.txt create mode 100644 testing/constraints-3.12.txt create mode 100644 testing/constraints-3.7.txt create mode 100644 testing/constraints-3.8.txt create mode 100644 testing/constraints-3.9.txt diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml new file mode 100644 index 0000000..8057a76 --- /dev/null +++ b/.github/workflows/unittest.yml @@ -0,0 +1,57 @@ +on: + pull_request: + branches: + - main +name: unittest +jobs: + unit: + runs-on: ubuntu-latest + strategy: + matrix: + python: ['3.7', '3.8', '3.9', '3.10', '3.11'] + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python }} + - name: Install nox + run: | + python -m pip install --upgrade setuptools pip wheel + python -m pip install nox + - name: Run unit tests + env: + COVERAGE_FILE: .coverage-${{ matrix.python }} + run: | + nox -s unit-${{ matrix.python }} + - name: Upload coverage results + uses: actions/upload-artifact@v3 + with: + name: coverage-artifacts + path: .coverage-${{ matrix.python }} + + cover: + runs-on: ubuntu-latest + needs: + - unit + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.8" + - name: Install coverage + run: | + python -m pip install --upgrade setuptools pip wheel + python -m pip install coverage + - name: Download coverage results + uses: actions/download-artifact@v3 + with: + name: coverage-artifacts + path: .coverage-results/ + - name: Report coverage results + run: | + coverage combine .coverage-results/.coverage* + coverage report --show-missing --fail-under=100 diff --git a/noxfile.py b/noxfile.py index 0228d78..ae8130b 100644 --- a/noxfile.py +++ b/noxfile.py @@ -55,15 +55,13 @@ def default(session): session.install("asyncmock", "pytest-asyncio") session.install("mock", "pytest", "pytest-cov") - session.install("-e", ".") - constraints_path = str( CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" ) # Install grpc-google-iam-v1 # This *must* be the last install command to get the package from source. - session.install("e", "..", "-c", constraints_path) + session.install("-e", ".", "-c", constraints_path) # Run py.test against the unit tests. session.run( @@ -80,6 +78,7 @@ def default(session): ) +@nox.session(python=["3.6", "3.7", "3.8", "3.9"]) def unit(session): """Run the unit test suite.""" default(session) @@ -106,15 +105,13 @@ def system(session): # virtualenv's dist-packages. session.install("mock", "pytest", "google-cloud-testutils") - session.install("-e", ".") - constraints_path = str( CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" ) # Install grpc-google-iam-v1 # This *must* be the last install command to get the package from source. - session.install("e", "..", "-c", constraints_path) + session.install("-e", ".", "-c", constraints_path) # Run py.test against the system tests. if system_test_exists: @@ -155,7 +152,7 @@ def test(session, library): session.cd(library) - unit(session) + default(session) # system tests are run on 3.7 only if session.python == "3.7": diff --git a/owlbot.py b/owlbot.py index 240e520..d1be076 100644 --- a/owlbot.py +++ b/owlbot.py @@ -49,7 +49,7 @@ s.move(templated_files / "CONTRIBUTING.rst") s.move(templated_files / "*.md") s.move(templated_files / "renovate.json") -s.move(templated_files / ".github", excludes=["workflows/unittest.yml"]) +s.move(templated_files / ".github") s.move(templated_files / ".trampolinerc") python.py_samples(skip_readmes=True) diff --git a/testing/constraints-3.10.txt b/testing/constraints-3.10.txt new file mode 100644 index 0000000..e69de29 diff --git a/testing/constraints-3.11.txt b/testing/constraints-3.11.txt new file mode 100644 index 0000000..e69de29 diff --git a/testing/constraints-3.12.txt b/testing/constraints-3.12.txt new file mode 100644 index 0000000..e69de29 diff --git a/testing/constraints-3.7.txt b/testing/constraints-3.7.txt new file mode 100644 index 0000000..e69de29 diff --git a/testing/constraints-3.8.txt b/testing/constraints-3.8.txt new file mode 100644 index 0000000..e69de29 diff --git a/testing/constraints-3.9.txt b/testing/constraints-3.9.txt new file mode 100644 index 0000000..e69de29 From 738fff00ad9e80af9cd959898774f41e0ba44342 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 16 Nov 2023 22:44:14 +0000 Subject: [PATCH 3/4] update unit test versions --- noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index ae8130b..0e24114 100644 --- a/noxfile.py +++ b/noxfile.py @@ -78,7 +78,7 @@ def default(session): ) -@nox.session(python=["3.6", "3.7", "3.8", "3.9"]) +@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11"]) def unit(session): """Run the unit test suite.""" default(session) From eb98093ab0fea939d72bc800c0cc9349c182ae8f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 16 Nov 2023 22:55:00 +0000 Subject: [PATCH 4/4] update required checks --- .github/sync-repo-settings.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml index 519ca38..fd3f4d1 100644 --- a/.github/sync-repo-settings.yaml +++ b/.github/sync-repo-settings.yaml @@ -9,6 +9,15 @@ branchProtectionRules: requiredStatusCheckContexts: - 'cla/google' - 'OwlBot Post Processor' + - 'docs' + - 'docfx' + - 'lint' + - 'unit (3.7)' + - 'unit (3.8)' + - 'unit (3.9)' + - 'unit (3.10)' + - 'unit (3.11)' + - 'cover' permissionRules: - team: actools-python permission: admin