|
30 | 30 | """ |
31 | 31 |
|
32 | 32 | import re |
33 | | -from typing import Set |
| 33 | +from typing import List, Set |
34 | 34 | from urllib.parse import urlparse |
35 | 35 |
|
36 | 36 | import pytest |
@@ -72,6 +72,13 @@ def pytest_addoption(parser: pytest.Parser): |
72 | 72 | "Use '--show-ported-from=prs' to show PR URLs." |
73 | 73 | ), |
74 | 74 | ) |
| 75 | + ported_from_group.addoption( |
| 76 | + "--ported-from-output-file", |
| 77 | + action="store", |
| 78 | + dest="ported_from_output_file", |
| 79 | + default=None, |
| 80 | + help="Output file for ported_from information.", |
| 81 | + ) |
75 | 82 | ported_from_group.addoption( |
76 | 83 | "--links-as-filled", |
77 | 84 | action="store_true", |
@@ -99,9 +106,15 @@ def __init__(self, config) -> None: |
99 | 106 | self.config = config |
100 | 107 | self.show_mode = config.getoption("show_ported_from") |
101 | 108 | self.links_as_filled = config.getoption("links_as_filled") |
| 109 | + self.ported_from_output_file = config.getoption("ported_from_output_file") |
102 | 110 |
|
103 | 111 | @pytest.hookimpl(hookwrapper=True, trylast=True) |
104 | | - def pytest_collection_modifyitems(self, session, config, items): |
| 112 | + def pytest_collection_modifyitems( |
| 113 | + self, |
| 114 | + session: pytest.Session, |
| 115 | + config: pytest.Config, |
| 116 | + items: List[pytest.Item], |
| 117 | + ): |
105 | 118 | """Extract ported_from information from collected test items.""" |
106 | 119 | yield |
107 | 120 |
|
@@ -133,25 +146,27 @@ def pytest_collection_modifyitems(self, session, config, items): |
133 | 146 |
|
134 | 147 | # Output results based on mode |
135 | 148 | if self.show_mode == "prs": |
136 | | - output = sorted(prs) |
| 149 | + outputs = sorted(prs) |
137 | 150 | else: # default to "paths" |
138 | | - output = sorted(paths) |
139 | | - |
| 151 | + outputs = sorted(paths) |
| 152 | + output_lines: List[str] = [] |
140 | 153 | if self.links_as_filled: |
141 | | - all_files: list = [] |
142 | | - for item in output: |
143 | | - converted_link = convert_to_filled(item) |
144 | | - if converted_link is not None: |
145 | | - all_files.append(converted_link) |
146 | | - print(" ".join(all_files)) |
| 154 | + for output in outputs: |
| 155 | + converted_link_output = convert_to_filled(output) |
| 156 | + if converted_link_output is not None: |
| 157 | + output_lines.append(converted_link_output) |
| 158 | + else: |
| 159 | + output_lines.extend(outputs) |
| 160 | + if self.ported_from_output_file: |
| 161 | + with open(self.ported_from_output_file, "w") as f: |
| 162 | + f.write("\n".join(output_lines)) |
147 | 163 | else: |
148 | | - for item in output: |
149 | | - print(item) |
| 164 | + for line in output_lines: |
| 165 | + print(line) |
150 | 166 |
|
151 | 167 | @pytest.hookimpl(tryfirst=True) |
152 | 168 | def pytest_runtestloop(self, session): |
153 | 169 | """Skip test execution, only show ported_from information.""" |
154 | | - session.testscollected = 0 |
155 | 170 | return True |
156 | 171 |
|
157 | 172 | def pytest_terminal_summary(self, terminalreporter, exitstatus, config): |
|
0 commit comments