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