My dotfiles
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

115 righe
3.9 KiB

  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License.
  3. from collections import namedtuple
  4. class TestPath(namedtuple('TestPath', 'root relfile func sub')):
  5. """Where to find a single test."""
  6. def __new__(cls, root, relfile, func, sub=None):
  7. self = super(TestPath, cls).__new__(
  8. cls,
  9. str(root) if root else None,
  10. str(relfile) if relfile else None,
  11. str(func) if func else None,
  12. [str(s) for s in sub] if sub else None,
  13. )
  14. return self
  15. def __init__(self, *args, **kwargs):
  16. if self.root is None:
  17. raise TypeError('missing id')
  18. if self.relfile is None:
  19. raise TypeError('missing kind')
  20. # self.func may be None (e.g. for doctests).
  21. # self.sub may be None.
  22. class ParentInfo(namedtuple('ParentInfo', 'id kind name root parentid')):
  23. KINDS = ('folder', 'file', 'suite', 'function', 'subtest')
  24. def __new__(cls, id, kind, name, root=None, parentid=None):
  25. self = super(ParentInfo, cls).__new__(
  26. cls,
  27. str(id) if id else None,
  28. str(kind) if kind else None,
  29. str(name) if name else None,
  30. str(root) if root else None,
  31. str(parentid) if parentid else None,
  32. )
  33. return self
  34. def __init__(self, *args, **kwargs):
  35. if self.id is None:
  36. raise TypeError('missing id')
  37. if self.kind is None:
  38. raise TypeError('missing kind')
  39. if self.kind not in self.KINDS:
  40. raise ValueError('unsupported kind {!r}'.format(self.kind))
  41. if self.name is None:
  42. raise TypeError('missing name')
  43. if self.root is None:
  44. if self.parentid is not None or self.kind != 'folder':
  45. raise TypeError('missing root')
  46. elif self.parentid is None:
  47. raise TypeError('missing parentid')
  48. class TestInfo(namedtuple('TestInfo', 'id name path source markers parentid kind')):
  49. """Info for a single test."""
  50. MARKERS = ('skip', 'skip-if', 'expected-failure')
  51. KINDS = ('function', 'doctest')
  52. def __new__(cls, id, name, path, source, markers, parentid, kind='function'):
  53. self = super(TestInfo, cls).__new__(
  54. cls,
  55. str(id) if id else None,
  56. str(name) if name else None,
  57. path or None,
  58. str(source) if source else None,
  59. [str(marker) for marker in markers or ()],
  60. str(parentid) if parentid else None,
  61. str(kind) if kind else None,
  62. )
  63. return self
  64. def __init__(self, *args, **kwargs):
  65. if self.id is None:
  66. raise TypeError('missing id')
  67. if self.name is None:
  68. raise TypeError('missing name')
  69. if self.path is None:
  70. raise TypeError('missing path')
  71. if self.source is None:
  72. raise TypeError('missing source')
  73. else:
  74. srcfile, _, lineno = self.source.rpartition(':')
  75. if not srcfile or not lineno or int(lineno) < 0:
  76. raise ValueError('bad source {!r}'.format(self.source))
  77. if self.markers:
  78. badmarkers = [m for m in self.markers if m not in self.MARKERS]
  79. if badmarkers:
  80. raise ValueError('unsupported markers {!r}'.format(badmarkers))
  81. if self.parentid is None:
  82. raise TypeError('missing parentid')
  83. if self.kind is None:
  84. raise TypeError('missing kind')
  85. elif self.kind not in self.KINDS:
  86. raise ValueError('unsupported kind {!r}'.format(self.kind))
  87. @property
  88. def root(self):
  89. return self.path.root
  90. @property
  91. def srcfile(self):
  92. return self.source.rpartition(':')[0]
  93. @property
  94. def lineno(self):
  95. return int(self.source.rpartition(':')[-1])