Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dds.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import os
  2. import itertools
  3. from contextlib import contextmanager, ExitStack
  4. from pathlib import Path
  5. from typing import Iterable, Union, Any, Dict, NamedTuple, ContextManager
  6. import subprocess
  7. import shutil
  8. import pytest
  9. from dds_ci import proc
  10. from . import fileutil
  11. class DDS:
  12. def __init__(self, dds_exe: Path, test_dir: Path, project_dir: Path, scope: ExitStack) -> None:
  13. self.dds_exe = dds_exe
  14. self.test_dir = test_dir
  15. self.source_root = project_dir
  16. self.scratch_dir = project_dir / '_test_scratch/Ю́рий Алексе́евич Гага́рин'
  17. self.scope = scope
  18. self.scope.callback(self.cleanup)
  19. @property
  20. def repo_dir(self) -> Path:
  21. return self.scratch_dir / 'repo'
  22. @property
  23. def catalog_path(self) -> Path:
  24. return self.scratch_dir / 'catalog.db'
  25. @property
  26. def deps_build_dir(self) -> Path:
  27. return self.scratch_dir / 'deps-build'
  28. @property
  29. def build_dir(self) -> Path:
  30. return self.scratch_dir / 'build'
  31. @property
  32. def lmi_path(self) -> Path:
  33. return self.scratch_dir / 'INDEX.lmi'
  34. def cleanup(self):
  35. if self.scratch_dir.exists():
  36. shutil.rmtree(self.scratch_dir)
  37. def run_unchecked(self, cmd: proc.CommandLine, *, cwd: Path = None) -> subprocess.CompletedProcess:
  38. full_cmd = itertools.chain([self.dds_exe, '-ltrace'], cmd)
  39. return proc.run(full_cmd, cwd=cwd or self.source_root)
  40. def run(self, cmd: proc.CommandLine, *, cwd: Path = None, check=True) -> subprocess.CompletedProcess:
  41. cmdline = list(proc.flatten_cmd(cmd))
  42. res = self.run_unchecked(cmd, cwd=cwd)
  43. if res.returncode != 0 and check:
  44. raise subprocess.CalledProcessError(res.returncode, [self.dds_exe] + cmdline, res.stdout)
  45. return res
  46. @property
  47. def repo_dir_arg(self) -> str:
  48. return f'--repo-dir={self.repo_dir}'
  49. @property
  50. def project_dir_arg(self) -> str:
  51. return f'--project-dir={self.source_root}'
  52. @property
  53. def catalog_path_arg(self) -> str:
  54. return f'--catalog={self.catalog_path}'
  55. def build_deps(self, args: proc.CommandLine, *, toolchain: str = None) -> subprocess.CompletedProcess:
  56. return self.run([
  57. 'build-deps',
  58. f'--toolchain={toolchain or self.default_builtin_toolchain}',
  59. self.catalog_path_arg,
  60. self.repo_dir_arg,
  61. f'--out={self.deps_build_dir}',
  62. f'--lmi-path={self.lmi_path}',
  63. args,
  64. ])
  65. def build(self,
  66. *,
  67. toolchain: str = None,
  68. apps: bool = True,
  69. warnings: bool = True,
  70. tests: bool = True,
  71. check: bool = True) -> subprocess.CompletedProcess:
  72. return self.run(
  73. [
  74. 'build',
  75. f'--out={self.build_dir}',
  76. f'--toolchain={toolchain or self.default_builtin_toolchain}',
  77. f'--catalog={self.catalog_path.relative_to(self.source_root)}',
  78. f'--repo-dir={self.repo_dir.relative_to(self.source_root)}',
  79. ['--no-tests'] if not tests else [],
  80. ['--no-apps'] if not apps else [],
  81. ['--no-warnings'] if not warnings else [],
  82. self.project_dir_arg,
  83. ],
  84. check=check,
  85. )
  86. def sdist_create(self) -> subprocess.CompletedProcess:
  87. self.build_dir.mkdir(exist_ok=True, parents=True)
  88. return self.run(['sdist', 'create', self.project_dir_arg], cwd=self.build_dir)
  89. def sdist_export(self) -> subprocess.CompletedProcess:
  90. return self.run([
  91. 'sdist',
  92. 'export',
  93. self.project_dir_arg,
  94. self.repo_dir_arg,
  95. ])
  96. def repo_import(self, sdist: Path) -> subprocess.CompletedProcess:
  97. return self.run(['repo', self.repo_dir_arg, 'import', sdist])
  98. @property
  99. def default_builtin_toolchain(self) -> str:
  100. if os.name == 'posix':
  101. return str(Path(__file__).parent.joinpath('gcc-9.tc.jsonc'))
  102. elif os.name == 'nt':
  103. return str(Path(__file__).parent.joinpath('msvc.tc.jsonc'))
  104. else:
  105. raise RuntimeError(f'No default builtin toolchain defined for tests on platform "{os.name}"')
  106. @property
  107. def exe_suffix(self) -> str:
  108. if os.name == 'posix':
  109. return ''
  110. elif os.name == 'nt':
  111. return '.exe'
  112. else:
  113. raise RuntimeError(f'We don\'t know the executable suffix for the platform "{os.name}"')
  114. def catalog_create(self) -> subprocess.CompletedProcess:
  115. self.scratch_dir.mkdir(parents=True, exist_ok=True)
  116. return self.run(['catalog', 'create', f'--catalog={self.catalog_path}'], cwd=self.test_dir)
  117. def catalog_import(self, json_path: Path) -> subprocess.CompletedProcess:
  118. self.scratch_dir.mkdir(parents=True, exist_ok=True)
  119. return self.run([
  120. 'catalog',
  121. 'import',
  122. f'--catalog={self.catalog_path}',
  123. f'--json={json_path}',
  124. ])
  125. def catalog_get(self, req: str) -> subprocess.CompletedProcess:
  126. return self.run([
  127. 'catalog',
  128. 'get',
  129. f'--catalog={self.catalog_path}',
  130. f'--out-dir={self.scratch_dir}',
  131. req,
  132. ])
  133. def set_contents(self, path: Union[str, Path], content: bytes) -> ContextManager[Path]:
  134. return fileutil.set_contents(self.source_root / path, content)
  135. @contextmanager
  136. def scoped_dds(test_dir: Path, project_dir: Path, name: str):
  137. dds_exe = Path(__file__).absolute().parent.parent / '_build/dds'
  138. if os.name == 'nt':
  139. dds_exe = dds_exe.with_suffix('.exe')
  140. with ExitStack() as scope:
  141. yield DDS(dds_exe, test_dir, project_dir, scope)
  142. class DDSFixtureParams(NamedTuple):
  143. ident: str
  144. subdir: Union[Path, str]
  145. def dds_fixture_conf(*argsets: DDSFixtureParams):
  146. args = list(argsets)
  147. return pytest.mark.parametrize('dds', args, indirect=True, ids=[p.ident for p in args])
  148. def dds_fixture_conf_1(subdir: Union[Path, str]):
  149. params = DDSFixtureParams(ident='only', subdir=subdir)
  150. return pytest.mark.parametrize('dds', [params], indirect=True, ids=['.'])