Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

100 lines
3.3KB

  1. from contextlib import ExitStack
  2. from typing import Any, Callable, Iterator
  3. from typing_extensions import Protocol
  4. from pathlib import Path
  5. import shutil
  6. from subprocess import check_call
  7. import pytest
  8. from dds_ci import paths
  9. from tests import scoped_dds, DDSFixtureParams, DDS
  10. # Exposes the HTTP fixtures:
  11. from .http import http_repo, http_tmp_dir_server # pylint: disable=unused-import
  12. class TempPathFactory(Protocol):
  13. def mktemp(self, basename: str, numbered: bool = True) -> Path:
  14. ...
  15. class PyTestConfig(Protocol):
  16. def getoption(self, name: str) -> Any:
  17. ...
  18. class TestRequest(Protocol):
  19. fixturename: str
  20. scope: str
  21. config: PyTestConfig
  22. fspath: str
  23. function: Callable[..., Any]
  24. param: DDSFixtureParams
  25. @pytest.fixture(scope='session')
  26. def dds_exe(pytestconfig: PyTestConfig) -> Path:
  27. opt = pytestconfig.getoption('--dds-exe') or paths.CUR_BUILT_DDS
  28. return Path(opt)
  29. @pytest.yield_fixture(scope='session') # type: ignore
  30. def dds_pizza_catalog(dds_exe: Path, tmp_path_factory: TempPathFactory) -> Path:
  31. tmpdir: Path = tmp_path_factory.mktemp(basename='dds-catalog')
  32. cat_path = tmpdir / 'catalog.db'
  33. check_call([str(dds_exe), 'repo', 'add', 'https://dds.pizza/repo', '--update', f'--catalog={cat_path}'])
  34. yield cat_path
  35. @pytest.yield_fixture # type: ignore
  36. def dds(request: TestRequest, dds_exe: Path, tmp_path: Path, worker_id: str, scope: ExitStack) -> Iterator[DDS]:
  37. test_source_dir = Path(request.fspath).absolute().parent
  38. test_root = test_source_dir
  39. # If we are running in parallel, use a unique directory as scratch
  40. # space so that we aren't stomping on anyone else
  41. if worker_id != 'master':
  42. test_root = tmp_path / request.function.__name__
  43. shutil.copytree(test_source_dir, test_root)
  44. project_dir = test_root / 'project'
  45. # Check if we have a special configuration
  46. if hasattr(request, 'param'):
  47. assert isinstance(request.param, DDSFixtureParams), \
  48. ('Using the `dds` fixture requires passing in indirect '
  49. 'params. Use @dds_fixture_conf to configure the fixture')
  50. params: DDSFixtureParams = request.param
  51. project_dir = test_root / params.subdir
  52. # Create the instance. Auto-clean when we're done
  53. yield scope.enter_context(scoped_dds(dds_exe, test_root, project_dir))
  54. @pytest.yield_fixture # type: ignore
  55. def scope() -> Iterator[ExitStack]:
  56. with ExitStack() as scope:
  57. yield scope
  58. def pytest_addoption(parser: Any) -> None:
  59. parser.addoption('--test-deps',
  60. action='store_true',
  61. default=False,
  62. help='Run the exhaustive and intensive dds-deps tests')
  63. parser.addoption('--dds-exe', help='Path to the dds executable under test', type=Path)
  64. def pytest_configure(config: Any) -> None:
  65. config.addinivalue_line('markers', 'deps_test: Deps tests are slow. Enable with --test-deps')
  66. def pytest_collection_modifyitems(config: PyTestConfig, items: Any) -> None:
  67. if config.getoption('--test-deps'):
  68. return
  69. for item in items:
  70. if 'deps_test' not in item.keywords:
  71. continue
  72. item.add_marker(
  73. pytest.mark.skip(
  74. reason='Exhaustive deps tests are slow and perform many Git clones. Use --test-deps to run them.'))