No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. return self.run([
  90. 'sdist',
  91. 'create',
  92. self.project_dir_arg,
  93. f'--out={self.build_dir / "created-sdist.sds"}',
  94. ])
  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. @property
  103. def default_builtin_toolchain(self) -> str:
  104. if os.name == 'posix':
  105. return ':c++17:gcc-9'
  106. elif os.name == 'nt':
  107. return ':c++17:msvc'
  108. else:
  109. raise RuntimeError(
  110. f'No default builtin toolchain defined for tests on platform "{os.name}"'
  111. )
  112. @property
  113. def exe_suffix(self) -> str:
  114. if os.name == 'posix':
  115. return ''
  116. elif os.name == 'nt':
  117. return '.exe'
  118. else:
  119. raise RuntimeError(
  120. f'We don\'t know the executable suffix for the platform "{os.name}"'
  121. )
  122. def catalog_create(self) -> subprocess.CompletedProcess:
  123. self.scratch_dir.mkdir(parents=True, exist_ok=True)
  124. return self.run(
  125. ['catalog', 'create', f'--catalog={self.catalog_path}'],
  126. cwd=self.test_dir)
  127. def catalog_import(self, json_path: Path) -> subprocess.CompletedProcess:
  128. self.scratch_dir.mkdir(parents=True, exist_ok=True)
  129. return self.run([
  130. 'catalog',
  131. 'import',
  132. f'--catalog={self.catalog_path}',
  133. f'--json={json_path}',
  134. ])
  135. def catalog_get(self, req: str) -> subprocess.CompletedProcess:
  136. return self.run([
  137. 'catalog',
  138. 'get',
  139. f'--catalog={self.catalog_path}',
  140. req,
  141. ])
  142. def set_contents(self, path: Union[str, Path],
  143. content: bytes) -> ContextManager[Path]:
  144. return fileutil.set_contents(self.source_root / path, content)
  145. @contextmanager
  146. def scoped_dds(test_dir: Path, project_dir: Path, name: str):
  147. dds_exe = Path(__file__).absolute().parent.parent / '_build/dds'
  148. if os.name == 'nt':
  149. dds_exe = dds_exe.with_suffix('.exe')
  150. with ExitStack() as scope:
  151. yield DDS(dds_exe, test_dir, project_dir, scope)
  152. class DDSFixtureParams(NamedTuple):
  153. ident: str
  154. subdir: Union[Path, str]
  155. def dds_fixture_conf(*argsets: DDSFixtureParams):
  156. args = list(argsets)
  157. return pytest.mark.parametrize(
  158. 'dds', args, indirect=True, ids=[p.ident for p in args])
  159. def dds_fixture_conf_1(subdir: Union[Path, str]):
  160. params = DDSFixtureParams(ident='only', subdir=subdir)
  161. return pytest.mark.parametrize('dds', [params], indirect=True, ids=['.'])