|
2 | 2 |
|
3 | 3 | import configparser |
4 | 4 | import os |
| 5 | +import stat |
5 | 6 | from pathlib import Path |
6 | 7 | from unittest.mock import patch |
7 | 8 |
|
| 9 | +import pytest |
8 | 10 | import tomlkit |
9 | 11 |
|
10 | 12 | from codeflash.code_utils.code_utils import custom_addopts |
@@ -104,3 +106,85 @@ def test_custom_addopts_handles_no_relevant_files(tmp_path: Path) -> None: |
104 | 106 | with custom_addopts(): |
105 | 107 | pass |
106 | 108 | # No assertions needed, the test passes if no exceptions were raised |
| 109 | + |
| 110 | + |
| 111 | +def test_custom_addopts_toml_without_pytest_section(tmp_path: Path) -> None: |
| 112 | + """Verify custom_addopts doesn't fail with a toml file missing a [tool.pytest] section.""" |
| 113 | + config_file = tmp_path / "pyproject.toml" |
| 114 | + original_content_dict = {"tool": {"other_tool": {"key": "value"}}} |
| 115 | + original_content = tomlkit.dumps(original_content_dict) |
| 116 | + config_file.write_text(original_content) |
| 117 | + |
| 118 | + os.chdir(tmp_path) |
| 119 | + with custom_addopts(): |
| 120 | + content_inside_context = config_file.read_text() |
| 121 | + assert content_inside_context == original_content |
| 122 | + |
| 123 | + content_after_context = config_file.read_text() |
| 124 | + assert content_after_context == original_content |
| 125 | + |
| 126 | + |
| 127 | +def test_custom_addopts_ini_without_pytest_section(tmp_path: Path) -> None: |
| 128 | + """Verify custom_addopts doesn't fail with an ini file missing a [pytest] section.""" |
| 129 | + config_file = tmp_path / "pytest.ini" |
| 130 | + original_content = "[other_section]\nkey = value\n" |
| 131 | + config_file.write_text(original_content) |
| 132 | + |
| 133 | + os.chdir(tmp_path) |
| 134 | + with custom_addopts(): |
| 135 | + content_inside_context = config_file.read_text() |
| 136 | + assert content_inside_context == original_content |
| 137 | + |
| 138 | + content_after_context = config_file.read_text() |
| 139 | + assert content_after_context == original_content |
| 140 | + |
| 141 | + |
| 142 | +def test_custom_addopts_with_multiple_config_files(tmp_path: Path) -> None: |
| 143 | + """Verify custom_addopts modifies and restores all found config files.""" |
| 144 | + os.chdir(tmp_path) |
| 145 | + |
| 146 | + # Create pytest.ini |
| 147 | + ini_file = tmp_path / "pytest.ini" |
| 148 | + ini_original_content = "[pytest]\naddopts = -v --cov\n" |
| 149 | + ini_file.write_text(ini_original_content) |
| 150 | + |
| 151 | + # Create pyproject.toml |
| 152 | + toml_file = tmp_path / "pyproject.toml" |
| 153 | + toml_original_addopts = "-s -n auto" |
| 154 | + toml_original_content_dict = { |
| 155 | + "tool": {"pytest": {"ini_options": {"addopts": toml_original_addopts}}} |
| 156 | + } |
| 157 | + toml_original_content = tomlkit.dumps(toml_original_content_dict) |
| 158 | + toml_file.write_text(toml_original_content) |
| 159 | + |
| 160 | + with custom_addopts(): |
| 161 | + # Check INI file modification |
| 162 | + ini_modified_content = ini_file.read_text() |
| 163 | + config = configparser.ConfigParser() |
| 164 | + config.read_string(ini_modified_content) |
| 165 | + assert config.get("pytest", "addopts", fallback="") == "-v" |
| 166 | + |
| 167 | + # Check TOML file modification |
| 168 | + toml_modified_content = toml_file.read_text() |
| 169 | + modified_data = tomlkit.parse(toml_modified_content) |
| 170 | + modified_addopts = modified_data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "") |
| 171 | + assert modified_addopts == "-s" |
| 172 | + |
| 173 | + # Check that both files are restored |
| 174 | + assert ini_file.read_text().strip() == ini_original_content.strip() |
| 175 | + assert toml_file.read_text().strip() == toml_original_content.strip() |
| 176 | + |
| 177 | + |
| 178 | +def test_custom_addopts_restores_on_exception(tmp_path: Path) -> None: |
| 179 | + """Ensure config file is restored even if an exception occurs inside the context.""" |
| 180 | + config_file = tmp_path / "pytest.ini" |
| 181 | + original_content = "[pytest]\naddopts = -v --cov\n" |
| 182 | + config_file.write_text(original_content) |
| 183 | + |
| 184 | + os.chdir(tmp_path) |
| 185 | + with pytest.raises(ValueError, match="Test exception"): |
| 186 | + with custom_addopts(): |
| 187 | + raise ValueError("Test exception") |
| 188 | + |
| 189 | + restored_content = config_file.read_text() |
| 190 | + assert restored_content.strip() == original_content.strip() |
0 commit comments