Skip to content
Open
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
31 changes: 31 additions & 0 deletions packages/google-cloud-spanner-dbapi-driver/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
SYSTEM_TEST_EXTRAS: List[str] = []
SYSTEM_TEST_EXTRAS_BY_PYTHON: Dict[str, List[str]] = {}

COMPLIANCE_TEST_STANDARD_DEPENDENCIES = [
"pytest",
"spannerlib-python",
"google-cloud-spanner",
]

VERBOSE = False
MODE = "--verbose" if VERBOSE else "--quiet"

Expand Down Expand Up @@ -337,6 +343,31 @@ def system(session):
)


@nox.session(python=DEFAULT_PYTHON_VERSION)
def compliance(session):
"""Run compliance tests."""

# Sanity check: Only run tests if the environment variable is set.
if not os.environ.get("SPANNER_EMULATOR_HOST", ""):
session.skip(
"Emulator host must be set via SPANNER_EMULATOR_HOST environment variable"
)

session.install(*COMPLIANCE_TEST_STANDARD_DEPENDENCIES)
session.install("-e", ".")

test_paths = (
session.posargs if session.posargs else [os.path.join("tests", "compliance")]
)
session.run(
"py.test",
MODE,
f"--junitxml=compliance_{session.python}_sponge_log.xml",
*test_paths,
env={},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The test process requires the SPANNER_EMULATOR_HOST and TEST_DIALECT environment variables, but they are not being passed to session.run. An empty env dictionary is provided, which means the test environment will not have these crucial variables set, causing the tests to fail. You should pass these variables from the nox environment to the test execution environment.

        env={
            "SPANNER_EMULATOR_HOST": os.environ["SPANNER_EMULATOR_HOST"],
            "TEST_DIALECT": os.environ.get("TEST_DIALECT", "GoogleSQL"),
        },

)


@nox.session(python=DEFAULT_PYTHON_VERSION)
def cover(session):
"""Run the final coverage report.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2026 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.

# This file is intentionally left blank to mark this directory as a package.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2026 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.
"""Helper functions for compliance tests."""

import os

SPANNER_EMULATOR_HOST = os.environ.get("SPANNER_EMULATOR_HOST")

PROJECT_ID = "test-project"
INSTANCE_ID = "test-instance"
DATABASE_ID = "test-db"

EMULATOR_TEST_CONNECTION_STRING = (
f"{SPANNER_EMULATOR_HOST}"
f"projects/{PROJECT_ID}"
f"/instances/{INSTANCE_ID}"
f"/databases/{DATABASE_ID}"
"?autoConfigEmulator=true"
)


def setup_test_env() -> None:
print(f"Set SPANNER_EMULATOR_HOST to {os.environ['SPANNER_EMULATOR_HOST']}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using os.environ['SPANNER_EMULATOR_HOST'] can raise a KeyError if the environment variable is not set. Although the nox session checks for this, running tests manually without nox could lead to a crash. It's safer to use the module-level SPANNER_EMULATOR_HOST variable, which is already populated using os.environ.get() and is available in the scope of this function.

Suggested change
print(f"Set SPANNER_EMULATOR_HOST to {os.environ['SPANNER_EMULATOR_HOST']}")
print(f"Set SPANNER_EMULATOR_HOST to {SPANNER_EMULATOR_HOST}")

print(f"Using Connection String: {get_test_connection_string()}")


def get_test_connection_string() -> str:
return EMULATOR_TEST_CONNECTION_STRING
Loading