您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839
  1. """
  2. Test utilities for error checking
  3. """
  4. from contextlib import contextmanager
  5. from typing import Iterator
  6. import subprocess
  7. from pathlib import Path
  8. import tempfile
  9. import os
  10. @contextmanager
  11. def expect_error_marker(expect: str) -> Iterator[None]:
  12. """
  13. A context-manager function that should wrap a scope that causes an error
  14. from ``dds``.
  15. :param expect: The error message ID string that is expected to appear.
  16. The wrapped scope should raise :class:`subprocess.CalledProcessError`.
  17. After handling the exception, asserts that the subprocess wrote an
  18. error marker containing the string given in ``expect``.
  19. """
  20. tdir = Path(tempfile.mkdtemp())
  21. err_file = tdir / 'error'
  22. try:
  23. os.environ['DDS_WRITE_ERROR_MARKER'] = str(err_file)
  24. yield
  25. assert False, 'dds subprocess did not raise CallProcessError'
  26. except subprocess.CalledProcessError:
  27. assert err_file.exists(), \
  28. f'No error marker file was generated, but dds exited with an error (Expected "{expect}")'
  29. marker = err_file.read_text().strip()
  30. assert marker == expect, \
  31. f'dds did not produce the expected error (Expected {expect}, got {marker})'
  32. finally:
  33. os.environ.pop('DDS_WRITE_ERROR_MARKER')