You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 line
6.3KB

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