-
Notifications
You must be signed in to change notification settings - Fork 22
Improve addopts masking (fixes repos with addopts which interfere with cf) #812
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c6d5f6f
wip
aseembits93 c6da4fd
wip
aseembits93 333271c
wip
aseembits93 54072a7
Merge branch 'main' into improve-addopts
aseembits93 82f1b81
todo cleanup write tests
aseembits93 79cee0d
todo cleanup write tests
aseembits93 da667fe
basic tests, write more, write for functions not being tested yet
aseembits93 9048d38
basic tests, write more, write for functions not being tested yet
aseembits93 8b64b39
remove patch and use actual function call
aseembits93 7d9ee86
tests done, ready to review
aseembits93 b7dc7bd
Merge branch 'main' into improve-addopts
aseembits93 4414b25
Update codeflash/code_utils/code_utils.py
aseembits93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import configparser | ||
| import os | ||
| import stat | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| import tomlkit | ||
|
|
||
| from codeflash.code_utils.code_utils import custom_addopts | ||
|
|
||
| def test_custom_addopts_modifies_and_restores_dotini_file(tmp_path: Path) -> None: | ||
| """Verify that custom_addopts correctly modifies and then restores a pytest.ini file.""" | ||
| # Create a dummy pytest.ini file | ||
| config_file = tmp_path / ".pytest.ini" | ||
| original_content = "[pytest]\naddopts = -v --cov=./src -n auto\n" | ||
| config_file.write_text(original_content) | ||
|
|
||
| # Use patch to mock get_all_closest_config_files | ||
| os.chdir(tmp_path) | ||
| with custom_addopts(): | ||
| # Check that the file is modified inside the context | ||
| modified_content = config_file.read_text() | ||
| config = configparser.ConfigParser() | ||
| config.read_string(modified_content) | ||
| modified_addopts = config.get("pytest", "addopts", fallback="") | ||
| assert modified_addopts == "-v" | ||
|
|
||
| # Check that the file is restored after exiting the context | ||
| restored_content = config_file.read_text() | ||
| assert restored_content.strip() == original_content.strip() | ||
|
|
||
| def test_custom_addopts_modifies_and_restores_ini_file(tmp_path: Path) -> None: | ||
| """Verify that custom_addopts correctly modifies and then restores a pytest.ini file.""" | ||
| # Create a dummy pytest.ini file | ||
| config_file = tmp_path / "pytest.ini" | ||
| original_content = "[pytest]\naddopts = -v --cov=./src -n auto\n" | ||
| config_file.write_text(original_content) | ||
|
|
||
| # Use patch to mock get_all_closest_config_files | ||
| os.chdir(tmp_path) | ||
| with custom_addopts(): | ||
| # Check that the file is modified inside the context | ||
| modified_content = config_file.read_text() | ||
| config = configparser.ConfigParser() | ||
| config.read_string(modified_content) | ||
| modified_addopts = config.get("pytest", "addopts", fallback="") | ||
| assert modified_addopts == "-v" | ||
|
|
||
| # Check that the file is restored after exiting the context | ||
| restored_content = config_file.read_text() | ||
| assert restored_content.strip() == original_content.strip() | ||
|
|
||
|
|
||
| def test_custom_addopts_modifies_and_restores_toml_file(tmp_path: Path) -> None: | ||
| """Verify that custom_addopts correctly modifies and then restores a pyproject.toml file.""" | ||
| # Create a dummy pyproject.toml file | ||
| config_file = tmp_path / "pyproject.toml" | ||
| os.chdir(tmp_path) | ||
| original_addopts = "-v --cov=./src --junitxml=report.xml" | ||
| original_content_dict = { | ||
| "tool": {"pytest": {"ini_options": {"addopts": original_addopts}}} | ||
| } | ||
| original_content = tomlkit.dumps(original_content_dict) | ||
| config_file.write_text(original_content) | ||
|
|
||
| # Use patch to mock get_all_closest_config_files | ||
| os.chdir(tmp_path) | ||
| with custom_addopts(): | ||
| # Check that the file is modified inside the context | ||
| modified_content = config_file.read_text() | ||
| modified_data = tomlkit.parse(modified_content) | ||
| modified_addopts = modified_data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "") | ||
| assert modified_addopts == "-v" | ||
|
|
||
| # Check that the file is restored after exiting the context | ||
| restored_content = config_file.read_text() | ||
| assert restored_content.strip() == original_content.strip() | ||
|
|
||
|
|
||
| def test_custom_addopts_handles_no_addopts(tmp_path: Path) -> None: | ||
| """Ensure custom_addopts doesn't fail when a config file has no addopts.""" | ||
| # Create a dummy pytest.ini file without addopts | ||
| config_file = tmp_path / "pytest.ini" | ||
| original_content = "[pytest]\n# no addopts here\n" | ||
| config_file.write_text(original_content) | ||
|
|
||
| os.chdir(tmp_path) | ||
| with custom_addopts(): | ||
| # The file should not be modified | ||
| content_inside_context = config_file.read_text() | ||
| assert content_inside_context == original_content | ||
|
|
||
| # The file should remain unchanged | ||
| content_after_context = config_file.read_text() | ||
| assert content_after_context == original_content | ||
|
|
||
| def test_custom_addopts_handles_no_relevant_files(tmp_path: Path) -> None: | ||
| """Ensure custom_addopts runs without error when no config files are found.""" | ||
| # No config files created in tmp_path | ||
|
|
||
| os.chdir(tmp_path) | ||
| # This should execute without raising any exceptions | ||
| with custom_addopts(): | ||
| pass | ||
| # No assertions needed, the test passes if no exceptions were raised | ||
|
|
||
|
|
||
| def test_custom_addopts_toml_without_pytest_section(tmp_path: Path) -> None: | ||
| """Verify custom_addopts doesn't fail with a toml file missing a [tool.pytest] section.""" | ||
| config_file = tmp_path / "pyproject.toml" | ||
| original_content_dict = {"tool": {"other_tool": {"key": "value"}}} | ||
| original_content = tomlkit.dumps(original_content_dict) | ||
| config_file.write_text(original_content) | ||
|
|
||
| os.chdir(tmp_path) | ||
| with custom_addopts(): | ||
| content_inside_context = config_file.read_text() | ||
| assert content_inside_context == original_content | ||
|
|
||
| content_after_context = config_file.read_text() | ||
| assert content_after_context == original_content | ||
|
|
||
|
|
||
| def test_custom_addopts_ini_without_pytest_section(tmp_path: Path) -> None: | ||
| """Verify custom_addopts doesn't fail with an ini file missing a [pytest] section.""" | ||
| config_file = tmp_path / "pytest.ini" | ||
| original_content = "[other_section]\nkey = value\n" | ||
| config_file.write_text(original_content) | ||
|
|
||
| os.chdir(tmp_path) | ||
| with custom_addopts(): | ||
| content_inside_context = config_file.read_text() | ||
| assert content_inside_context == original_content | ||
|
|
||
| content_after_context = config_file.read_text() | ||
| assert content_after_context == original_content | ||
|
|
||
|
|
||
| def test_custom_addopts_with_multiple_config_files(tmp_path: Path) -> None: | ||
| """Verify custom_addopts modifies and restores all found config files.""" | ||
| os.chdir(tmp_path) | ||
|
|
||
| # Create pytest.ini | ||
| ini_file = tmp_path / "pytest.ini" | ||
| ini_original_content = "[pytest]\naddopts = -v --cov\n" | ||
| ini_file.write_text(ini_original_content) | ||
|
|
||
| # Create pyproject.toml | ||
| toml_file = tmp_path / "pyproject.toml" | ||
| toml_original_addopts = "-s -n auto" | ||
| toml_original_content_dict = { | ||
| "tool": {"pytest": {"ini_options": {"addopts": toml_original_addopts}}} | ||
| } | ||
| toml_original_content = tomlkit.dumps(toml_original_content_dict) | ||
| toml_file.write_text(toml_original_content) | ||
|
|
||
| with custom_addopts(): | ||
| # Check INI file modification | ||
| ini_modified_content = ini_file.read_text() | ||
| config = configparser.ConfigParser() | ||
| config.read_string(ini_modified_content) | ||
| assert config.get("pytest", "addopts", fallback="") == "-v" | ||
|
|
||
| # Check TOML file modification | ||
| toml_modified_content = toml_file.read_text() | ||
| modified_data = tomlkit.parse(toml_modified_content) | ||
| modified_addopts = modified_data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "") | ||
| assert modified_addopts == "-s" | ||
|
|
||
| # Check that both files are restored | ||
| assert ini_file.read_text().strip() == ini_original_content.strip() | ||
| assert toml_file.read_text().strip() == toml_original_content.strip() | ||
|
|
||
|
|
||
| def test_custom_addopts_restores_on_exception(tmp_path: Path) -> None: | ||
| """Ensure config file is restored even if an exception occurs inside the context.""" | ||
| config_file = tmp_path / "pytest.ini" | ||
| original_content = "[pytest]\naddopts = -v --cov\n" | ||
| config_file.write_text(original_content) | ||
|
|
||
| os.chdir(tmp_path) | ||
| with pytest.raises(ValueError, match="Test exception"): | ||
| with custom_addopts(): | ||
| raise ValueError("Test exception") | ||
|
|
||
| restored_content = config_file.read_text() | ||
| assert restored_content.strip() == original_content.strip() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.