Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce compatibility with native namespace packages #59

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/sync-repo-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 0 additions & 15 deletions google/iam/__init__.py

This file was deleted.

11 changes: 4 additions & 7 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -80,6 +78,7 @@ def default(session):
)


@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11"])
def unit(session):
"""Run the unit test suite."""
default(session)
Expand All @@ -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:
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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)
Expand Down
8 changes: 1 addition & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Empty file added testing/constraints-3.10.txt
Empty file.
Empty file added testing/constraints-3.11.txt
Empty file.
Empty file added testing/constraints-3.12.txt
Empty file.
Empty file added testing/constraints-3.7.txt
Empty file.
Empty file added testing/constraints-3.8.txt
Empty file.
Empty file added testing/constraints-3.9.txt
Empty file.
11 changes: 8 additions & 3 deletions google/__init__.py → tests/unit/test_google_iam.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# 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,
# 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__)
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()
37 changes: 37 additions & 0 deletions tests/unit/test_packaging.py
Original file line number Diff line number Diff line change
@@ -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)