Skip to content
Closed
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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ COPY ./pybind_interface/ /qsim/lib/
COPY ./qsimcirq_tests/ /qsim/qsimcirq_tests/
COPY ./requirements.txt /qsim/requirements.txt
COPY ./pyproject.toml /qsim/pyproject.toml
COPY ./setup.py /qsim/setup.py

WORKDIR /qsim/

Expand Down
78 changes: 78 additions & 0 deletions qsimcirq_tests/setuppy_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 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
#
# https://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.

"""Test setup.py code."""

import os
import subprocess
import sys


def run_setup(cmake_args, root_dir):
env = os.environ.copy()
env["CMAKE_ARGS"] = cmake_args

try:
# Using sys.executable to run setup.py with the same Python executable.
return subprocess.run(
[sys.executable, "setup.py", "build_ext"],
env=env,
capture_output=True,
text=True,
cwd=str(root_dir),
)
except FileNotFoundError as e:
# This Dummy class emulates what subprocess.run() returns.
class Dummy:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As a general comment, term dummy is problematic and should be avoided in naming things, go/respectful-words.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Oh yes, quite right. Thank you for pointing that out.

stderr = f"Command or file not found: {e}"
returncode = 1

return Dummy()
except PermissionError as e:

class Dummy:
stderr = f"Permission denied: {e}"
returncode = 1

return Dummy()
except OSError as e:

class Dummy:
stderr = f"OS error occurred: {e}"
returncode = 1

return Dummy()
except Exception as e:
# Fallback for unexpected errors in subprocess.run itself
class Dummy:
stderr = f"Unexpected error: {str(e)}"
returncode = 1

return Dummy()
Comment thread
mhucka marked this conversation as resolved.


def test_valid_cmake_args(pytestconfig):
res = run_setup("-DCMAKE_CXX_STANDARD=17", pytestconfig.rootpath)
# If it fails, it shouldn't be because of our validation.
assert "arguments must begin with a dash" not in res.stderr


def test_invalid_cmake_args_no_dash(pytestconfig):
res = run_setup("NOT_A_FLAG", pytestconfig.rootpath)
assert "arguments must begin with a dash" in res.stderr


def test_invalid_cmake_args_malicious(pytestconfig):
res = run_setup("-DVAR=VAL ; rm -rf /", pytestconfig.rootpath)
assert "arguments must begin with a dash" in res.stderr
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@
# Append additional CMake arguments from the environment variable.
# This is e.g. used by cibuildwheel to force a certain C++ standard.
additional_cmake_args = os.environ.get("CMAKE_ARGS", "")
if additional_cmake_args:
cmake_args += additional_cmake_args.split()
for arg in additional_cmake_args.split():
if not arg.startswith("-D"):
raise RuntimeError(
f"The value '{arg}' in the environment variable CMAKE_ARGS "
"is invalid; only definition arguments starting with '-D' are allowed."

Check warning on line 92 in setup.py

View workflow job for this annotation

GitHub Actions / Python format & lint checks

C0301: Line too long (91/88) (line-too-long)
)
cmake_args.append(arg)
Comment thread
mhucka marked this conversation as resolved.

cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]
Expand Down
Loading