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.

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