|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import ast |
| 4 | +import configparser |
4 | 5 | import difflib |
5 | 6 | import os |
6 | 7 | import re |
|
15 | 16 | import tomlkit |
16 | 17 |
|
17 | 18 | from codeflash.cli_cmds.console import logger, paneled_text |
18 | | -from codeflash.code_utils.config_parser import find_pyproject_toml |
| 19 | +from codeflash.code_utils.config_parser import find_pyproject_toml, get_all_closest_config_files |
19 | 20 |
|
20 | 21 | ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE) |
21 | 22 |
|
@@ -83,50 +84,54 @@ def create_rank_dictionary_compact(int_array: list[int]) -> dict[int, int]: |
83 | 84 | return {original_index: rank for rank, original_index in enumerate(sorted_indices)} |
84 | 85 |
|
85 | 86 |
|
| 87 | +def modify_addopts(config_file: Path) -> tuple[str, bool]: |
| 88 | + content = "" |
| 89 | + try: |
| 90 | + if config_file.suffix.lower() == "toml": |
| 91 | + # use tomlkit |
| 92 | + pass |
| 93 | + else: |
| 94 | + # use configparser |
| 95 | + pass |
| 96 | + except Exception: |
| 97 | + logger.debug("Trouble parsing") |
| 98 | + return content, False # not modified |
| 99 | + config = configparser.ConfigParser() |
| 100 | + config.read(config_file) |
| 101 | + # read file |
| 102 | + # parse |
| 103 | + # modify |
| 104 | + # save |
| 105 | + # return original content |
| 106 | + print(config_file) |
| 107 | + return "", True |
| 108 | + |
| 109 | + |
86 | 110 | @contextmanager |
87 | 111 | def custom_addopts() -> None: |
88 | | - pyproject_file = find_pyproject_toml() |
89 | | - # closest_config_files = get_all_closest_config_files() |
90 | | - original_content = None |
91 | | - non_blacklist_plugin_args = "" |
| 112 | + closest_config_files = get_all_closest_config_files() |
92 | 113 |
|
93 | | - try: |
94 | | - # Read original file |
95 | | - if pyproject_file.exists(): |
96 | | - with Path.open(pyproject_file, encoding="utf-8") as f: |
97 | | - original_content = f.read() |
98 | | - data = tomlkit.parse(original_content) |
99 | | - # Backup original addopts |
100 | | - original_addopts = data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "") |
101 | | - # nothing to do if no addopts present |
102 | | - if original_addopts != "" and isinstance(original_addopts, list): |
103 | | - non_blacklist_plugin_args = [] |
104 | | - for opt in original_addopts: |
105 | | - opt_stripped = opt.strip().lstrip("-") |
106 | | - # Filter out -n/--numprocesses and blacklisted options |
107 | | - if opt_stripped.startswith(("n=", "numprocesses=")) or any( |
108 | | - opt_stripped.startswith(b) for b in BLACKLIST_ADDOPTS |
109 | | - ): |
110 | | - continue |
111 | | - non_blacklist_plugin_args.append(opt) |
112 | | - |
113 | | - if non_blacklist_plugin_args != original_addopts: |
114 | | - data["tool"]["pytest"]["ini_options"]["addopts"] = non_blacklist_plugin_args |
115 | | - # Write modified file |
116 | | - with Path.open(pyproject_file, "w", encoding="utf-8") as f: |
117 | | - f.write(tomlkit.dumps(data)) |
| 114 | + # 1. find closest config files |
| 115 | + # 2. iterate through each of them and mask the addopts |
| 116 | + # 3. yield |
| 117 | + # 4. restore the original addopts when the context manager exits |
118 | 118 |
|
| 119 | + original_content = {} |
| 120 | + |
| 121 | + try: |
| 122 | + for config_file in closest_config_files: |
| 123 | + # Read original file |
| 124 | + print(config_file) |
| 125 | + # if pyproject_file.exists(): |
| 126 | + original_content[config_file] = modify_addopts(config_file) |
119 | 127 | yield |
120 | 128 |
|
121 | 129 | finally: |
122 | 130 | # Restore original file |
123 | | - if ( |
124 | | - original_content |
125 | | - and pyproject_file.exists() |
126 | | - and tuple(original_addopts) not in {(), tuple(non_blacklist_plugin_args)} |
127 | | - ): |
128 | | - with Path.open(pyproject_file, "w", encoding="utf-8") as f: |
129 | | - f.write(original_content) |
| 131 | + for file, (content, was_modified) in original_content.items(): |
| 132 | + if was_modified: |
| 133 | + with Path.open(file, "w", encoding="utf-8") as f: |
| 134 | + f.write(content) |
130 | 135 |
|
131 | 136 |
|
132 | 137 | @contextmanager |
|
0 commit comments