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.

195 lines
6.2KB

  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], 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. def build_deps(self, args: proc.CommandLine, *,
  57. toolchain: str = None) -> subprocess.CompletedProcess:
  58. return self.run([
  59. 'build-deps',
  60. f'--toolchain={toolchain or self.default_builtin_toolchain}',
  61. f'--catalog={self.catalog_path}',
  62. f'--repo-dir={self.repo_dir}',
  63. f'--out={self.deps_build_dir}',
  64. f'--lmi-path={self.lmi_path}',
  65. args,
  66. ])
  67. def build(self,
  68. *,
  69. toolchain: str = None,
  70. apps: bool = True,
  71. warnings: bool = True,
  72. tests: bool = True,
  73. check: bool = True) -> subprocess.CompletedProcess:
  74. return self.run(
  75. [
  76. 'build',
  77. f'--out={self.build_dir}',
  78. f'--toolchain={toolchain or self.default_builtin_toolchain}',
  79. f'--catalog={self.catalog_path.relative_to(self.source_root)}',
  80. f'--repo-dir={self.repo_dir.relative_to(self.source_root)}',
  81. ['--no-tests'] if not tests else [],
  82. ['--no-apps'] if not apps else [],
  83. ['--no-warnings'] if not warnings else [],
  84. self.project_dir_arg,
  85. ],
  86. check=check,
  87. )
  88. def sdist_create(self) -> subprocess.CompletedProcess:
  89. self.build_dir.mkdir(exist_ok=True, parents=True)
  90. return self.run(['sdist', 'create', self.project_dir_arg],
  91. cwd=self.build_dir)
  92. def sdist_export(self) -> subprocess.CompletedProcess:
  93. return self.run([
  94. 'sdist',
  95. 'export',
  96. self.project_dir_arg,
  97. self.repo_dir_arg,
  98. ])
  99. def repo_import(self, sdist: Path) -> subprocess.CompletedProcess:
  100. return self.run(['repo', self.repo_dir_arg, 'import', sdist])
  101. @property
  102. def default_builtin_toolchain(self) -> str:
  103. if os.name == 'posix':
  104. return ':c++17:gcc-9'
  105. elif os.name == 'nt':
  106. return ':c++17:msvc'
  107. else:
  108. raise RuntimeError(
  109. f'No default builtin toolchain defined for tests on platform "{os.name}"'
  110. )
  111. @property
  112. def exe_suffix(self) -> str:
  113. if os.name == 'posix':
  114. return ''
  115. elif os.name == 'nt':
  116. return '.exe'
  117. else:
  118. raise RuntimeError(
  119. f'We don\'t know the executable suffix for the platform "{os.name}"'
  120. )
  121. def catalog_create(self) -> subprocess.CompletedProcess:
  122. self.scratch_dir.mkdir(parents=True, exist_ok=True)
  123. return self.run(
  124. ['catalog', 'create', f'--catalog={self.catalog_path}'],
  125. cwd=self.test_dir)
  126. def catalog_import(self, json_path: Path) -> subprocess.CompletedProcess:
  127. self.scratch_dir.mkdir(parents=True, exist_ok=True)
  128. return self.run([
  129. 'catalog',
  130. 'import',
  131. f'--catalog={self.catalog_path}',
  132. f'--json={json_path}',
  133. ])
  134. def catalog_get(self, req: str) -> subprocess.CompletedProcess:
  135. return self.run([
  136. 'catalog',
  137. 'get',
  138. f'--catalog={self.catalog_path}',
  139. req,
  140. ])
  141. def set_contents(self, path: Union[str, Path],
  142. content: bytes) -> ContextManager[Path]:
  143. return fileutil.set_contents(self.source_root / path, content)
  144. @contextmanager
  145. def scoped_dds(test_dir: Path, project_dir: Path, name: str):
  146. dds_exe = Path(__file__).absolute().parent.parent / '_build/dds'
  147. if os.name == 'nt':
  148. dds_exe = dds_exe.with_suffix('.exe')
  149. with ExitStack() as scope:
  150. yield DDS(dds_exe, test_dir, project_dir, scope)
  151. class DDSFixtureParams(NamedTuple):
  152. ident: str
  153. subdir: Union[Path, str]
  154. def dds_fixture_conf(*argsets: DDSFixtureParams):
  155. args = list(argsets)
  156. return pytest.mark.parametrize(
  157. 'dds', args, indirect=True, ids=[p.ident for p in args])
  158. def dds_fixture_conf_1(subdir: Union[Path, str]):
  159. params = DDSFixtureParams(ident='only', subdir=subdir)
  160. return pytest.mark.parametrize('dds', [params], indirect=True, ids=['.'])