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 5.8KB

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