Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
Report file paths as POSIX on Windows
Browse files Browse the repository at this point in the history
For consistency, we use forward slashes for file paths on all
platforms and test frameworks.
  • Loading branch information
ramosbugs committed Nov 12, 2023
1 parent be0feda commit ce2c672
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/pytest_unflakable/_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def node_path(session: Union[pytest.Item, pytest.Session]) -> PathCompat:
return session.fspath


def relative_to(path: PathCompat, base: PathCompat) -> str:
def posix_relative_to(path: PathCompat, base: PathCompat) -> str:
if hasattr(path, 'relative_to'):
return str(path.relative_to(base))
return path.relative_to(base).as_posix()
else:
return str(path.relto(base)) # type: ignore
return Path(path.relto(base)).as_posix() # type: ignore


def item_name(item: _pytest.nodes.Node) -> Tuple[str, ...]:
Expand Down Expand Up @@ -243,7 +243,7 @@ def pytest_collection_modifyitems(
) -> None:
self.logger.debug('called hook pytest_collection_modifyitems')
for idx, item in enumerate(items):
test_path = relative_to(node_path(item), node_path(session))
test_path = posix_relative_to(node_path(item), node_path(session))
test_name = item_name(item)
if (test_path, test_name) in self.quarantined_tests:
self.logger.info(
Expand All @@ -261,14 +261,14 @@ def pytest_runtest_protocol(self, item: pytest.Item, nextitem: Optional[pytest.I
ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location)

assert self.session
test_filename = relative_to(node_path(item), node_path(self.session))
test_filename = posix_relative_to(node_path(item), node_path(self.session))
test_name = item_name(item)
for attempt in range(item.config.option.unflakable_failure_retries + 1):
if attempt > 0:
self.logger.info(
'retrying test `%s` in file %s (attempt %d of %d)',
'.'.join(test_name),
relative_to(node_path(item), node_path(item.session)),
posix_relative_to(node_path(item), node_path(item.session)),
attempt + 1,
item.config.option.unflakable_failure_retries + 1,
)
Expand Down Expand Up @@ -304,7 +304,7 @@ def pytest_runtest_makereport(
if item.parent:
pass

test_filename = relative_to(node_path(item), node_path(item.session))
test_filename = posix_relative_to(node_path(item), node_path(item.session))
test_name = item_name(item)
is_quarantined = (test_filename, test_name) in self.quarantined_tests and (
self.quarantine_mode == QuarantineMode.IGNORE_FAILURES)
Expand Down
22 changes: 15 additions & 7 deletions tests/test_unflakable.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,20 @@ def test_quarantine_flaky(
verbose: bool,
xdist: bool,
) -> None:
pytester.makepyfile(test_input="""
pytester.makepyfile(**{'folder/test_input': """
first_invocation = True
def test_pass():
pass
def test_flaky():
global first_invocation
if first_invocation:
first_invocation = False
assert False
""")
"""})

subprocess_mock.update(branch='MOCK_BRANCH', commit='MOCK_COMMIT')

Expand All @@ -160,24 +164,28 @@ def test_flaky():
manifest={'quarantined_tests': [
{
'test_id': 'MOCK_TEST_ID',
'filename': 'test_input.py',
'filename': 'folder/test_input.py',
'name': ['test_flaky']
}
]},
expected_test_file_outcomes=[
(
'test_input.py',
os.path.join('folder', 'test_input.py'),
[
(('test_pass',), [
_TestAttemptOutcome.PASSED,
]),
(('test_flaky',), [
_TestAttemptOutcome.QUARANTINED,
_TestAttemptOutcome.RETRY_PASSED,
])
]),
],
),
],
expected_test_result_counts=_TestResultCounts(num_quarantined=1),
expected_test_result_counts=_TestResultCounts(num_passed=1, num_quarantined=1),
expected_uploaded_test_runs={
('test_input.py', ('test_flaky',)): ['quarantined', 'pass'],
('folder/test_input.py', ('test_pass',)): ['pass'],
('folder/test_input.py', ('test_flaky',)): ['quarantined', 'pass'],
},
expected_exit_code=ExitCode.OK,
expect_xdist=xdist,
Expand Down

0 comments on commit ce2c672

Please sign in to comment.