config: make `collect_ignore` accept any PathLike
The main reason is to remove a reference to `py.path`.
This commit is contained in:
		
							parent
							
								
									b26d1bb18f
								
							
						
					
					
						commit
						a03ee02817
					
				| 
						 | 
					@ -936,7 +936,7 @@ pytest treats some global variables in a special manner when defined in a test m
 | 
				
			||||||
**Tutorial**: :ref:`customizing-test-collection`
 | 
					**Tutorial**: :ref:`customizing-test-collection`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Can be declared in *conftest.py files* to exclude test directories or modules.
 | 
					Can be declared in *conftest.py files* to exclude test directories or modules.
 | 
				
			||||||
Needs to be ``list[str]``.
 | 
					Needs to be a list of paths (``str``, :class:`pathlib.Path` or any :class:`os.PathLike`).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. code-block:: python
 | 
					.. code-block:: python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1445,9 +1445,7 @@ class Config:
 | 
				
			||||||
        modpath = Path(mod.__file__).parent
 | 
					        modpath = Path(mod.__file__).parent
 | 
				
			||||||
        values: List[Path] = []
 | 
					        values: List[Path] = []
 | 
				
			||||||
        for relroot in relroots:
 | 
					        for relroot in relroots:
 | 
				
			||||||
            if isinstance(relroot, Path):
 | 
					            if isinstance(relroot, os.PathLike):
 | 
				
			||||||
                pass
 | 
					 | 
				
			||||||
            elif isinstance(relroot, LEGACY_PATH):
 | 
					 | 
				
			||||||
                relroot = Path(relroot)
 | 
					                relroot = Path(relroot)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                relroot = relroot.replace("/", os.sep)
 | 
					                relroot = relroot.replace("/", os.sep)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -367,12 +367,19 @@ class TestCustomConftests:
 | 
				
			||||||
    def test_collectignore_exclude_on_option(self, pytester: Pytester) -> None:
 | 
					    def test_collectignore_exclude_on_option(self, pytester: Pytester) -> None:
 | 
				
			||||||
        pytester.makeconftest(
 | 
					        pytester.makeconftest(
 | 
				
			||||||
            """
 | 
					            """
 | 
				
			||||||
            # potentially avoid dependency on pylib
 | 
					 | 
				
			||||||
            from _pytest.compat import legacy_path
 | 
					 | 
				
			||||||
            from pathlib import Path
 | 
					            from pathlib import Path
 | 
				
			||||||
            collect_ignore = [legacy_path('hello'), 'test_world.py', Path('bye')]
 | 
					
 | 
				
			||||||
 | 
					            class MyPathLike:
 | 
				
			||||||
 | 
					                def __init__(self, path):
 | 
				
			||||||
 | 
					                    self.path = path
 | 
				
			||||||
 | 
					                def __fspath__(self):
 | 
				
			||||||
 | 
					                    return "path"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            collect_ignore = [MyPathLike('hello'), 'test_world.py', Path('bye')]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            def pytest_addoption(parser):
 | 
					            def pytest_addoption(parser):
 | 
				
			||||||
                parser.addoption("--XX", action="store_true", default=False)
 | 
					                parser.addoption("--XX", action="store_true", default=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            def pytest_configure(config):
 | 
					            def pytest_configure(config):
 | 
				
			||||||
                if config.getvalue("XX"):
 | 
					                if config.getvalue("XX"):
 | 
				
			||||||
                    collect_ignore[:] = []
 | 
					                    collect_ignore[:] = []
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue