Skip to content

Commit 72c6e94

Browse files
committed
fix: base path problem
1 parent 10257bc commit 72c6e94

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

smart_tests/commands/subset.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def subset(
237237
# PR merge hell. This should be moved to a top-level class
238238

239239
TestPathWriter.base_path = base_path
240+
TestPathWriter.base_path_explicitly_set = (base_path is not None)
240241

241242
class Optimize(TestPathWriter):
242243
# test_paths: List[TestPath] # doesn't work with Python 3.5
@@ -265,6 +266,11 @@ def set_test_runner(self, test_runner: str):
265266
"""Set the test runner name for this subset operation"""
266267
self.test_runner = test_runner
267268

269+
@property
270+
def base_path(self):
271+
"""Provide access to base_path for test runners compatibility"""
272+
return TestPathWriter.base_path
273+
268274
def _default_output_handler(self, output: list[TestPath], rests: list[TestPath]):
269275
if rest:
270276
self.write_file(rest, rests)

smart_tests/commands/test_path_writer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
class TestPathWriter(object):
1111
base_path: str | None = None
12+
base_path_explicitly_set: bool = False # Track if base_path was explicitly provided
1213

1314
def __init__(self, app: Application):
1415
self._formatter: Callable[[TestPath], str] = TestPathWriter.default_formatter
@@ -20,7 +21,9 @@ def __init__(self, app: Application):
2021
def default_formatter(cls, x: TestPath):
2122
"""default formatter that's in line with to_test_path(str)"""
2223
file_name = x[0]['name']
23-
if cls.base_path:
24+
# Only prepend base_path if it was explicitly set via --base option
25+
# Auto-inferred base paths should not affect output formatting
26+
if cls.base_path and cls.base_path_explicitly_set:
2427
# default behavior consistent with default_path_builder's relative
2528
# path handling
2629
file_name = join(str(cls.base_path), file_name)

smart_tests/testpath.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,17 @@ def _relativize(self, p: pathlib.Path) -> pathlib.Path:
123123

124124
return p
125125

126+
def get_effective_base_path(self) -> str | None:
127+
"""Get the effective base path, either explicitly set or inferred."""
128+
if self._base_path:
129+
return self._base_path
130+
return self._inferred_base_path
131+
126132
def _auto_infer_base_path(self, p: pathlib.Path) -> str | None:
127-
p = p.parent
133+
# If p is a file, start from its parent directory
134+
if p.is_file():
135+
p = p.parent
136+
128137
while p != p.root and not p.exists():
129138
p = p.parent
130139
try:

smart_tests/utils/dynamic_commands.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ def combined_function(*args, **kwargs):
136136
if hasattr(client, 'set_test_runner'):
137137
client.set_test_runner(test_runner_name)
138138

139+
# Auto-infer base path if not explicitly provided for all test runners
140+
# This ensures all test runners have access to base_path when needed
141+
has_base_path_attr = hasattr(client, 'base_path')
142+
base_path_is_none = client.base_path is None if has_base_path_attr else False
143+
no_inference_disabled = not kwargs.get('no_base_path_inference', False)
144+
145+
if has_base_path_attr and base_path_is_none and no_inference_disabled:
146+
147+
# Attempt to infer base path from current working directory
148+
try:
149+
import pathlib
150+
151+
from smart_tests.commands.test_path_writer import TestPathWriter
152+
from smart_tests.testpath import FilePathNormalizer
153+
154+
file_path_normalizer = FilePathNormalizer()
155+
inferred_base_path = file_path_normalizer._auto_infer_base_path(pathlib.Path.cwd().resolve())
156+
if inferred_base_path:
157+
TestPathWriter.base_path = inferred_base_path
158+
except Exception:
159+
# If inference fails, continue with None
160+
pass
161+
139162
# Prepare arguments for test runner function
140163
test_runner_args = [client] # First argument is always client
141164
test_runner_kwargs = {}

0 commit comments

Comments
 (0)