My dotfiles
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License.
  3. from __future__ import print_function
  4. import json
  5. def report_discovered(tests, parents, pretty=False, simple=False,
  6. _send=print, **_ignored):
  7. """Serialize the discovered tests and write to stdout."""
  8. if simple:
  9. data = [{
  10. 'id': test.id,
  11. 'name': test.name,
  12. 'testroot': test.path.root,
  13. 'relfile': test.path.relfile,
  14. 'lineno': test.lineno,
  15. 'testfunc': test.path.func,
  16. 'subtest': test.path.sub or None,
  17. 'markers': test.markers or [],
  18. } for test in tests]
  19. else:
  20. byroot = {}
  21. for parent in parents:
  22. rootdir = parent.name if parent.root is None else parent.root
  23. try:
  24. root = byroot[rootdir]
  25. except KeyError:
  26. root = byroot[rootdir] = {
  27. 'id': rootdir,
  28. 'parents': [],
  29. 'tests': [],
  30. }
  31. if not parent.root:
  32. root['id'] = parent.id
  33. continue
  34. root['parents'].append({
  35. 'id': parent.id,
  36. 'kind': parent.kind,
  37. 'name': parent.name,
  38. 'parentid': parent.parentid,
  39. })
  40. for test in tests:
  41. # We are guaranteed that the parent was added.
  42. root = byroot[test.path.root]
  43. testdata = {
  44. 'id': test.id,
  45. 'name': test.name,
  46. # TODO: Add a "kind" field
  47. # (e.g. "unittest", "function", "doctest")
  48. 'source': test.source,
  49. 'markers': test.markers or [],
  50. 'parentid': test.parentid,
  51. }
  52. root['tests'].append(testdata)
  53. data = [{
  54. 'rootid': byroot[root]['id'],
  55. 'root': root,
  56. 'parents': byroot[root]['parents'],
  57. 'tests': byroot[root]['tests'],
  58. } for root in sorted(byroot)]
  59. kwargs = {}
  60. if pretty:
  61. # human-formatted
  62. kwargs = dict(
  63. sort_keys=True,
  64. indent=4,
  65. separators=(',', ': '),
  66. )
  67. serialized = json.dumps(data, **kwargs)
  68. _send(serialized)