-
Notifications
You must be signed in to change notification settings - Fork 168
fix: test isolation for firewall tests on unix-like systems #767
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
Conversation
Prevents tests from modifying user's system files during development. Tests now run in isolated temporary directories instead of user's home directory. These tests will be refactored to run in containerized e2e environments to better simulate real-world deployment scenarios.
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
🚀 Build artifacts are ready for testing!Download the wheel file and binaries with gh CLI or from the workflow artifacts. 📦 Install & RunPre-requisites# Install uv if needed
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create and enter artifacts directory
mkdir artifacts && cd artifacts Quick Test with Python Package# Download and run with uv
gh run download 15808797208 -n dist -R pyupio/safety
uv run --with safety-3.5.2+fix.test.isolation.firewall-py3-none-any.whl safety --version Binary Installation# Linux
gh run download 15808797208 -n safety-linux -D linux -R pyupio/safety
cd linux && mv safety safety-pr && chmod +x safety-pr
# macOS
gh run download 15808797208 -n safety-macos -D macos -R pyupio/safety
cd macos && mv safety safety-pr && chmod +x safety-pr
# Windows
gh run download 15808797208 -n safety-windows -D windows -R pyupio/safety
cd windows && mv safety.exe safety-pr.exe
./safety-pr --version
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request updates the test suite for Unix-like firewall tests to prevent modifications to the user’s system files by using isolated temporary directories and in-method patching. Key changes include:
- Replacing patch decorators with inline patchers for improved control over patch lifecycles.
- Updating expected test outputs to reference the new patch variable (self.mock_now).
- Correcting the uninstall tests to call remove_interceptors() instead of install_interceptors().
Comments suppressed due to low confidence (1)
tests/tool/interceptors/test_unix.py:116
- [nitpick] Since this test now verifies the removal of interceptors, consider renaming the test method to 'test_remove_interceptors_all_tools' to clearly reflect its purpose.
result = interceptor.remove_interceptors()
self.mock_config_patcher = patch( | ||
"safety.tool.interceptors.unix.USER_CONFIG_DIR", | ||
Path(self.temp_dir) / ".safety", | ||
) | ||
self.mock_config_patcher.start() | ||
|
||
self.mock_home_patcher = patch("safety.tool.interceptors.unix.Path.home") | ||
self.mock_datetime_patcher = patch("safety.tool.interceptors.base.datetime") | ||
self.mock_version_patcher = patch("safety.tool.interceptors.base.get_version") | ||
|
||
self.mock_home = self.mock_home_patcher.start() | ||
self.mock_datetime = self.mock_datetime_patcher.start() | ||
self.mock_version = self.mock_version_patcher.start() | ||
|
||
self.mock_home.return_value = Path(self.temp_dir) | ||
self.mock_version.return_value = "1.0.0" | ||
self.mock_now = datetime(2024, 1, 1, tzinfo=timezone.utc) | ||
self.mock_datetime.now.return_value = self.mock_now | ||
|
||
self.addCleanup(self.mock_home_patcher.stop) | ||
self.addCleanup(self.mock_datetime_patcher.stop) | ||
self.addCleanup(self.mock_version_patcher.stop) | ||
self.addCleanup(self.mock_config_patcher.stop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider moving the patch initialization to the setUp method (or a common fixture) to improve readability and reduce duplication if similar patches are used in multiple tests.
self.mock_config_patcher = patch( | |
"safety.tool.interceptors.unix.USER_CONFIG_DIR", | |
Path(self.temp_dir) / ".safety", | |
) | |
self.mock_config_patcher.start() | |
self.mock_home_patcher = patch("safety.tool.interceptors.unix.Path.home") | |
self.mock_datetime_patcher = patch("safety.tool.interceptors.base.datetime") | |
self.mock_version_patcher = patch("safety.tool.interceptors.base.get_version") | |
self.mock_home = self.mock_home_patcher.start() | |
self.mock_datetime = self.mock_datetime_patcher.start() | |
self.mock_version = self.mock_version_patcher.start() | |
self.mock_home.return_value = Path(self.temp_dir) | |
self.mock_version.return_value = "1.0.0" | |
self.mock_now = datetime(2024, 1, 1, tzinfo=timezone.utc) | |
self.mock_datetime.now.return_value = self.mock_now | |
self.addCleanup(self.mock_home_patcher.stop) | |
self.addCleanup(self.mock_datetime_patcher.stop) | |
self.addCleanup(self.mock_version_patcher.stop) | |
self.addCleanup(self.mock_config_patcher.stop) | |
self.mock_patchers = { | |
"config": patch( | |
"safety.tool.interceptors.unix.USER_CONFIG_DIR", | |
Path(self.temp_dir) / ".safety", | |
), | |
"home": patch("safety.tool.interceptors.unix.Path.home"), | |
"datetime": patch("safety.tool.interceptors.base.datetime"), | |
"version": patch("safety.tool.interceptors.base.get_version"), | |
} | |
self.mock_objects = { | |
name: patcher.start() for name, patcher in self.mock_patchers.items() | |
} | |
self.mock_objects["home"].return_value = Path(self.temp_dir) | |
self.mock_objects["version"].return_value = "1.0.0" | |
self.mock_now = datetime(2024, 1, 1, tzinfo=timezone.utc) | |
self.mock_objects["datetime"].now.return_value = self.mock_now | |
for patcher in self.mock_patchers.values(): | |
self.addCleanup(patcher.stop) |
Copilot uses AI. Check for mistakes.
Prevents tests from modifying user's system files during development. Tests now run in isolated temporary directories instead of user's home directory. These tests will be refactored to run in containerized e2e environments to better simulate real-world deployment scenarios.