|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import configparser |
| 4 | +import os |
| 5 | +import stat |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +import pytest |
| 10 | +import tomlkit |
| 11 | + |
| 12 | +from codeflash.code_utils.code_utils import custom_addopts |
| 13 | + |
| 14 | +def test_custom_addopts_modifies_and_restores_dotini_file(tmp_path: Path) -> None: |
| 15 | + """Verify that custom_addopts correctly modifies and then restores a pytest.ini file.""" |
| 16 | + # Create a dummy pytest.ini file |
| 17 | + config_file = tmp_path / ".pytest.ini" |
| 18 | + original_content = "[pytest]\naddopts = -v --cov=./src -n auto\n" |
| 19 | + config_file.write_text(original_content) |
| 20 | + |
| 21 | + # Use patch to mock get_all_closest_config_files |
| 22 | + os.chdir(tmp_path) |
| 23 | + with custom_addopts(): |
| 24 | + # Check that the file is modified inside the context |
| 25 | + modified_content = config_file.read_text() |
| 26 | + config = configparser.ConfigParser() |
| 27 | + config.read_string(modified_content) |
| 28 | + modified_addopts = config.get("pytest", "addopts", fallback="") |
| 29 | + assert modified_addopts == "-v" |
| 30 | + |
| 31 | + # Check that the file is restored after exiting the context |
| 32 | + restored_content = config_file.read_text() |
| 33 | + assert restored_content.strip() == original_content.strip() |
| 34 | + |
| 35 | +def test_custom_addopts_modifies_and_restores_ini_file(tmp_path: Path) -> None: |
| 36 | + """Verify that custom_addopts correctly modifies and then restores a pytest.ini file.""" |
| 37 | + # Create a dummy pytest.ini file |
| 38 | + config_file = tmp_path / "pytest.ini" |
| 39 | + original_content = "[pytest]\naddopts = -v --cov=./src -n auto\n" |
| 40 | + config_file.write_text(original_content) |
| 41 | + |
| 42 | + # Use patch to mock get_all_closest_config_files |
| 43 | + os.chdir(tmp_path) |
| 44 | + with custom_addopts(): |
| 45 | + # Check that the file is modified inside the context |
| 46 | + modified_content = config_file.read_text() |
| 47 | + config = configparser.ConfigParser() |
| 48 | + config.read_string(modified_content) |
| 49 | + modified_addopts = config.get("pytest", "addopts", fallback="") |
| 50 | + assert modified_addopts == "-v" |
| 51 | + |
| 52 | + # Check that the file is restored after exiting the context |
| 53 | + restored_content = config_file.read_text() |
| 54 | + assert restored_content.strip() == original_content.strip() |
| 55 | + |
| 56 | + |
| 57 | +def test_custom_addopts_modifies_and_restores_toml_file(tmp_path: Path) -> None: |
| 58 | + """Verify that custom_addopts correctly modifies and then restores a pyproject.toml file.""" |
| 59 | + # Create a dummy pyproject.toml file |
| 60 | + config_file = tmp_path / "pyproject.toml" |
| 61 | + os.chdir(tmp_path) |
| 62 | + original_addopts = "-v --cov=./src --junitxml=report.xml" |
| 63 | + original_content_dict = { |
| 64 | + "tool": {"pytest": {"ini_options": {"addopts": original_addopts}}} |
| 65 | + } |
| 66 | + original_content = tomlkit.dumps(original_content_dict) |
| 67 | + config_file.write_text(original_content) |
| 68 | + |
| 69 | + # Use patch to mock get_all_closest_config_files |
| 70 | + os.chdir(tmp_path) |
| 71 | + with custom_addopts(): |
| 72 | + # Check that the file is modified inside the context |
| 73 | + modified_content = config_file.read_text() |
| 74 | + modified_data = tomlkit.parse(modified_content) |
| 75 | + modified_addopts = modified_data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "") |
| 76 | + assert modified_addopts == "-v" |
| 77 | + |
| 78 | + # Check that the file is restored after exiting the context |
| 79 | + restored_content = config_file.read_text() |
| 80 | + assert restored_content.strip() == original_content.strip() |
| 81 | + |
| 82 | + |
| 83 | +def test_custom_addopts_handles_no_addopts(tmp_path: Path) -> None: |
| 84 | + """Ensure custom_addopts doesn't fail when a config file has no addopts.""" |
| 85 | + # Create a dummy pytest.ini file without addopts |
| 86 | + config_file = tmp_path / "pytest.ini" |
| 87 | + original_content = "[pytest]\n# no addopts here\n" |
| 88 | + config_file.write_text(original_content) |
| 89 | + |
| 90 | + os.chdir(tmp_path) |
| 91 | + with custom_addopts(): |
| 92 | + # The file should not be modified |
| 93 | + content_inside_context = config_file.read_text() |
| 94 | + assert content_inside_context == original_content |
| 95 | + |
| 96 | + # The file should remain unchanged |
| 97 | + content_after_context = config_file.read_text() |
| 98 | + assert content_after_context == original_content |
| 99 | + |
| 100 | +def test_custom_addopts_handles_no_relevant_files(tmp_path: Path) -> None: |
| 101 | + """Ensure custom_addopts runs without error when no config files are found.""" |
| 102 | + # No config files created in tmp_path |
| 103 | + |
| 104 | + os.chdir(tmp_path) |
| 105 | + # This should execute without raising any exceptions |
| 106 | + with custom_addopts(): |
| 107 | + pass |
| 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