From 04d3119a6a1e8b06ea75ecee471318536335526b Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Thu, 23 Jun 2022 17:42:05 -0700 Subject: [PATCH] Fix for :::: in node id for pytest. --- .../adapter/pytest/_pytest_item.py | 6 +- .../adapter/pytest/test_discovery.py | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py b/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py index 53c943b5bf41..bbddcdbefde6 100644 --- a/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py +++ b/pythonFiles/testing_tools/adapter/pytest/_pytest_item.py @@ -95,12 +95,12 @@ import sys -import pytest import _pytest.doctest import _pytest.unittest +import pytest from ..info import SingleTestInfo, SingleTestPath -from ..util import fix_fileid, PATH_SEP, NORMCASE +from ..util import NORMCASE, PATH_SEP, fix_fileid def should_never_reach_here(item, **extra): @@ -508,6 +508,8 @@ def _normalize_test_id( """Return the canonical form for the given node ID.""" while "::()::" in testid: testid = testid.replace("::()::", "::") + while ":::" in testid: + testid = testid.replace(":::", "::") if kind is None: return testid, testid orig = testid diff --git a/pythonFiles/tests/testing_tools/adapter/pytest/test_discovery.py b/pythonFiles/tests/testing_tools/adapter/pytest/test_discovery.py index 34b5c37a53e4..1b81f4e3bc99 100644 --- a/pythonFiles/tests/testing_tools/adapter/pytest/test_discovery.py +++ b/pythonFiles/tests/testing_tools/adapter/pytest/test_discovery.py @@ -1468,6 +1468,64 @@ def test_mysterious_parens(self): ], ) + def test_mysterious_colons(self): + stub = util.Stub() + discovered = StubDiscoveredTests(stub) + session = StubPytestSession(stub) + testroot = adapter_util.ABS_PATH(adapter_util.fix_path("/a/b/c")) + relfile = adapter_util.fix_path("x/y/z/test_eggs.py") + session.items = [ + create_stub_function_item( + stub, + nodeid=relfile + "::SpamTests:::()::test_spam", + name="test_spam", + originalname=None, + location=(relfile, 12, "SpamTests.test_spam"), + path=adapter_util.PATH_JOIN(testroot, relfile), + function=FakeFunc("test_spam"), + ), + ] + collector = _discovery.TestCollector(tests=discovered) + + collector.pytest_collection_finish(session) + + self.maxDiff = None + self.assertEqual( + stub.calls, + [ + ("discovered.reset", None, None), + ( + "discovered.add_test", + None, + dict( + parents=[ + ("./x/y/z/test_eggs.py::SpamTests", "SpamTests", "suite"), + ("./x/y/z/test_eggs.py", "test_eggs.py", "file"), + ("./x/y/z", "z", "folder"), + ("./x/y", "y", "folder"), + ("./x", "x", "folder"), + (".", testroot, "folder"), + ], + test=info.SingleTestInfo( + id="./x/y/z/test_eggs.py::SpamTests::test_spam", + name="test_spam", + path=info.SingleTestPath( + root=testroot, + relfile=adapter_util.fix_relpath(relfile), + func="SpamTests.test_spam", + sub=[], + ), + source="{}:{}".format( + adapter_util.fix_relpath(relfile), 13 + ), + markers=None, + parentid="./x/y/z/test_eggs.py::SpamTests", + ), + ), + ), + ], + ) + def test_imported_test(self): # pytest will even discover tests that were imported from # another module!